C# - Calling unmanaged DLL from managed C# code.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?cmtwYXQ=?=

    C# - Calling unmanaged DLL from managed C# code.

    I have a DLL that i'm interfacing with that's in C++. here's the interface
    defn:
    SendCommand(voi d *h, char *cmd, char *data, char *respbuffer);

    here's the wrapper for the dll call in my C# code:
    [DllImport("test .dll", CharSet = CharSet.Ansi, EntryPoint = "SendComman d")]
    unsafe public static extern char* SendCommand(voi d* h,
    [MarshalAs(Unman agedType.LPStr)] string cmd, [MarshalAs(Unman agedType.LPStr)]
    string data, [MarshalAs(Unman agedType.LPArra y)] ref byte[] buffer);

    and here's my function call:


    //code here...
    byte buf = new byte[25];
    SendCommand(han dle, "cmd string", "data", buf);

    i think the cmd parameter is getting messed up when i marshal it as LPStr!!
    am i doing something wrong? the reason i say this is i do not get any
    response in the response buffer!!!

    i could get the same function call working in C++ and i do see the response
    in the buffer...can someone please help?
  • Willy Denoyette [MVP]

    #2
    Re: C# - Calling unmanaged DLL from managed C# code.

    "rkpat" <rkpat@discussi ons.microsoft.c omwrote in message
    news:8F27EA3B-6B56-40D3-9B08-E6397824BABA@mi crosoft.com...
    >I have a DLL that i'm interfacing with that's in C++. here's the interface
    defn:
    SendCommand(voi d *h, char *cmd, char *data, char *respbuffer);
    >
    here's the wrapper for the dll call in my C# code:
    [DllImport("test .dll", CharSet = CharSet.Ansi, EntryPoint = "SendComman d")]
    unsafe public static extern char* SendCommand(voi d* h,
    [MarshalAs(Unman agedType.LPStr)] string cmd, [MarshalAs(Unman agedType.LPStr)]
    string data, [MarshalAs(Unman agedType.LPArra y)] ref byte[] buffer);
    >
    and here's my function call:
    >
    >
    //code here...
    byte buf = new byte[25];
    SendCommand(han dle, "cmd string", "data", buf);
    >
    i think the cmd parameter is getting messed up when i marshal it as LPStr!!
    am i doing something wrong? the reason i say this is i do not get any
    response in the response buffer!!!
    >
    i could get the same function call working in C++ and i do see the response
    in the buffer...can someone please help?


    Why the char* in :
    extern char* SendCommand
    there is no return defined in your C function.

    Try this:
    DllImport("test .dll", CharSet = CharSet.Ansi, EntryPoint = "SendComman d")]
    public static extern void SendCommand(Int Ptr h, string cmd, string data, StringBuilder
    buffer);

    IntPtr handle = ....;
    StringBuilder buf = new StringBuilder(2 5);
    SendCommand(han dle, ....);

    Willy.

    Comment

    • =?Utf-8?B?cmtwYXQ=?=

      #3
      Re: C# - Calling unmanaged DLL from managed C# code.

      thanks for the reply Willy.

      actually, the sendcommand function looks like this (my bad - typo)
      char * SendCommand(voi d *h, char *cmd, char *data, char *respbuffer);
      i'll try the recommended changes and will let u know how it goes. THANKS.


      "Willy Denoyette [MVP]" wrote:
      "rkpat" <rkpat@discussi ons.microsoft.c omwrote in message
      news:8F27EA3B-6B56-40D3-9B08-E6397824BABA@mi crosoft.com...
      I have a DLL that i'm interfacing with that's in C++. here's the interface
      defn:
      SendCommand(voi d *h, char *cmd, char *data, char *respbuffer);

      here's the wrapper for the dll call in my C# code:
      [DllImport("test .dll", CharSet = CharSet.Ansi, EntryPoint = "SendComman d")]
      unsafe public static extern char* SendCommand(voi d* h,
      [MarshalAs(Unman agedType.LPStr)] string cmd, [MarshalAs(Unman agedType.LPStr)]
      string data, [MarshalAs(Unman agedType.LPArra y)] ref byte[] buffer);

      and here's my function call:


      //code here...
      byte buf = new byte[25];
      SendCommand(han dle, "cmd string", "data", buf);

      i think the cmd parameter is getting messed up when i marshal it as LPStr!!
      am i doing something wrong? the reason i say this is i do not get any
      response in the response buffer!!!

      i could get the same function call working in C++ and i do see the response
      in the buffer...can someone please help?
      >
      >
      >
      Why the char* in :
      extern char* SendCommand
      there is no return defined in your C function.
      >
      Try this:
      DllImport("test .dll", CharSet = CharSet.Ansi, EntryPoint = "SendComman d")]
      public static extern void SendCommand(Int Ptr h, string cmd, string data, StringBuilder
      buffer);
      >
      IntPtr handle = ....;
      StringBuilder buf = new StringBuilder(2 5);
      SendCommand(han dle, ....);
      >
      Willy.
      >
      >

      Comment

      Working...