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 ?
'
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.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks for the help, that last comment fixed it.
I hope i split the points fairly between the two of you who assisted me :)
I hope i split the points fairly between the two of you who assisted me :)
ASKER
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:
Open in new window