C# : Pass int array to c++ dll

Prasad

I have a C++ dll which is used to card printing( ID cards ). My implementation done using C#.Net. I used following code to call c++ dll.

[DllImport(@"J230i.dll",CallingConvention = CallingConvention.Cdecl,SetLastError=true)]
public static extern int N_PrintJobStatus(ref int[] nPrtintjobStatus);

int[] pJob = {0,0,0,0,0,0,0,0} ;

ret = N_PrintJobStatus( ref pJob);

N_PrintJobStatus method signature given as bellow

N_PrintJobStatus(int *pJobStatus )

After calling the method it gives following error

A call to PInvoke function '********!*********.frmCardPrint::N_PrintJobStatus' 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.

How can I fix this issue

thank you .....

David Heffernan

Your translation is incorrect. An int array, int* does not map to ref int[]. The latter would be marshalled as int**. You need instead to use int[].

[DllImport(@"J230i.dll", CallingConvention = CallingConvention.Cdecl, 
    SetLastError = true)]
public static extern int N_PrintJobStatus(int[] nPrtintjobStatus);

Allocate the array before calling the function. Presumably you have some way to determine how long it should be. As it stands this function looks like a buffer overrun waiting to happen. How can the function know how long the array is and so take steps to avoid writing beyond its end?

It's not clear that this is the only problem. We cannot be sure that the return type really is int. Or that the calling convention is cdecl. Or that the function really does call SetLastError.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related