Question : Passing PChar parm to DLL

I have written a DLL with a function that takes a PChar parameter and returns a boolean. When I call the function from a test app it always comes up with an AV. I have now reduced it to its most basic as below but still get AV.

function myDLLFunction(szText: PChar): Boolean; stdcall;
begin
   Result := True;
end;

My test app calls it like this:

procedure TForm1.Button1Click(Sender: TObject);
var
  szText: PChar;
begin
  szText:= 'Some text';
  if myDLLFunction(szText) then
    showmessage('OK');
end;

I've also replaced the calling PChar with array of Char but same result. I'm new to DLLs and PChar's. Am I doing something stupid?

Answer : Passing PChar parm to DLL

Are you declaring it properly?
This works for me.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
Project1, Unit1:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function myDLLFunction(szText: PChar): Boolean; stdcall; external 'project2.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
  szText: PChar;
begin
  szText:= 'Some text';
  if myDLLFunction(szText) then
    showmessage('OK');
end;

end.

=======================

Project 2, single dpr:

library Project2;

{ 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;

{$R *.res}

function myDLLFunction(szText: PChar): Boolean; stdcall;
begin
   Result := True;
end;

exports
  myDLLFunction;

begin

end.
Random Solutions  
 
programming4us programming4us