I have a DLL and its header file, which were written in Visual C++. I need to use the following function in a C# project:
LIBSPEC int
CprFindDevices(
PCprDeviceInfo *ppDevInfo,
int *pNumDevices,
DWORD timeout
);
I am able to import it with a DllImport, but I cannot figure out how to implement the following structure in C#:
typedef struct _CprDeviceInfo
{
unsigned char id[ID_LEN];
unsigned char macAddr[MAC_LEN];
in_addr inAddr;
char ipAddr[IP_LEN];
char devName[INFO_NAME_LEN];
char port1Name[INFO_NAME_LEN];
char port2Name[INFO_NAME_LEN];
int tcpPort1;
int tcpPort2;
char interfaceIpAddr[IP_LEN];
} CprDeviceInfo, *PCprDeviceInfo;
The all-upper-case variables are all known constants which I can use in my C# project.
I know I must use [StructLayout(LayoutKind.Sequential)], but am not sure what the equivalent types are for each of the structure’s members, and what the function signature would be when I import it. It’s been a while since my C++ days.
This should do the job for you:
Make sure you are using
Charset.ASCIIin your DLLImport which is the default.