DLLImport get 尝试读取或写入受保护的内存

发布于 2024-12-07 23:01:47 字数 1041 浏览 0 评论 0原文

我要疯了。

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我早已燃尽 2024-12-14 23:01:47

您的 Delphi 函数使用开放数组作为字符串参数。这不应该跨 DLL 边界公开。调用 Delphi 开放数组的协议是特定于实现的。

您应该更改 Delphi 代码以接收 PChar

function MyFunc(myId: Integer; LstCB: PChar): Integer; stdcall;

如果数据从 C# 传递到 Delphi DLL,那么您的 P/invoke 就没有问题。如果 DLL 旨在将数据返回到 C# 代码,那么您需要在 P/invoke 中将文本参数声明为 StringBuilder

[DllImport(@"MyDll.dll", EntryPoint = "MyFunc", CallingConvention = CallingConvention.StdCall)]
internal static extern int MyFunc(int myId, StringBuilder list);
...
StringBuilder list = new StringBuilder(2048);
int res = MyFunc(ID, list);
string theList = list.ToString();

唯一需要注意的是 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.

function MyFunc(myId: Integer; LstCB: PChar): Integer; stdcall;

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.

[DllImport(@"MyDll.dll", EntryPoint = "MyFunc", CallingConvention = CallingConvention.StdCall)]
internal static extern int MyFunc(int myId, StringBuilder list);
...
StringBuilder list = new StringBuilder(2048);
int res = MyFunc(ID, list);
string theList = list.ToString();

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 then char is a Unicode character and you need to specify the CharSet in your P/invoke.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文