Bill Walton
Bill Walton

Reputation: 823

'unsafe' functions in C++/CLI wrapper?

Firstly allow me to thank everyone who helped me over the last few days, I finally have a C++/CLI wrapper for my C++ class, which I can create instances of in C#, thankyou StackOverflow!

Some of these functions take pointers as parameters however, when using PInvoke you can just declare the function as unsafe and then call it with the ref keyword like so

[DllImport("DLLName.dll")]
public static unsafe extern someFunc(ref int);

// then in main... 
int a;
someFunc(ref a);

but how do I go about wrapping C++ functions that take pointer paramaters in C++/CLI so that I can call them from C#?

Thanks.

EDIT - My function signiatures are

UINT Init(char* info[], int amount, int ID1, int ID2)       
UINT GetJPValues(SJPInfo* jpi, int iTimeoutMS)
UINT GetNums(int amount, int* out, int iTimeoutMS)
UINT GetJPWValues(SJP* jp, int ID, UINT stake, int iTimeoutMS)
UINT Confirm(bool *bConfirmed, int ID, int iTimeoutMS)

JPInfo and SJP are C++ structs that contain only data, I've created C# versions that are packed the same way as their C++ counterparts but I'm also not sure how I can take the C# structs as a parameter to the C++/CLI functions which then call the C++ functions.

EDIT 2 - Forgot to mention the structs are just filled with data from a winsock recv in the C++ code, so they could probably be passed in as char*?

Upvotes: 0

Views: 1407

Answers (1)

Alastair Taylor
Alastair Taylor

Reputation: 425

From experience I always keep the interface to simple types like int,double etc (I did also use String^ in the interface and simply converted to std::string using code that's easy to find on the Internet).

This does tend to mean exposed functions in the wrapper class take more parameters but it's easy to understand and most importantly it works!

Upvotes: 1

Related Questions