签名正常时持续检测到 PInvokeStackBalance
我的代码有一点问题。我有一个非托管 C++ .dll,我想将其与 C# 应用程序一起使用。
dll 中的 C++ 函数如下所示:
void EXPORT_API vSetLights(BYTE byLights)
{
remote.SetLEDs(byLights);
}
C# 代码如下所示:
[DllImport("APlugin")]
private static extern void vSetLights(byte byLights);
我这样调用该函数:
byte byLeds = 0x0;
vSetLights(byLeds);
如果我是正确的,则函数签名没问题(两者均不返回任何内容,并且需要一个字节),但 PInvokeStackBalance 保持不变弹出。之后应用程序运行得很好,我可以安全地忽略它吗?或者有修复吗?
谢谢,
韦斯利
I have a little problem with my code. I've got a unmanaged C++ .dll that I want to use with a C# applaction.
The C++ function in the dll looks like this:
void EXPORT_API vSetLights(BYTE byLights)
{
remote.SetLEDs(byLights);
}
The C# code looks like this:
[DllImport("APlugin")]
private static extern void vSetLights(byte byLights);
And I call the function like this:
byte byLeds = 0x0;
vSetLights(byLeds);
If I'm correct, the function signature is ok (Both return nothing and require a byte), but the PInvokeStackBalance keeps popping up. The application runs just fine after that, can I safely ifnore it or is there a fix?
Thanks,
Wesley
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的堆栈不平衡可能是由调用约定的差异引起的。
DllImport
中的默认调用约定是WinApi
(默认为StdCall
)。您的 C++ 函数可能正在使用Cdecl
调用约定。您必须向我们展示 EXPORT_API 的定义才能确定。出现堆栈不平衡的原因是
StdCall
期望被调用函数清理堆栈,而Cdecl
期望调用者清理堆栈。因此,如果需要StdCall
调用Cdecl
函数的程序,堆栈不会被清理。反之(Cdecl
调用StdCall
),堆栈会被清理两次。您可以将
DllImport
更改为使用Cdecl
,如下所示:[DllImport("APlugin", CallingConvention=CallingConvention.Cdecl)]
Your stack imbalance is probably caused by a difference in calling convention. The default calling convention in
DllImport
isWinApi
(which in turn defaults toStdCall
). It's likely that your C++ function is using theCdecl
calling convention. You'll have to show us the definition of EXPORT_API in order to be sure.The stack imbalance occurs because
StdCall
expects the called function to clean up the stack, andCdecl
expects the caller to clean up the stack. So if a program that expectsStdCall
calls aCdecl
function, the stack doesn't get cleaned up. Going the other way (Cdecl
callingStdCall
), the stack gets cleaned up twice.You can change your
DllImport
to useCdecl
, like this:[DllImport("APlugin", CallingConvention=CallingConvention.Cdecl)]