Hello.
I have a dll(a.dll), C++ unmanaged, that has a an entry point with a parameter. This parameter is a pointer to a void function :
typedef void (__stdcall *ReceiveCallbackFunction)(XXX myObject);
int
WINAPI open(ReceiveCallbackFunction receiveCallbackFunction)
The XXX type is written once in a.dll and once in a MANAGED code(in other dll - b.dll... The difference between the XXX in a.dll and b.dll is that XXX from b.dll has a ref keyword in it`s declaration).
In C#, I declare a delegate, make an instance of it that points to a callback method and import the function from a.dll:
public
delegate void ReceiveCallbackFunction(XXX myObject); public static ReceiveCallbackFunction del = new ReceiveCallbackFunction(App.CallbackMethod);[
DllImport("a.dll")] public extern static void open(ReceiveCallbackFunction del);
//callbakc method
public
static void CallbackMethod(XXX myObject){
int i = 0;}
//invoke the open method
private
void button1_Click(object sender, EventArgs e){
App.openPort(App.del);}
When I run the application, I get this exception :
The runtime has encountered a fatal error. The address of the error was at 0x79f00d6e, on thread 0xe50. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
I have removed the XXX object and replaced it with void. And the callback worked(I've put a brakepoint at the line int i = 0; I am sure that I replace XXX object with an int, it will work). So.. I'm thinking that is a problem between the conversion from an unmanaged object to a managed one. I guess it dosen`t like my XXX managed object.
I don't really want to have the XXX class in 2 places... so I want to remove the one from managed code. But I don't know how to use the XXX class from unmanaged code in C#.
How can I consume the unmanaged class in C#
Or... anyone with any ideea how to solve this problem
Thanks