16,748
社区成员
发帖
与我相关
我的任务
分享
//注:需要在DLL函数的参数中加上一个变参,用来表示mystr被DLL函数加工后的长度,以便通知主调函数释放内存的长度,我这里为省事,就不写了,这部分内容僵哥曾经仔细讲过,可查询一下以前的贴子
DLL部分
function test(mystr:PChar):boolean;stdcall;
var
str:string;
begin
result:=False;
str:='ccccccc';
CopyMemory(mystr,PChar(str),Length(str));
result:=true;
end;
主调:
var
mystr:PChar;
begin
GetMem(mystr,100 );
test(mystr);
showmessage(mystr);
FreeMem(mystr);
end;
用PChar
原因见楼上!
DLL部分
type
PStaff = ^TStaff;
TStaff = record
data1:integer;
data2:integer;
end;
function recivedata( MyTest:PStaff):boolean;stdcall;
begin
Result:=True;
try
//操作
MyTest^.data1:=20;
MyTest^.data2:=30;
except
Result:=False;
end;
end;
exports
recivedata ;
//主调程序
type
PStaff = ^TStaff;
TStaff = record
data1:integer;
data2:integer;
end;
TGetRecieveData=function(MyTest:PStaff):boolean;stdcall;
function TForm1.GetData: boolean;
var
DllHnd: THandle;
GetRDProc: TGetRecieveData;
Stf:PStaff;
begin
DllHnd := LoadLibrary(PChar('project1.dll'));
try
if (DllHnd <> 0) then
begin
@GetRDProc :=GetProcAddress(DllHnd, 'recivedata');
if (@GetRDProc<>nil) then
begin
New(Stf);
try
if not GetRDProc(Stf) then ShowMessage('获取值错误');
//Showmessage('result='+Inttostr(Stf^.data1));
finally
Dispose(Stf);
end;
end;
end
else
begin
Application.MessageBox(PChar('DLL加载出错,DLL可能不存在!'), PChar('错误'),
MB_ICONWARNING or MB_OK);
end;
finally
FreeLibrary(DllHnd);
end;
end;