C++/CLI double pointer typecasting to ref IntPtr for C# access
I am writing a wrapper around C DLL in C++/CLI so that it can be accessed in C# through a managed assembly. Using direct P/Invoke to access C functions via C# is not possible because C DLL raises exceptions which cannot be correctly caught using P/Invoke (exception message is lost when moving across C/C# boundary). So the idea is to create a managed CLI DLL which internally calls C DLL and wraps th开发者_如何学JAVAe exception to Exception class of CLI.
So the C DLL function has this declaration.
void InitDB(void **handle);
The C# app would required following declaration
void InitDB_cs(ref IntPtr handle);
To accomplish this I created C++/CLI function with following declaration
void InitDB_clr(IntPtr %handle);
However, I am unable to typecast ref InPtr into C function. I tried using following code but seems can't get the typecasting right.
void InitDB_clr(IntPtr %handle)
{
pin_ptr<IntPtr> ptr = handle.ToPointer();
InitDB(&ptr);
}
Error message for above code
error C2440: 'initializing' : cannot convert from 'void *' to 'cli::pin_ptr<Type>'
1> with
1> [
1> Type=System::IntPtr
1> ]
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>error C2664: 'InitDB' : cannot convert parameter 1 from 'cli::pin_ptr<Type> *' to 'void **'
1> with
1> [
1> Type=System::IntPtr
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
That's an out
parameter, not really ref
, correct?
Try
void InitDB_clr(IntPtr% handle)
{
void* ptr /* = handle.ToPointer() */;
InitDB(&ptr);
handle = IntPtr(ptr);
}
Uncomment the initializer if it needs to be in/out.
You could use C# in an unsafe context and use
unsafe void InitDB_cs(IntPtr** handle);
精彩评论