String Function Error in DLL from Delphi 7 to Delphi 11
When using a string function in a DLL file made in Delphi 11 from a Delphi 7 host application, you may encounter an "invalid pointer operation" error. This occurs because Delphi 7's String
type is AnsiString
, while newer Delphi versions (from D2009) use String=UnicodeString
. As a result, you attempt to pass different types between the EXE and DLL.
Additionally, AnsiString
in D7 and modern versions:
- Have different intrinsic structures.
- Are managed with distinct memory managers, so
Sharemem
doesn't resolve all possible memory management issues.
To avoid this error, it's advisable to use the same simple (not managed) type at both ends, such as PWideChar
or PAnsiChar
. Alternatively, you can utilize the Widestring
type, which implements the system BSTR
intended for COM interaction and is managed by the operating system.
Here's an example of how to use PWideChar
in Delphi 7 and Delphi 11:
Delphi 7
type PWideChar = ^WideChar; var sWideChar: PWideChar; begin sWideChar := WideStringToString('Wide String'); // Use sWideChar as a WideChar pointer end;
Delphi 11
type PWideChar = ^WideChar; var sWideChar: PWideChar; begin sWideChar := WideCharToString('Wide String'); // Use sWideChar as a WideChar pointer end;
By using the same simple type at both ends, you can ensure compatibility between the DLL and the host application, eliminating the "invalid pointer operation" error.
``` I hope this helps!