views:

82

answers:

2

I will really appreciate anybody's help about how a Unicode string can be passed (marshaled) from a managed (Delphi .NET) to an unmanaged (Delphi's Win32 DLL) function.

The managed environment (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;

...

The unmanaged environment (Delphi's Win32 DLL):

library TM.PROFileConverter;

...

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

exports
  ImportLineworksFromPROFile;

...

Thank you for your time.

+3  A: 

.Net, and therefore Delphi for .Net, can not marshal win32 Delphi's AnsiString. It does know how to marshal PChar. Changing the win32 parameters to PChar should work.

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

PS: The unmanaged function uses ANSI encoding, not Unicode. If you want your unmanaged code to handle unicode you should use PWideChar or WideString instead of PChar and CharSet = CharSet.Unicode on the managed side.

Lars Truijens
It might also be worth noting that D2007 was the last version where PChar = PAnsiChar. D2009 added Unicode support, which changed PChar to PWideChar. If you upgrade, the DllImport must then be changed to use CharSet.Unicode (or you'll have to change the DLL to remain ANSI).
Michael Madsen
A: 

I don't exactly know how the Delphi.NET handles things, but I have some experience with combining native win32 Delphi and C# .NET code and if you can use COM InterOp, you will see the TLB class library import units use WideString and handle the interfacing for you, so I'd research in this direction.

Viktor Svub