我在DLL里面的函数中分配了内存,在得到返回值时,再释放内存,但是在调用的时候会出错,如分配内存,不释放内存的话就不会出错,请问怎么解决?在DELPHI,PB中调用都出错?为何?
如下代码:
function GetWbPy(hzstr: pchar; pytype: integer):pchar;Stdcall;export;
var
I: Integer;
allstr: shortstring;
hh: THandle;
pp: pointer;
ss: TStringList;
PTemp: PChar;
begin
{代码}
PTemp := AllocMem(length(allstr));
StrCopy(PTemp,pchar(Trim(allstr)));
Result := PTemp;
FreeMemory(PTemp);
end;
如下代码:
function GetWbPy(hzstr: pchar; pytype: integer):pchar;Stdcall;export;
var
I: Integer;
allstr: shortstring;
hh: THandle;
pp: pointer;
ss: TStringList;
PTemp: PChar;
begin
{代码}
PTemp := AllocMem(length(allstr));
StrCopy(PTemp,pchar(Trim(allstr)));
Result := PTemp;
FreeMemory(PTemp);
end;
方案1:使用全局变量
var
Temp: string;function GetWbPy(hzstr: pchar; pytype: integer):pchar;Stdcall;export;
begin
Temp := { 赋值 };
Result := PChar(Temp);
end;方案2:由app分配空间function GetWbPy(
hzstr: PChar;
pytype: integer;
return: PChar // 由app分配空间
): PChar; stdcall; export;
begin
end;app中
var
vBuffer: array[0..20] of Char;
begin
GetWbPy(?, ?, vBuffer);
end;
用全局变量怎么分配内存,不分配会出错的啊
方案2:由app分配空间
能具体说一下吗?我的程序如下啊
function GetWbPy(hzstr: pchar; pytype: integer):pchar;Stdcall;export;
var
I: Integer;
allstr: shortstring;
hh: THandle;
pp: pointer;
ss: TStringList;
PTemp: PChar;
function retturn_wbpy(tempstr: shortstring; tqtype: integer): shortstring;
var
outstr, str: ShortString;
i: integer;
begin
//################### 汉字查询电位
i := 0;
while i <= ss.Count - 1 do
begin
str := ss.Strings[i];
if (tempstr = trim(str[1] + str[2])) or (tempstr = trim(str[3] + str[4])) then
begin
str := ss.Strings[i];
Break;
end;
i := i + 1;
end;
//################### outstr := ''; //提取编码
if tqtype = 1 then
begin
for i := pos('①', str) + 2 to pos('②', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 2 then
begin
for i := pos('②', str) + 2 to pos('③', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end; if tqtype = 3 then
begin
for i := pos('③', str) + 2 to pos('④', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end; if tqtype = 4 then
begin
for i := pos('④', str) + 2 to length(str) do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
Result := trim(outstr);
end;
begin
//加载资源文件,将内容赋值给 s
ss := TStringList.Create;
hh := FindResource(hInstance, 'mywb', 'TXT');
hh := LoadResource(hInstance, hh);
pp := LockResource(hh);
ss.Text := pchar(pp);
UnLockResource(hh);
FreeResource(hh);
allstr := '';
i := 0;
while i <= length(hzstr) do //提取汉字字符
begin
if (Ord(hzstr[I]) > 127) then
begin
if allstr = '' then allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype) else allstr := allstr + retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype);
i := i + 2;
end
else
begin
if allstr = '' then allstr := hzstr[I] else allstr := allstr + hzstr[I];
i := i + 1;
end;
end;
ss.Free;
PTemp := AllocMem(length(allstr));
StrCopy(PTemp,pchar(Trim(allstr)));
Result := PTemp;
FreeMemory(PTemp);
end;
exports
GetWbPy;
end.
Temp: string;function GetWbPy(hzstr: PChar; pytype: Integer): PChar; stdcall;export;
var
I: Integer;
allstr: shortstring;
hh: THandle;
pp: pointer;
ss: TStringList;
function retturn_wbpy(tempstr: shortstring; tqtype: integer): shortstring;
var
outstr, str: ShortString;
i: integer;
begin
//################### 汉字查询电位
i := 0;
while i <= ss.Count - 1 do
begin
str := ss.Strings[i];
if (tempstr = trim(str[1] + str[2])) or (tempstr = trim(str[3] + str[4])) then
begin
str := ss.Strings[i];
Break;
end;
i := i + 1;
end;
//################### outstr := ''; //提取编码
if tqtype = 1 then
begin
for i := pos('①', str) + 2 to pos('②', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 2 then
begin
for i := pos('②', str) + 2 to pos('③', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end; if tqtype = 3 then
begin
for i := pos('③', str) + 2 to pos('④', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end; if tqtype = 4 then
begin
for i := pos('④', str) + 2 to length(str) do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
Result := trim(outstr);
end;
begin
//加载资源文件,将内容赋值给 s
ss := TStringList.Create;
hh := FindResource(hInstance, 'mywb', 'TXT');
hh := LoadResource(hInstance, hh);
pp := LockResource(hh);
ss.Text := pchar(pp);
UnLockResource(hh);
FreeResource(hh);
allstr := '';
i := 0;
while i <= length(hzstr) do //提取汉字字符
begin
if (Ord(hzstr[I]) > 127) then
begin
if allstr = '' then
allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype)
else allstr := allstr + retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype);
i := i + 2;
end
else
begin
if allstr = '' then allstr := hzstr[I] else allstr := allstr + hzstr[I];
i := i + 1;
end;
end;
ss.Free;
allstr := hzstr + 'fffffffffffffffffffff';
Temp := allstr;
Result := PChar(Temp); // change
end;
var
I: Integer;
allstr: ShortString;
hh: THandle;
pp: Pointer;
ss: TStringList;
function retturn_wbpy(tempstr: shortstring; tqtype: integer): shortstring;
var
outstr, str: ShortString;
i: integer;
begin
//################### 汉字查询电位
i := 0;
while i <= ss.Count - 1 do
begin
str := ss.Strings[i];
if (tempstr = trim(str[1] + str[2])) or (tempstr = trim(str[3] + str[4])) then
begin
str := ss.Strings[i];
Break;
end;
i := i + 1;
end;
//################### outstr := ''; //提取编码
if tqtype = 1 then
begin
for i := pos('①', str) + 2 to pos('②', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
if tqtype = 2 then
begin
for i := pos('②', str) + 2 to pos('③', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end; if tqtype = 3 then
begin
for i := pos('③', str) + 2 to pos('④', str) - 1 do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end; if tqtype = 4 then
begin
for i := pos('④', str) + 2 to length(str) do
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i];
end;
Result := trim(outstr);
end;
begin
//加载资源文件,将内容赋值给 s
ss := TStringList.Create;
hh := FindResource(hInstance, 'mywb', 'TXT');
hh := LoadResource(hInstance, hh);
pp := LockResource(hh);
ss.Text := pchar(pp);
UnLockResource(hh);
FreeResource(hh);
allstr := '';
i := 0;
while i <= length(hzstr) do //提取汉字字符
begin
if (Ord(hzstr[I]) > 127) then
begin
if allstr = '' then
allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype)
else allstr := allstr + retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype);
i := i + 2;
end
else
begin
if allstr = '' then allstr := hzstr[I] else allstr := allstr + hzstr[I];
i := i + 1;
end;
end;
ss.Free;
allstr := hzstr + 'fffffffffffffffffffff';
Result := Length(allstr);
return[Result] := #0;
if Result > 0 then Move(allstr[1], return^, Result);
end;app中
procedure TForm1.Button1Click(Sender: TObject);
var
vBuffer: array[0..256] of Char;
begin
if GetWbPy('汉字', 1, vBuffer) > 0 then
Caption := vBuffer;
end;