Unicode字符串如何从托管函数传递到非托管函数

时间:2010-05-31 05:48:01

标签: .net delphi delphi-2007 delphi.net

我将非常感谢任何人对如何将Unicode字符串从托管(Delphi .NET)传递(编组)到非托管(Delphi的Win32 DLL)函数的帮助。

托管环境(Delphi .NET):

...

interface

...

const TM_PRO_CONVERTER = 'TM.PROFileConverter.dll';

function ImportLineworksFromPROFile(FileName          :String; 
                                    TargetFileNameDXF :String): Integer;

...

implementation

...

[DllImport(TM_PRO_CONVERTER, EntryPoint = 'ImportLineworksFromPROFile', 
           CharSet = CharSet.Ansi, SetLastError = True, 
           CallingConvention = CallingConvention.StdCall)]

function ImportLineworksFromPROFile(FileName          :String; 
                                    TargetFileNameDXF :String): Integer; external;

...

非托管环境(Delphi的Win32 DLL):

library TM.PROFileConverter;

...

function ImportLineworksFromPROFile(FileName          :String;
                                    TargetFileNameDXF :String) :Integer; stdcall;

exports
  ImportLineworksFromPROFile;

...

感谢您的时间。

2 个答案:

答案 0 :(得分:3)

<。> .Net,因此Delphi for .Net,不能编组win32 Delphi的AnsiString。它确实知道如何编组PChar。将win32参数更改为PChar应该可以正常工作。

function ImportLineworksFromPROFile(FileName          :PChar;
                                    TargetFileNameDXF :PChar) :Integer; stdcall;

PS:非托管函数使用ANSI编码,而不是Unicode。如果您希望非托管代码处理unicode,您应该在托管端使用PWideChar或WideString而不是PChar和CharSet = CharSet.Unicode。

答案 1 :(得分:0)

我不知道Delphi.NET如何处理事情,但我有一些组合本机win32 Delphi和C#.NET代码的经验,如果你可以使用COM InterOp,你会看到TLB类库导入单元使用WideString并为您处理接口,因此我会朝这个方向进行研究。