在.Net 4中:PInvokeStackImbalance异常
我在 .Net 3.5 项目中使用 msvcrt.dll
中的 strlen
函数。更具体地说:
private unsafe static extern int strlen( byte *pByte );
迁移到 .NET 4.0 后,如果我使用此函数,它会抛出 PInvokeStackImbalance
异常。
如何导入 .NET 3.5 msvcrt.dll
或修复此异常?
I was using the strlen
function from msvcrt.dll
in a .Net 3.5 project. More specifically:
private unsafe static extern int strlen( byte *pByte );
After migrating to .NET 4.0, if I use this function it throws a PInvokeStackImbalance
exception.
How can I import the .NET 3.5 msvcrt.dll
or fix this exception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑问题出在调用约定上,您应该使用 Cdecl。
I suspect that the problem is with the calling convention, you should be using Cdecl.
这实际上并不是一个直接的答案,但似乎对于像这样的功能,最好自己编写。例如,以下 C# 代码可能会起作用(尽管可能有一个使用现有函数的衬里也可以起作用):
This is not really a direct answer, but it seems that for something like this functionality, it might be better to write your own. For example, the following C# code might work (although there are probably one liners using existing functions that would work too):
从 .NET 3.5 到 4,不应有任何更改。(顺便说一句,msvcrt.dll 不是框架的一部分 - 它是 Microsft C++ 运行时库)。您确定您的项目中没有其他任何变化吗?
我刚刚尝试了这段代码,它可以正常工作并按预期打印“4”:
我不清楚为什么您想要从托管代码中调用 strlen,但当然您可能有自己的理由。如果您需要另一种托管实现,这里有一个您可以使用的单行:
当然,它不处理多字节(unicode)字符,但我认为 strlen 也不处理。
There should not be any changes in this from .NET 3.5 to 4. (and, btw, msvcrt.dll is not part of the framework - it is the Microsft C++ Runtime Library). Are you sure nothing else has changed in your project.
I just tried this code, which works and prints "4" as expected:
It is unclear to me why you would ever want to call strlen from managed code, but of course you might have your reasons. If you need an alternative managed implementation, here is a one liner you can use:
Of course that does not deal with multi-byte (unicode) characters, but I don't think strlen does either.
只是为了好玩:
Just for fun :