I have a dll written in C, which I need to use in my C# application. But I have a problem,I have an interface which uses the structure from below:
typedef struct {
int (*Func1)(void*, int*);
int (*Func2)(void*, int*);
...
} myStructure;
So, I need to translate it to C#:
[StructLayout(LayoutKind.Sequential)]
public unsafe struct myStructure
{
public delegate int Func1(void* l,int* data);
public delegate int Func2(void* l,int* data);
...
}
In C# I need to couple functions of this structure to the actual methods, but I can not do this, because delegate are strong types and I need to make instance of these delegates, so i decided to change this structure a bit in C#, like this:
[StructLayout(LayoutKind.Sequential)]
public unsafe struct myStructure
{
public delegate int func1(void* l,int* data);
public delegate int func2(void* l,int* data);
...
public func1 Func1;
public func2 Func2;
...
}
It compiles, but application crashes and give this debug info: receive this error: Managed Debugging Assistant 'PInvokeStackImbalance' A call to PInvoke function 'myApp!myApp.Somenamespace::MyInterface' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature. The program '[7976] myApp.exe' has exited with code -1 (0xffffffff).
Some ideas, how can i solve this issue?