Search code examples
c#c++pinvokedllimport

Translating c++ types to C#


How would one "translate" following C++ function

LONG CALL_METHOD NET_SDK_Login(
    char *sDVRIP,
    WORD wDVRPort,
    char *sUserName,
    char *sPassword,
    LPNET_SDK_DEVICEINFO lpDeviceInfo);

to .Net (C#) to be used with P/Invoke? I tried with

[DllImport("DVR_NET_SDK.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int NET_SDK_Login(
    [MarshalAs(UnmanagedType.LPStr)] string sDVRIP,
    ushort wDVRPort,
    [MarshalAs(UnmanagedType.LPStr)] string sUserName,
    [MarshalAs(UnmanagedType.LPStr)] string sPassword,
    out _net_sdk_deviceinfo devinfo);

But no luck. Can anyone help?


Solution

  • The translation presented in the question is fine. The problem lies in your translation of the struct. As I explained in my answer to your previous question, you have not translated the struct correctly. You failed to translate the inline arrays in the struct.

    For example, in the C++ version of the struct we see this field:

    unsigned char deviceMAC[6]; 
    

    You translated that as

    byte deviceMAC;
    

    That is a clear mistake. I'm sure that you realise that it is impossible to fit a 6 byte MAC address into a single byte.

    That needs to be translated like this:

    [MarshalAs(UnmanagedType.ByValArray, SizeConst=6)]
    public byte[] deviceMAC;
    

    And so on for all the other arrays in the struct.