Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Marshalling int* to C#

I've a function in C++ void someFunc(char* arg1, int* arg2) which I want to marshal the parameters as I use this function in C# (after importing the DLL)..

Can you pls tell me how I should marshall as I'm confused here.

[DllImport(Dllname)]
extern void someFunc([MarshallAsAttribute(UnmanagedType,LPStr)] string arg1, IntPtr arg2);

Should I use an IntPtr here? I cant pass the address of any int variable from C# so that it would land up in the pointer in C++?

like image 622
stack_pointer is EXTINCT Avatar asked Jun 19 '12 16:06

stack_pointer is EXTINCT


1 Answers

The problem is int* can be used for a lot of different scenarios in C. How you marshal this depends a bit on what the int* arg2 is meant to represent.

For example, if it's just setting a value of an int, you can marshal this as ref int. However, if the int* is representing an array, you'll want to pass an array (this is unlikely, however, as there is no length term, which is common when using an array via a pointer).

like image 168
Reed Copsey Avatar answered Nov 08 '22 02:11

Reed Copsey