Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Delphi字符串传递给Prism DLL?_Delphi_Delphi 2010_Delphi Prism_Oxygene - Fatal编程技术网

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

如何将Delphi字符串传递给Prism DLL?,delphi,delphi-2010,delphi-prism,oxygene,Delphi,Delphi 2010,Delphi Prism,Oxygene,我们尝试将一个字符串从本机Delphi程序传递到Delphi Prism DLL。 传递整数没有问题,但是DLL中的字符串不匹配。 我们看到了对另一个问题的回答,但是没有本地Delphi程序的代码 如何将字符串从Delphi传递到Delphi Prism DLL?Delphi Win32中的字符串与.Net中的字符串管理方式不同,因此不能将.Net字符串传递到Delphi Win32,反之亦然 要交换字符串值,最好使用两个编译器都支持的PChar类型。这与向Windows API函数发送字符串值

我们尝试将一个字符串从本机Delphi程序传递到Delphi Prism DLL。 传递整数没有问题,但是DLL中的字符串不匹配。 我们看到了对另一个问题的回答,但是没有本地Delphi程序的代码


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

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

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

问候


另外,我不是罗伯特;-)

最好的方法是使用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编译它时,你会遇到异常(取决于最初的开发系统是什么)

这是一个社区,所以你不应该在你的问题中回答一个人。如果你想这样做,就给他的答案写一条评论。如果你写了一个问题(在这种情况下更好),试着把它表述得尽可能笼统。Robert Giesecke的回答比我的回答要好。对不起,这应该是给上述代码片段作者的一条消息。点击了错误的按钮。但是无论如何,使用PChar是如何工作的呢。Delphi prism没有PChar类型。如果我在本机delphi中使用pchar,在prism中使用string,那么第一个字符就正确了。但是只有一个字符。怎么解决?别担心,马库斯。你还没有足够的声望点,所以你无论如何也不能在别人的问题上发表表扬。这是一个合理的问题。我已经为您编辑了它,这样看起来您不会试图发送堆栈溢出不支持的直接消息。PChar只是一个指向字符的指针,要发送这样的字符串,您需要首先指向第一个字符,然后指定字符串数据的长度,这样,另一端就可以从起始点一直读取到字符串长度的数据。如果DLL函数返回任何字符串值,则另一端应提供一个内存空间,并将其起始地址和长度发送给函数,以便函数可以将输出数据写入该内存空间。如何从指针位置读取到prism中指定的长度?我不知道有什么方法可以做到这一点。我在prism中使用什么数据类型。当使用指针类并希望获取值时,它总是显示为指向void的指针。