I see the following issues:
LRESULT
is a pointer sized signed integer. That'sIntPtr
on C#.LONG
is a macro that expands tolong
. And on Windows, C++long
is a 32 bit signed integer. So on the C# side you need to useint
.- The struct declaration is wrong (see below). And you fail to pass the struct by reference.
- The macro
CALLBACK
means that the function isstdcall
. - There is also no need at all for
unsafe
. You should remove that.
The code should be:
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct SENSDEVICEW
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string szSerialNo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string szDeviceID;
public int nIndex;
}
[DllImport("...", CallingConvention=CallingConvention.Stdcall,
CharSet=CharSet.Unicode)]
static extern IntPtr SensFindDeviceW(int n, string pszMask,
ref SENSDEVICEW pDevice);
Finally, I am assuming that pszMask
is an input parameter. And hence string
would be correct. However, the C++ code declares it as LPWSTR
when an input parameter ought to be LPCWSTR
. You might like to check the semantics of this parameter. It certainly feels like an input parameter in which case the C++ coder was a little sloppy.