DLLImport get 尝试读取或写入受保护的内存
我要疯了。
我有一个dll,具有以下功能:
function MyFunc(myId: integer; var LstCB: array of char): integer; stdcall;
第一个参数是一个可怜的整数。 但第二个是一个 char[2048] ,它得到这样的东西
('9', #13, #10, '8', '8', '8', '8', '0', '0', '0', '0', '0', '0', '0', '0', '2', '5', '0', '7', #13, #10, '8', '8', '8', '8', '0', '0', '0', '0', '0', '0', '0', '0', '2', '6', '0', #13, #10, '8', '8', '8', '8', '0', '0', '0', '0', '0', '0', '0', '0', '3', '3', '1', '5', #13, #10, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,....#0)
我用 DllImport 导入了它:
[DllImport(@"MyDll.dll", EntryPoint = "MyFunc", CallingConvention = CallingConvention.StdCall)]
internal static extern int MyFunc(int myId, string list);
我得到:
Attempted to read or write protected memory. This is often an indication that other memory has been corrupted.
你有什么想法吗???
谢谢。
I'm going crazy.
I have a dll, with this function :
function MyFunc(myId: integer; var LstCB: array of char): integer; stdcall;
The first parameter is a poor integer.
But the second one is a char[2048] which get someting like this
('9', #13, #10, '8', '8', '8', '8', '0', '0', '0', '0', '0', '0', '0', '0', '2', '5', '0', '7', #13, #10, '8', '8', '8', '8', '0', '0', '0', '0', '0', '0', '0', '0', '2', '6', '0', #13, #10, '8', '8', '8', '8', '0', '0', '0', '0', '0', '0', '0', '0', '3', '3', '1', '5', #13, #10, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,....#0)
I imported this with DllImport :
[DllImport(@"MyDll.dll", EntryPoint = "MyFunc", CallingConvention = CallingConvention.StdCall)]
internal static extern int MyFunc(int myId, string list);
And I get :
Attempted to read or write protected memory. This is often an indication that other memory has been corrupted.
Have you some ideas please ???
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 Delphi 函数使用开放数组作为字符串参数。这不应该跨 DLL 边界公开。调用 Delphi 开放数组的协议是特定于实现的。
您应该更改 Delphi 代码以接收
PChar
。如果数据从 C# 传递到 Delphi DLL,那么您的 P/invoke 就没有问题。如果 DLL 旨在将数据返回到 C# 代码,那么您需要在 P/invoke 中将文本参数声明为
StringBuilder
。唯一需要注意的是 Delphi 中
char
的含义。如果 DLL 是使用 Delphi 2009 或更高版本构建的,则char
是 Unicode 字符,您需要在 P/invoke 中指定CharSet
。Your Delphi function is using an open array for the string parameter. This is not something that should be exposed across a DLL boundary. The protocol for calling an Delphi open array is implementation specific.
You should change your Delphi code to receive a
PChar
.If the data is being passed from C# to the Delphi DLL then your P/invoke is fine. If the DLL is meant to return data to the C# code then you need to declare the text parameter as
StringBuilder
in the P/invoke.The only other thing to watch out for is what the meaning of
char
is in Delphi. If the DLL is built with Delphi 2009 or later thenchar
is a Unicode character and you need to specify theCharSet
in your P/invoke.