【问题标题】:How Unicode strings can be passed from a managed to an unmanaged function如何将 Unicode 字符串从托管函数传递到非托管函数
【发布时间】:2010-05-31 05:48:00
【问题描述】:

对于如何将 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;

...

感谢您的宝贵时间。

【问题讨论】:

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


    【解决方案1】:

    .Net,因此.Net 的Delphi 无法编组win32 Delphi 的AnsiString。它确实知道如何编组 PChar。将 win32 参数更改为 PChar 应该可以工作。

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

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

    【讨论】:

    • 值得注意的是,D2007 是 PChar = PAnsiChar 的最后一个版本。 D2009 添加了 Unicode 支持,将 PChar 更改为 PWideChar。如果升级,则必须将 DllImport 更改为使用 CharSet.Unicode(或者您必须更改 DLL 以保持 ANSI)。
    【解决方案2】:

    我不完全了解 Delphi.NET 是如何处理事情的,但我有一些结合原生 win32 Delphi 和 C# .NET 代码的经验,如果你可以使用COM InterOp,你会看到 TLB 类库导入单位使用WideString 并为您处理接口,所以我会朝这个方向研究。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 2011-09-06
      • 2012-07-03
      • 1970-01-01
      • 2013-02-09
      相关资源
      最近更新 更多