dll的一段代码,程序调用第一次会出现地址错误,以后就不会有,经过多方测试发现 hisitemno,samplelist 两个tstringlist不能同时释放,只释放其中的一个就没有错误产生,不明是什么问题,请各位专家不吝赐教,谢谢!谢谢!代码如下:
function f_tubecount(jyxmh: pchar): pchar;stdcall;
var
hisitemno, samplelist: TStrings;
bloodtype: array[0..3] of integer;
jyxmhstr, btstr: string;
tempquery: tadoquery;
resultstr: string;
i, n: integer;
begin
result := '';
for i := 0 to 3 do
bloodtype[i] := 0;
tempquery := tadoquery.Create(nil);
tempquery.Connection := dmmain.ADOConMain;
hisitemno := tstringlist.Create;
samplelist := tstringlist.Create;
try
jyxmhstr := string(jyxmh); //pchar to string
i := pos('|', jyxmhstr);
n := 0;
while i > 0 do
begin
hisitemno.Add(copy(jyxmhstr, 1, i - 1));
delete(jyxmhstr, 1, i);
i := pos('|', jyxmhstr);
n := n + 1;
end;
for i := 0 to hisitemno.Count - 1 do
begin
OpenQuery(TempQuery, 'select * from testitem where orderno=''' + hisitemno.Strings[i] + '''');
if not tempquery.IsEmpty then
begin
hisitemno.Strings[i] := tempquery.fieldbyname('itemno').asstring;
SaveToLog('lis代码:'+hisitemno.Strings[i]);
end
else
application.MessageBox(PCHAR('!' + hisitemno.Strings[i]), '警告', MB_OK + MB_ICONINFORMATION);
end;
for i := 0 to hisitemno.Count - 1 do
begin
OpenQuery(TempQuery, 'select * from Samplingitem where itemno=' + hisitemno.Strings[i]);
if not tempquery.IsEmpty then
begin
if samplelist.IndexOf(tempquery.fieldbyname('SamplingGroupNo').AsString) < 0 then
samplelist.Add(tempquery.fieldbyname('SamplingGroupNo').AsString);
end;
end;
for i := 0 to samplelist.Count - 1 do
begin
OpenQuery(TempQuery, 'select * from Samplinggroup where samplinggroupno=' + samplelist.Strings[i]);
if not tempquery.IsEmpty then
btstr := tempquery.fieldbyname('bloodtype').asstring;
if trim(btstr) = '常规血液' then
bloodtype[0] := bloodtype[0] + 1;
if trim(btstr) = '生化血液' then
bloodtype[1] := bloodtype[1] + 1;
if trim(btstr) = '血凝血液' then
bloodtype[2] := bloodtype[2] + 1;
if trim(btstr) = '血沉血液' then
bloodtype[3] := bloodtype[3] + 1;
end;
tempquery.Close;
resultstr := inttostr(bloodtype[0]) + '|' + inttostr(bloodtype[1]) + '|' + inttostr(bloodtype[2]) + '|' + inttostr(bloodtype[3]);
result :=pchar(resultstr+CHAR(0));
finally
HISITEMNO.FREE;
SAMPLELIST.FREE;
tempquery.FREE;
end;
end;
function f_tubecount(jyxmh: pchar): pchar;stdcall;
var
hisitemno, samplelist: TStrings;
bloodtype: array[0..3] of integer;
jyxmhstr, btstr: string;
tempquery: tadoquery;
resultstr: string;
i, n: integer;
begin
result := '';
for i := 0 to 3 do
bloodtype[i] := 0;
tempquery := tadoquery.Create(nil);
tempquery.Connection := dmmain.ADOConMain;
hisitemno := tstringlist.Create;
samplelist := tstringlist.Create;
try
jyxmhstr := string(jyxmh); //pchar to string
i := pos('|', jyxmhstr);
n := 0;
while i > 0 do
begin
hisitemno.Add(copy(jyxmhstr, 1, i - 1));
delete(jyxmhstr, 1, i);
i := pos('|', jyxmhstr);
n := n + 1;
end;
for i := 0 to hisitemno.Count - 1 do
begin
OpenQuery(TempQuery, 'select * from testitem where orderno=''' + hisitemno.Strings[i] + '''');
if not tempquery.IsEmpty then
begin
hisitemno.Strings[i] := tempquery.fieldbyname('itemno').asstring;
SaveToLog('lis代码:'+hisitemno.Strings[i]);
end
else
application.MessageBox(PCHAR('!' + hisitemno.Strings[i]), '警告', MB_OK + MB_ICONINFORMATION);
end;
for i := 0 to hisitemno.Count - 1 do
begin
OpenQuery(TempQuery, 'select * from Samplingitem where itemno=' + hisitemno.Strings[i]);
if not tempquery.IsEmpty then
begin
if samplelist.IndexOf(tempquery.fieldbyname('SamplingGroupNo').AsString) < 0 then
samplelist.Add(tempquery.fieldbyname('SamplingGroupNo').AsString);
end;
end;
for i := 0 to samplelist.Count - 1 do
begin
OpenQuery(TempQuery, 'select * from Samplinggroup where samplinggroupno=' + samplelist.Strings[i]);
if not tempquery.IsEmpty then
btstr := tempquery.fieldbyname('bloodtype').asstring;
if trim(btstr) = '常规血液' then
bloodtype[0] := bloodtype[0] + 1;
if trim(btstr) = '生化血液' then
bloodtype[1] := bloodtype[1] + 1;
if trim(btstr) = '血凝血液' then
bloodtype[2] := bloodtype[2] + 1;
if trim(btstr) = '血沉血液' then
bloodtype[3] := bloodtype[3] + 1;
end;
tempquery.Close;
resultstr := inttostr(bloodtype[0]) + '|' + inttostr(bloodtype[1]) + '|' + inttostr(bloodtype[2]) + '|' + inttostr(bloodtype[3]);
result :=pchar(resultstr+CHAR(0));
finally
HISITEMNO.FREE;
SAMPLELIST.FREE;
tempquery.FREE;
end;
end;
function f_tubecount(jyxmh: pchar): pchar;stdcall;
var
hisitemno, samplelist: TStrings;
bloodtype: array[0..3] of integer;
jyxmhstr, btstr: string;
tempquery: tadoquery;
resultstr: string;
i, n: integer;
begin
result := '';
for i := 0 to 3 do
bloodtype[i] := 0;
tempquery := tadoquery.Create(nil);
tempquery.Connection := dmmain.ADOConMain;
hisitemno := tstringlist.Create;
samplelist := tstringlist.Create;
try
jyxmhstr := string(jyxmh); //pchar to string
i := pos(' ¦', jyxmhstr);
n := 0;
while i > 0 do
begin
hisitemno.Add(copy(jyxmhstr, 1, i - 1));
delete(jyxmhstr, 1, i);
i := pos(' ¦', jyxmhstr);
n := n + 1;
end;
for i := 0 to hisitemno.Count - 1 do
begin
OpenQuery(TempQuery, 'select * from testitem where orderno=''' + hisitemno.Strings[i] + '''');
if not tempquery.IsEmpty then
begin
hisitemno.Strings[i] := tempquery.fieldbyname('itemno').asstring;
SaveToLog('lis代码:'+hisitemno.Strings[i]);
end
else
application.MessageBox(PCHAR('!' + hisitemno.Strings[i]), '警告', MB_OK + MB_ICONINFORMATION);
end;
for i := 0 to hisitemno.Count - 1 do
begin
OpenQuery(TempQuery, 'select * from Samplingitem where itemno=' + hisitemno.Strings[i]);
if not tempquery.IsEmpty then
begin
if samplelist.IndexOf(tempquery.fieldbyname('SamplingGroupNo').AsString) < 0 then
samplelist.Add(tempquery.fieldbyname('SamplingGroupNo').AsString);
end;
end;
for i := 0 to samplelist.Count - 1 do
begin
OpenQuery(TempQuery, 'select * from Samplinggroup where samplinggroupno=' + samplelist.Strings[i]);
if not tempquery.IsEmpty then
btstr := tempquery.fieldbyname('bloodtype').asstring;
if trim(btstr) = '常规血液' then
bloodtype[0] := bloodtype[0] + 1;
if trim(btstr) = '生化血液' then
bloodtype[1] := bloodtype[1] + 1;
if trim(btstr) = '血凝血液' then
bloodtype[2] := bloodtype[2] + 1;
if trim(btstr) = '血沉血液' then
bloodtype[3] := bloodtype[3] + 1;
end;
tempquery.Close;
resultstr := inttostr(bloodtype[0]) + ' ¦' + inttostr(bloodtype[1]) + ' ¦' + inttostr(bloodtype[2]) + ' ¦' + inttostr(bloodtype[3]);
result :=pchar(resultstr+CHAR(0));
finally
HISITEMNO.FREE;
SAMPLELIST.FREE;
tempquery.FREE;
end;
end;
result := SysAllocString(resultstr+CHAR(0));
exe中使用f_tubecount用完字符串后也要用SysFreeString来释放
我看了下代码,去掉了 ado 相关的部分,代码执行没问题,所以我怀疑问题出在 ado 的操作上。楼主再耐心点跟一下看看。 有实际数据可能调试起来会相对简单些。