If your StructLayout is consistent, then it is really identical.
The easiest way to check this out for yourself is, of course, to try:
Create a C ++ Win32 DLL project:
extern "C" { __declspec(dllexport) void MyFunction(const void* ptr) {
Make a C # project:
public struct Foo { public IntPtr x; } [DllImport(@"Win32Project1.dll", EntryPoint = "MyFunction", CallingConvention = CallingConvention.Cdecl)] public static extern void MyFunctionWithIntPtr(IntPtr x); [DllImport(@"Win32Project1.dll", EntryPoint = "MyFunction", CallingConvention = CallingConvention.Cdecl)] public static extern void MyFunctionWithStruct(Foo x); static void Main(string[] args) { IntPtr j = new IntPtr(10); var s = new Foo(); sx = new IntPtr(10); MyFunctionWithIntPtr(j); MyFunctionWithStruct(s); }
In the debug settings, make sure that you select "Initial debugging".
You will see that both values ββare 0xA.
Please note that if you use out / ref parameters for your IntPtr vs Struct, they will be different values.
source share