Delphi – COM server AnsiString parameters in Delphi 2009

comdelphidelphi-2009unicode

I have a simple COM dll with a method that takes two strings. In the type library editor of delphi these strings are defined as LPSTR. This translates to PChar in the TLB file. When upgrading from D2007 to D2009 this became a problem since PChar now has changed from PAnsiChar to PWideChar (it still becomes PChar in the TLB file when it is generated from the ridl file). And the interface needs to compatible with the previous one…

Is there a way to get PAnsiChar as type in the TLB file so that it corresponds to the previous declaration in D2007?

Best Solution

You can modify the generated code yourself. The easiest way is probably to redeclare PChar:

type
  PChar = PAnsiChar;

on top of the generated unit.

Or just search and replace all occurencies (where needed) of PChar with PAnsiChar.

BTW, it's a strange COM DLL since it's not Automation-compatible. Normally, BSTR (WideString in Delphi) is used for strings in COM.

Related Question