Link to home
Create AccountLog in
Avatar of slaterm1961
slaterm1961

asked on

Simple Example of Pchar in Delphi DLL

Hi,
I am trying to understand another area of Delphi, that of DLL's and Pointers. In the Documentation that i have read to date, it says that when passing strings to and from a function contained in an extermal  dll, that you either use strings of the shortstring type, or use a pchar as a pointer.

I seem to have some problem with Syntax however, as the following code gives me an error message when i try and compile it.

[E2010 Incompatible types: 'string' and 'PAnsiChar

what am i doing wrong ?
'
library test;
 
{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }
 
uses
  SysUtils,
  Classes,
  Registry,
  Windows;
 
{$R *.res}
 
function getMyDocumentsFolder():Pchar;stdcall;
var
  reg: TRegistry;
  MyDocumentsFolder: Pointer;
begin
  reg := nil;
  try
    reg := reg.Create;
    reg.RootKey := HKEY_CURRENT_USER;
    reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', false);
    MyDocumentsFolder := reg.ReadString('Personal');
  finally
    reg.free;
  end;
end;
 
exports
 GetMyDocumentsFolder;
 
begin
 
end.

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ThievingSix
ThievingSix
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of slaterm1961
slaterm1961

ASKER

Hi,
With The first bit of code, i was able to get the project to compile (haveing added ShareMem)  as suggested in Comment #2 but when i call the function from the other unit which has declared the function, i get an access violation error in the dll.

I declare the function as follows:


//This bit of code is just below the Uses clause of the form.
 
function getMyDocumentsFolder():string;stdcall external 'test.dll';
 
//And i call the function like this
 
var
 MyDocuments: String;
begin
 MyDocuments := GetMyDocumentsFolder;
 ShowMessage(MyDocuments);

Open in new window

SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Thanks for the help, that last comment fixed it.

I hope i split the points fairly between the two of you who assisted me :)