English 中文(简体)
C# delegate with string reference to c++ callback
原标题:

I wrote a C# application that uses an unmanaged c++ dll via managed c++ dll. In the unmanaged dll, there s a callback that one of its params is std::string&.

I can t seem to find the right way to wrap this with the managed dll. When I use String^, the callback works, but the C# application does not get anything in the string. When I used String^%, things started to crash in other places that does not seem to be related (maybe memory corruption).

So my question is, what s the right way to do this?

Thanks

问题回答

I can t copy-paste code here, but I ll try to explain again. I can t use marshaling in the managed c++ section because I m not calling a function, but passing a c# delegate for a callback.

In the unmanaged dll, I have a callback that requires a function like this: void Func(unsigned int, int, std:string &).

My goal is to pass a c# delegate from my program to that callback, so in the unmanaged code, I made a delegate like this: delegate void DEL(unsigned int a, int b, String ^ c) and a function like: void mFunc(DEL ^ del), and that function marshal s the delegate into a cb that the unmanaged callback subscribe function accepts. The unsigned int and int works fine, but the string is always "" when the C# function is triggered.

Posting some code would help me understand better and give a better answer; but there is no automatic conversion or marshalling of String^ to std::string. You would need to do to marshalling yourself to get the string back to the C# code. A quick search can provide with details on how to do this.

http://msdn.microsoft.com/en-us/library/42zy2z41.aspx

I don t believe that the marshalling can deal with std::string. I think you need to make your own callback that passes char * and then write the glue code between the two.

Also, once the delegate is marshalled into a callback, that callback does not count as a reference to the object that the delegate might have been made from. So if the delegate is not a static method, you need to stuff it somewhere for the lifetime of the unmanaged callback.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签