主题:请高手帮看一下,下面代码错在哪里?
反复调用函数GetTPData,偶尔会弹出错误:Project Project1.exe raised exception class
EAccessViolation with message 'Access violation at address 00C3099A. Read of address
FFFFFFFF'. Process stopped. Use Step or Run to continue. 到底是什么原因呢?
function TfrmMain.getBitStr(aHandle:HWND;ileft,iTop,iWidth,iHeight:integer;IsYDData:boolean):TStringList;
var
FullscreenCanvas:TCanvas;
dc:HDC;
i,j,h:integer;
c:TColor;
Str,strline:string;
begin
Result:=TstringList.Create ;
dc:= GetDC (aHandle);
FullscreenCanvas := TCanvas.Create;
FullscreenCanvas.Handle := dc;
if IsYDData then
c:=clLime
else c:=fullscreenCanvas.Pixels[1,1];
Str:=' ';
for i :=1 to iHeight do
begin //i
strline:='';
for j :=1 to iWidth do
begin //j
if c<>FullscreenCanvas.Pixels[iLeft+j,iTop +i] then
begin //if
c:=FullscreenCanvas.Pixels[iLeft+j,iTop +i];
if str=' ' then
str:='1'
else
str:=' ';
end; //if
strLine:=strLine+str;
end; //j
Result.Add(strLine);
end; //i
//ReleaseDC(aHandle,dc); 此句用不用都不影响弹出错误提示
//ReleaseDC(aHandle,FullscreenCanvas.Handle); 此句用不用都不影响弹出错误提示
FullscreenCanvas.Free;
end;
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function GetTPData(AHwnd:HWND;T:integer):boolean;
var
i,j,m,n:integer;
strbit:TStringList;
TempTP:string;
Ok:boolean;
begin
m:=455;
Ok:=False;
for i:=0 to 3 do
begin //i
for j:=0 to 3 do
begin //j
strbit:=frmMain.getBitStr(AHwnd,m+j,T+i,34,16,False);
TempTP:='';
if Trim(strbit.Strings[0])<>'' then
begin //if
for n:=0 to strbit.Count-1 do
begin
TempTP:=TempTP+strbit.Strings[n];
end;
Ok:=(TempTP=frmMain.TPdat.Strings[10]);
end; //if
strbit.Free;
if Ok then break;
end; //j
if Ok then break;
end; //i
Result:=Ok;
end;
EAccessViolation with message 'Access violation at address 00C3099A. Read of address
FFFFFFFF'. Process stopped. Use Step or Run to continue. 到底是什么原因呢?
function TfrmMain.getBitStr(aHandle:HWND;ileft,iTop,iWidth,iHeight:integer;IsYDData:boolean):TStringList;
var
FullscreenCanvas:TCanvas;
dc:HDC;
i,j,h:integer;
c:TColor;
Str,strline:string;
begin
Result:=TstringList.Create ;
dc:= GetDC (aHandle);
FullscreenCanvas := TCanvas.Create;
FullscreenCanvas.Handle := dc;
if IsYDData then
c:=clLime
else c:=fullscreenCanvas.Pixels[1,1];
Str:=' ';
for i :=1 to iHeight do
begin //i
strline:='';
for j :=1 to iWidth do
begin //j
if c<>FullscreenCanvas.Pixels[iLeft+j,iTop +i] then
begin //if
c:=FullscreenCanvas.Pixels[iLeft+j,iTop +i];
if str=' ' then
str:='1'
else
str:=' ';
end; //if
strLine:=strLine+str;
end; //j
Result.Add(strLine);
end; //i
//ReleaseDC(aHandle,dc); 此句用不用都不影响弹出错误提示
//ReleaseDC(aHandle,FullscreenCanvas.Handle); 此句用不用都不影响弹出错误提示
FullscreenCanvas.Free;
end;
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function GetTPData(AHwnd:HWND;T:integer):boolean;
var
i,j,m,n:integer;
strbit:TStringList;
TempTP:string;
Ok:boolean;
begin
m:=455;
Ok:=False;
for i:=0 to 3 do
begin //i
for j:=0 to 3 do
begin //j
strbit:=frmMain.getBitStr(AHwnd,m+j,T+i,34,16,False);
TempTP:='';
if Trim(strbit.Strings[0])<>'' then
begin //if
for n:=0 to strbit.Count-1 do
begin
TempTP:=TempTP+strbit.Strings[n];
end;
Ok:=(TempTP=frmMain.TPdat.Strings[10]);
end; //if
strbit.Free;
if Ok then break;
end; //j
if Ok then break;
end; //i
Result:=Ok;
end;