我在调试获得控制面板中的电源管理信息(powercfg.cpl)时,出现当程序运行到CPlproc(handle,CPL_NEWINQUIRE,i,longint(@newCplinfo));时,程序中所传入的TreeNode被提前释放了,引起内存访问出错,这只是在加载powercfg.cpl时会出现,其它cpl文件不会
源码如下所示:
uses cpl;
procedure processNode(ANode:TTreeNode);
var
Hlib:Thandle;
CplProc:TCplapplet;
ICount,i:integer;
NewNode:TTreeNode;
cplinfo:TCplinfo;
newCplinfo:TNewCplinfo;
sName,sInfo:array[0..255] of char;
hAppIcon:hIcon;
begin
hlib:=loadlibrary(Pchar(Anode.text));
if hlib=0 then
begin
anode.text:=anode.text+'Can't be loaded';
exit;
end;
try
CPlproc:=TCPlapplet(getprocaddress(hlib,'cplapplet'));
if @cplproc=nil then exit;
if Cplproc(handle,cpl_init,0,0)=0 then exit;//handle 为窗口的handel
icount:=cplproc(handle,CPL_GETCOUNT,0,0);
for i:=0 to iCount-1 do
begin
fillchar(cplinfo,sizeof(TCPLInfo),0);
cplproc(handle,CPL_INQUIRE,i,longInt(@cplinfo));
loadstring(hlib,cplinfo,idname,@sName,255);
loadstring(hLib,cplinfo.idinfo,@sinfo,255);
happicon:=loadIcon(hLib,MAKEINTRESOURCE(cplInfo.idIcon));
if (cplInfo.idName=CPL_DYNAMIC_RES) or(cplinfo.idinfo=CPL_DYNAMIC_RES) or (cplinfo.idIcon=CPL_DYNAMIC_RES) then
begin
fillchar(newcplinfo,sizeof(TNewcplinfo),0);
CPlproc(handle,CPL_NEWINQUIRE,i,longint(@newCplinfo));
//取得组件名
if cplinfo.idname=CPL_DYNAMIC_RES then
strcopy(sName,newCPLinfo.szname);
if cplinfo.idinfo=CPL_DYNAMIC_RES then
strcopy(sinfo,newcplinfo.szinfo);
if cplinfo.idicon=CPL_DYNAMIC_RES then
happicon:=newcplinfo.hicon;
end;
end;
end;
源码如下所示:
uses cpl;
procedure processNode(ANode:TTreeNode);
var
Hlib:Thandle;
CplProc:TCplapplet;
ICount,i:integer;
NewNode:TTreeNode;
cplinfo:TCplinfo;
newCplinfo:TNewCplinfo;
sName,sInfo:array[0..255] of char;
hAppIcon:hIcon;
begin
hlib:=loadlibrary(Pchar(Anode.text));
if hlib=0 then
begin
anode.text:=anode.text+'Can't be loaded';
exit;
end;
try
CPlproc:=TCPlapplet(getprocaddress(hlib,'cplapplet'));
if @cplproc=nil then exit;
if Cplproc(handle,cpl_init,0,0)=0 then exit;//handle 为窗口的handel
icount:=cplproc(handle,CPL_GETCOUNT,0,0);
for i:=0 to iCount-1 do
begin
fillchar(cplinfo,sizeof(TCPLInfo),0);
cplproc(handle,CPL_INQUIRE,i,longInt(@cplinfo));
loadstring(hlib,cplinfo,idname,@sName,255);
loadstring(hLib,cplinfo.idinfo,@sinfo,255);
happicon:=loadIcon(hLib,MAKEINTRESOURCE(cplInfo.idIcon));
if (cplInfo.idName=CPL_DYNAMIC_RES) or(cplinfo.idinfo=CPL_DYNAMIC_RES) or (cplinfo.idIcon=CPL_DYNAMIC_RES) then
begin
fillchar(newcplinfo,sizeof(TNewcplinfo),0);
CPlproc(handle,CPL_NEWINQUIRE,i,longint(@newCplinfo));
//取得组件名
if cplinfo.idname=CPL_DYNAMIC_RES then
strcopy(sName,newCPLinfo.szname);
if cplinfo.idinfo=CPL_DYNAMIC_RES then
strcopy(sinfo,newcplinfo.szinfo);
if cplinfo.idicon=CPL_DYNAMIC_RES then
happicon:=newcplinfo.hicon;
end;
end;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货