DLL里面利用消息 创建窗体的部分代码Function GetMsgProc(icode: Integer;wp: WPARAM;lp: LPARAM):LRESULT;stdcall;
begin
if (icode=HC_ACTION) then
begin
if (PMSG(lp)^.message=WM_USER+600) and ((1 shl 31)and lp=0) then //MessageBox(0,'显示','显示',0);
begin
if form1=nil then------------这里会多次创建窗体 Form1:=Tform1.Create(nil); //应该是在这里修改 但是具体不知道怎么修改
form1.Visible:=not form1.Visible; //希望有高人指点下这个是什么意思
end;
end;
Result:=CallNextHookEx(keyhhk,icode,wp,lp);
end;
Function SetHook(hwid:Integer):boolean;stdcall;//这个函数是导出表函数,接受传递的参数
var
GameTid:THandle;
h:HWND;
begin
Result:=false;
h:=hwid;
if (windows.IsWindow(h)=false) then
begin
Messagebox(0,'无效窗口句柄','error',0);
exit;
end;
GameTid:=GetWindowThreadProcessId(h);
if GameTid=0 then
begin
Messagebox(0,'无法获得线程ID','error',0);
exit;
end;
if GameTid<>0 then
begin
keyhhk:=SetWindowsHookEx(WH_GETMESSAGE,@GetMsgProc,GetModuleHandle('dllGame.dll'),GameTid);
end; if keyhhk>0 then
Result:=true;
end;procedure DllEnterProc(reason:integer);
begin
case reason of
windows.DLL_PROCESS_ATTACH: begin end;
windows.DLL_PROCESS_DETACH: begin Form1.Free;form1:=nil; end;
end;
end;利用自定义消息创建窗体
exe发送消息 请求窗体出现 if SrvIP.Selected <> nil then
begin
nowpid:=StrToCard(SrvIP.Selected.Caption); end;
nowwinid:=GetHWndByPID(nowpid);
SwitchToThisWindow(nowwinid,true);
sleep(50);
if nowwinid<>0 then
begin PostMessage(nowwinid,WM_USER+600,0,0);//呼出或者隐藏窗体
end;因为DLL窗体里面用了泛型list
窗体创建的时候动态创建了list
窗体销毁的时候 又要清理list
但是因为代码不严谨 窗体其实是多次被创建了
所以如果销毁的时候释放list会导致程序的某些功能出现问题 因为程序还需要继续使用那些list
如果不释放的话会导致每次创建窗体都重新创建list导致 内存严整的泄露.
呼出窗体和 销毁窗体次数越多导致内存泄露的也越多.以下是窗体创建时 部分的代码
procedure TForm1.FormCreate(Sender: TObject);
begin
Set8087CW(MCW_EM); //设置浮点协处理不开启,不要删除 否则导致内存泄露和 浮点除零错误
yxtid:=GetCurrentProcessId;
ptlist:=TList<Tpetlist>.Create;
nptlist:=TList<Tpetlist>.Create;
zbList:=TList<Tzblist>.Create;
jnlist:=TList<Tjn>.Create;
jnlisttwo:=TList<Tjn>.Create;
dqhtlist:=TList<Thate>.Create;
end;procedure TForm1.FormDestroy(Sender: TObject);//这些数据需要一直要被使用,直到DLL彻底退出才被销毁begin
ptlist.free;
nptlist.free;
zbList.free;
jnlist.free;
jnlisttwo.free;
dqhtlist.free;
end;
归纳下
就是我希望DLL只有在彻底退出的时候才进行销毁操作,在接受到自定义消息的时候只是进行隐藏窗体和呼出窗体的操作.隐藏窗体的时候可以再次被呼出 并且在隐藏窗体最好不要出现在任务管理器中.
分分不多 ,求大家多指教.内存泄露DelphiDLL窗体SetWindowsHookEx
begin
if (icode=HC_ACTION) then
begin
if (PMSG(lp)^.message=WM_USER+600) and ((1 shl 31)and lp=0) then //MessageBox(0,'显示','显示',0);
begin
if form1=nil then------------这里会多次创建窗体 Form1:=Tform1.Create(nil); //应该是在这里修改 但是具体不知道怎么修改
form1.Visible:=not form1.Visible; //希望有高人指点下这个是什么意思
end;
end;
Result:=CallNextHookEx(keyhhk,icode,wp,lp);
end;
Function SetHook(hwid:Integer):boolean;stdcall;//这个函数是导出表函数,接受传递的参数
var
GameTid:THandle;
h:HWND;
begin
Result:=false;
h:=hwid;
if (windows.IsWindow(h)=false) then
begin
Messagebox(0,'无效窗口句柄','error',0);
exit;
end;
GameTid:=GetWindowThreadProcessId(h);
if GameTid=0 then
begin
Messagebox(0,'无法获得线程ID','error',0);
exit;
end;
if GameTid<>0 then
begin
keyhhk:=SetWindowsHookEx(WH_GETMESSAGE,@GetMsgProc,GetModuleHandle('dllGame.dll'),GameTid);
end; if keyhhk>0 then
Result:=true;
end;procedure DllEnterProc(reason:integer);
begin
case reason of
windows.DLL_PROCESS_ATTACH: begin end;
windows.DLL_PROCESS_DETACH: begin Form1.Free;form1:=nil; end;
end;
end;利用自定义消息创建窗体
exe发送消息 请求窗体出现 if SrvIP.Selected <> nil then
begin
nowpid:=StrToCard(SrvIP.Selected.Caption); end;
nowwinid:=GetHWndByPID(nowpid);
SwitchToThisWindow(nowwinid,true);
sleep(50);
if nowwinid<>0 then
begin PostMessage(nowwinid,WM_USER+600,0,0);//呼出或者隐藏窗体
end;因为DLL窗体里面用了泛型list
窗体创建的时候动态创建了list
窗体销毁的时候 又要清理list
但是因为代码不严谨 窗体其实是多次被创建了
所以如果销毁的时候释放list会导致程序的某些功能出现问题 因为程序还需要继续使用那些list
如果不释放的话会导致每次创建窗体都重新创建list导致 内存严整的泄露.
呼出窗体和 销毁窗体次数越多导致内存泄露的也越多.以下是窗体创建时 部分的代码
procedure TForm1.FormCreate(Sender: TObject);
begin
Set8087CW(MCW_EM); //设置浮点协处理不开启,不要删除 否则导致内存泄露和 浮点除零错误
yxtid:=GetCurrentProcessId;
ptlist:=TList<Tpetlist>.Create;
nptlist:=TList<Tpetlist>.Create;
zbList:=TList<Tzblist>.Create;
jnlist:=TList<Tjn>.Create;
jnlisttwo:=TList<Tjn>.Create;
dqhtlist:=TList<Thate>.Create;
end;procedure TForm1.FormDestroy(Sender: TObject);//这些数据需要一直要被使用,直到DLL彻底退出才被销毁begin
ptlist.free;
nptlist.free;
zbList.free;
jnlist.free;
jnlisttwo.free;
dqhtlist.free;
end;
归纳下
就是我希望DLL只有在彻底退出的时候才进行销毁操作,在接受到自定义消息的时候只是进行隐藏窗体和呼出窗体的操作.隐藏窗体的时候可以再次被呼出 并且在隐藏窗体最好不要出现在任务管理器中.
分分不多 ,求大家多指教.内存泄露DelphiDLL窗体SetWindowsHookEx
Form1:=Tform1.Create(nil); // 就攒创建这个form1
//如果form1显示中,那就隐藏它, 如果form1隐藏中,那就显示它
//这样就不必创建多个form1,已创建的form1不用的时候隐藏起来而不是销毁它
form1.Visible:=not form1.Visible;
应该可以解决,lz 可以 google一下 “delphi 单件”。
----------------------------专业鲁过兄弟 我应该怎么做才可以解决我现在的问题希望DLL只有在彻底退出的时候才进行销毁操作,在接受到自定义消息的时候只是进行隐藏窗体和呼出窗体的操作.隐藏窗体的时候可以再次被呼出 并且在隐藏窗体最好不要出现在任务管理器中.
Form1:=Tform1.Create(nil); // 就攒创建这个form1
//如果form1显示中,那就隐藏它, 如果form1隐藏中,那就显示它
//这样就不必创建多个form1,已创建的form1不用的时候隐藏起来而不是销毁它
form1.Visible:=not form1.Visible;
我想知道 这段代码执行的时候 procedure TForm1.FormCreate(Sender: TObject); 会不会被执行到?