简体   繁体   中英

PInvoke with a void * versus a struct with an IntPtr

Imagine I have a function called

Myfunction(const void * x);

My C# declaration could be

MyFunction(IntPtr x);

Is this functionally and technically equivalent to

struct MyStruct { IntPtr P; }

MyFunction(MyStruct x);

Or will there be a difference in how they are marshalled.

I'm asking this because the library I'm calling is all void *, typedef'd to other names, and in C# I'd like to get type safety, for what it's worth.

If your StructLayout is Sequential, then it is indeed identical.

Easiest way to verify this for yourself is to try it out, of course:

Make a C++ Win32 DLL project:

extern "C"
{
    __declspec(dllexport) void MyFunction(const void* ptr)
    {
       // put a breakpoint and inspect
    }
}

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();
        s.x = new IntPtr(10);
        MyFunctionWithIntPtr(j);
        MyFunctionWithStruct(s);
    }

In your debug settings, make sure you select Native debugging is enabled.

You'll see both values to be 0xA.

Note, however, if you use out/ref parameters for your IntPtr vs Struct, they will be different values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM