the other solution is to just use PChar:
1)************************
library thenewdll;
{ 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
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
StdCtrls,
shellapi,
shlobj,
CommCtrl,
comobj;
{$R *.res}
function gettotalmem():PChar;
var
GlobalMemoryInfo: TMemoryStatus; // holds the global memory status information
begin
{set the size of the structure before the call}
GlobalMemoryInfo.dwLength := SizeOf(GlobalMemoryInfo);
{retrieve the global memory status...}
GlobalMemoryStatus(GlobalM
{and display the information}
gettotalmem := PChar(IntToStr(GlobalMemor
end;
exports gettotalmem;
end.
**************************
2)*******
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure gettotalmem; external 'thenewdll.dll'
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender
var
x:string;
begin
x:=gettotalmem;
showmessage(x);
end;
----------------
function gettotalmem:pchar; external 'thenewdll.dll'
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender
var
x:pchar;
begin
x:=gettotalmem;
showmessage(x);
end;
end.
-----------------
also consider that in your example you use " procedure gettotalmem; external 'thenewdll.dll'" but you export a function
Main Topics
Browse All Topics





by: RadikalQ3Posted on 2005-09-20 at 07:17:38ID: 14920343
Hi!
rm1, Form1);
emoryInfo) ;
dwTotalPhy s);
: TObject);
rm1, Form1);
In your project, you must add ShareMem (as first entry) in your project uses line.
Project->View source and add ShareMem in the uses:
program Project1;
uses
ShareMem,
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TFo
Application.Run;
end.
This is because you are using strings in a exported function of a DLL.
Ah!, the DLL declaration is wrong in your example, is a function, not a procedure, and the uses line of the DLL has innecesary parts.
Here is my sources:
The DLL:
library thenewdll;
{ 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
ShareMem,
Windows,
SysUtils;
{$R *.res}
function gettotalmem():string;
var
GlobalMemoryInfo: TMemoryStatus; // holds the global memory status information
begin
{set the size of the structure before the call}
GlobalMemoryInfo.dwLength := SizeOf(GlobalMemoryInfo);
{retrieve the global memory status...}
GlobalMemoryStatus(GlobalM
{and display the information}
gettotalmem := IntToStr(GlobalMemoryInfo.
end;
exports gettotalmem;
end.
The caller project:
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;
function gettotalmem: string; external 'thenewdll.dll'
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender
var
x: string;
begin
x:=gettotalmem;
showmessage(x);
end;
end.
The project source:
program Project1;
uses
ShareMem,
Forms,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TFo
Application.Run;
end.