简体   繁体   English

C#:将 int 数组传递给 C++ dll

[英]C# : Pass int array to c++ dll

I have a C++ dll which is used to card printing( ID cards ).我有一个用于卡片打印(身份证)的 C++ dll。 My implementation done using C#.Net.我的实现是使用 C#.Net 完成的。 I used following code to call c++ dll.我使用以下代码来调用 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 方法签名如下

N_PrintJobStatus(int *pJobStatus )

After calling the method it gives following error调用该方法后出现以下错误

A call to PInvoke function '********!*********.frmCardPrint::N_PrintJobStatus' has unbalanced the stack.对 PInvoke 函数 '********!*********.frmCardPrint::N_PrintJobStatus' 的调用使堆栈不平衡。 This is likely because the managed PInvoke signature does not match the unmanaged target signature.这可能是因为托管 PInvoke 签名与非托管目标签名不匹配。 Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.检查 PInvoke 签名的调用约定和参数是否与目标非托管签名匹配。

How can I fix this issue我该如何解决这个问题

thank you .....谢谢 .....

Your translation is incorrect.你的翻译不正确。 An int array, int* does not map to ref int[] . int 数组int*不映射到ref int[] The latter would be marshalled as int** .后者将被编组为int** You need instead to use int[] .您需要改用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 .我们不能确定返回类型真的是int Or that the calling convention is cdecl.或者调用约定是 cdecl。 Or that the function really does call SetLastError .或者该函数确实调用了SetLastError

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM