如何将 Delphi 字符串传递给 Prism DLL?

发布于 2024-08-22 06:16:22 字数 279 浏览 4 评论 0原文

我们尝试将字符串从本机 Delphi 程序传递到 Delphi Prism DLL。 传递整数没有问题,但 DLL 中的字符串不匹配。 我们在回答另一个问题时看到了 Robert Love 的代码片段,但没有原生Delphi程序的代码。

我们如何将字符串从 Delphi 传递到 Delphi Prism DLL?

We try to pass a string from a native Delphi program to a Delphi Prism DLL.
We have no problem passing integers, but strings are mismatched in the DLL.
We saw Robert Love's code snippet in response to another question, but there is no code for the native Delphi program.

How can we pass strings from Delphi to a Delphi Prism DLL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

骷髅 2024-08-29 06:16:22

最好的方法是使用 WideString。

出于几个原因。

  • 它是 Unicode,在 D2009 之前工作。
  • 它的内存在 ole32.dll 中管理,因此不依赖于 Delphi 的内存管理器或 CLR GC。
  • 您不必直接处理指针

在 Oxygene 中,您可以这样编写:

type
  Sample = static class
  private
    [UnmanagedExport]
    method StringTest([MarshalAs(UnmanagedType.BStr)]input : String;
                      [MarshalAs(UnmanagedType.BStr)]out output : String);
  end;

implementation

method Sample.StringTest(input : String; out output : String);
begin
  output := input + "ä ~ î 暗";
end;

“MarshalAs”告诉 CLR 如何来回编组字符串。如果没有它,字符串将作为 Ansi (PAnsiChar) 传递,这可能不是您想要做的。

这是在 Delphi 中使用它的方法:

procedure StringTest(const input : WideString; out output : WideString);
  stdcall; external 'OxygeneLib';

var
  input, output : WideString;
begin
  input := 'A b c';
  StringTest(input, output);
  Writeln(output);
end.

此外,永远不要将未明确定义的类型用于外部接口。
您不得使用 PChar 进行 DLL 导入或导出。因为如果你这样做,当你用 D7 或 D2009 编译它时你会遇到异常(取决于原始的开发系统是什么)

The best way would be to use WideString.

For several reasons.

  • It is Unicode and works before D2009
  • It's memory is managed in ole32.dll, so no dependency on either Delphi's memory manager or the CLR GC.
  • You do not have to directly deal with pointers

In Oxygene, you could write it like so:

type
  Sample = static class
  private
    [UnmanagedExport]
    method StringTest([MarshalAs(UnmanagedType.BStr)]input : String;
                      [MarshalAs(UnmanagedType.BStr)]out output : String);
  end;

implementation

method Sample.StringTest(input : String; out output : String);
begin
  output := input + "ä ~ î 暗";
end;

"MarshalAs" tells the CLR how to marshal strings back and forth. Without it, strings are passed as Ansi (PAnsiChar), which is probably NOT what you would want to do.

This is how to use it from Delphi:

procedure StringTest(const input : WideString; out output : WideString);
  stdcall; external 'OxygeneLib';

var
  input, output : WideString;
begin
  input := 'A b c';
  StringTest(input, output);
  Writeln(output);
end.

Also, never ever use types, that are not clearly defined, for external interfaces.
You must not use PChar for DLL imports or exports. Because if you do, you will run into exceptions when you compile it with D7 or D2009 (depending on what the original dev system was)

罪#恶を代价 2024-08-29 06:16:22

Delphi Win32 中字符串的管理方式与 .Net 中的字符串不同,因此您不能将 .Net 字符串传递给 Delphi Win32,反之亦然。

要交换字符串值,最好使用两个编译器都支持的 PChar 类型。这与将字符串值发送到 Windows API 函数的方式相同。

问候

​我不是罗伯特;-)

Strings in Delphi Win32 are managed differently from strings in .Net, so you can not pass a .Net string to Delphi Win32 or vice versa.

To exchange strings values you'd better use PChar type which is supported by both compilers. That is the same way you send string values to Windows API functions.

Regards

P.S. I am NOT Robert ;-)

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