Problem invoking C DLL in C#

Posted by CristiC on Stack Overflow See other posts from Stack Overflow or by CristiC
Published on 2011-01-08T09:43:59Z Indexed on 2011/01/08 9:53 UTC
Read the original article Hit count: 183

Filed under:
|
|

I'm currently trying to invoke a method made in C from C#

C code looks like this:

extern "C"  int addSum(int a, int b)
{
    return a*b;
}

extern "C" int getCount()
{
    return 12;
}

and C# code looks like this:

 [DllImport("mydll.dll", SetLastError=true)]
 private static extern int addSum(IntPtr a, IntPtr b);
 [DllImport("mydll.dll", SetLastError = true)]
 private static extern int getCount();

 public static int mySum(int a, int b)
 {
     return suma(a, b);
 }

 public static int getMyCount()
 {
     return getCount();
 }

The code returns the right values but i'm getting the following error:

addSum' 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.

Any sugestion regarding this issue ?

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about c