stack_pointer is EXTINCT
stack_pointer is EXTINCT

Reputation: 2393

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++?

Upvotes: 7

Views: 5162

Answers (1)

Reed Copsey
Reed Copsey

Reputation: 564413

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).

Upvotes: 10

Related Questions