casting Void* to Object^

Jun 4, 2016 at 1:44am
Hello all,

I have a question on native void* to Object^ conversion.

basically I have a native c++ dll which will call a function in my CLI with a void* as a parameter like this:

bool Class1::func1 (int num0,void* pStruct,int num1,int num2)

then in my CLI I will have to convert the void* to an Object^ and pass it to the c# fucntion:

public bool func1(int num0, object pStruct, int num1, int num2)


i'm wondering how can I do that? I'm new to all these native<->managed stuff. Thanks a lot for your help.

by the way, the struct which the void* pointing to is like this:

struct myStruct
{
void* ptr;
int myInt1;
int myInt2;
long mylong1;
};
Jun 4, 2016 at 1:50am
Pass it to C# as an IntPtr and let C# do the marshaling. You'll probably want something like
1
2
3
4
5
6
public struct myStruct{
    IntPtr ptr;
    Int32 myInt1;
    Int32 myInt2;
    Int32 myLong1; //On Windows, longs are 32 bits wide.
};
Google "c# marshal pointer to struct" for more information.
Jun 5, 2016 at 12:10am
so you mean in my c# code, i need to have this struct?
public struct myStruct{
IntPtr ptr;
Int32 myInt1;
Int32 myInt2;
Int32 myLong1; //On Windows, longs are 32 bits wide.
};

or in my CLI code?

if in my CLI code, i cast my void* pStruct to Intptr:

IntPtr myIntP = IntPtr(pStruct);

and then I pass myIntP to the C# code, will that work?

thanks a lot again.

Kin
Jun 5, 2016 at 6:02am
Yes, that struct is for C#.

if in my CLI code, i cast my void* pStruct to Intptr:

IntPtr myIntP = IntPtr(pStruct);

and then I pass myIntP to the C# code, will that work?
I suppose. I don't really know.
Keep in mind, you'll still have to do the marshaling in C#.
Jun 5, 2016 at 9:33pm
Thanks Helios!

somewhat related to the same topic. How do we convert a native "const void* const" pointer to a managed object? I tried the same method as previous:

IntPtr myptr = IntPtr(myConstVoidConstPtr)

it will throw an error
Topic archived. No new replies allowed.