问题
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 .....
回答1:
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
.
来源:https://stackoverflow.com/questions/27035788/c-sharp-pass-int-array-to-c-dll