C++/CLI double pointer typecasting to ref IntPtr for C# access

柔情痞子 提交于 2019-12-24 02:15:06

问题


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 the 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

回答1:


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.




回答2:


You could use C# in an unsafe context and use

unsafe void InitDB_cs(IntPtr** handle);


来源:https://stackoverflow.com/questions/6810419/c-cli-double-pointer-typecasting-to-ref-intptr-for-c-sharp-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!