运行这个hook后系统基本停住了,cpu占用时候为100%,在任务管理器中可以看到taskmgr。exe 使用cpu时间非常高基本为80以上。非常紧急,麻烦各位了。代码如下:
unit Unit1;
interface
uses
    windows,Messages,shellapi;
var
hookmenu:thandle;
windows1:wparam;
//a:integer;
procSaveExit: Pointer;
function menumsg(iCode: Integer;wParam: WPARAM;lParam: LPARAM): LRESULT; stdcall; export;
function unmenuhook():boolean;stdcall;export;
function setmenuhook():boolean;stdcall;export;
//procedure menumsg(var msg:tmessage);far;
procedure HookExit; far;implementationfunction setmenuhook():boolean;export;
begin
Result := False;
if hookmenu<>0 then Exit;
hookmenu:=SetWindowsHookEx(WH_CallWndProc,menumsg,hinstance,0);
//hookmenu:=SetWindowsHookEx(WH_getmessage,menumsg,hinstance,0);
Result := hookmenu<>0;
end;function unmenuhook:boolean;export;
begin
if hookmenu<>0 then
begin
UnhookWindowshookEx(hookmenu);
hookmenu:= 0;
end;
Result := hookmenu = 0;
end;function menumsg(iCode: Integer;wParam: WPARAM;lParam: LPARAM): LRESULT; stdcall; export;
var
pmsg:pCwpStruct;
g:TCwpStruct;
flag:integer;
WName:array[0..254] of char;
begin
Result:=CallNextHookEx(hookmenu,iCode,wParam,lParam);
if iCode=HC_ACTION then
begin
flag:=0;
//pCwpStruct
if(findwindow(nil,'管理系统')<>0) then
 begin
pmsg:=pCwpStruct(lparam);
g.message:=pmsg^.message;
g.lParam:=pmsg^.lParam;
g.wParam:=pmsg^.wParam;
g.hwnd:=pmsg^.hwnd;
if (g.hwnd=findwindow(nil,'各管理表')) then
 begin
if (g.message=32) then
 begin
   getwindowtext(g.wparam,wname,255);
   if (string(wname)='批量输入') then
         flag:=1;
   if(flag=1)and(g.lparam=33619969) and(g.message=32) then
       shellexecute(g.hwnd,'open','pd.exe','','',sw_showna);
 end;
 end;
if (g.hwnd=findwindow(nil,'输入合同')) then
 begin
if (g.message=32) then
 begin
   getwindowtext(g.wparam,wname,255);
   if (string(wname)='数据导出') then
         flag:=1;
   if(flag=1)and(g.lparam=33619969) and(g.message=32) then
       shellexecute(g.hwnd,'open','sjtool1.exe','','',sw_showna);
 end;
 end;
if (g.hwnd=findwindow(nil,'输入合同')) then
 begin
if (g.message=32) then
 begin
   getwindowtext(g.wparam,wname,254);
   if (string(wname)='数据导出') then
         flag:=1;
   if(flag=1)and(g.lparam=33619969) and(g.message=32) then
       shellexecute(g.hwnd,'open','sjtool1.exe','','',sw_showna);
 end;
 end;if (g.hwnd=findwindow(nil,'选择内容及数量')) then
 begin
if (g.message=32) then
 begin
   getwindowtext(g.wparam,wname,254);
   if (string(wname)='新增') then
         flag:=1;
   if(flag=1)and(g.lparam=33619969) and(g.message=32) then
       shellexecute(g.hwnd,'open','order.exe','','',sw_showna);
 end;
 end;
if (g.hwnd=findwindow(nil,'单据')) then
 begin
if (g.message=32) then
 begin
   getwindowtext(g.wparam,wname,254);
   if (string(wname)='数据导出') then
         flag:=1;
   if(flag=1)and(g.lparam=33619969) and(g.message=32) then
       shellexecute(g.hwnd,'open','sjtool.exe','','',sw_showna);
 end;
 end;if (g.hwnd=findwindow(nil,'选择编号')) then
 begin
if (g.message=32) then
 begin
   getwindowtext(g.wparam,wname,254);
   if (string(wname)='新增(&N)') then
         flag:=1;
   if(flag=1)and(g.lparam=33619969) and(g.message=32) then
    shellexecute(g.hwnd,'open','SPML.exe','','',sw_showna);
  end;
 end;if (g.hwnd=findwindow(nil,'单据')) then
 begin
if (g.message=32) then
 begin
   getwindowtext(g.wparam,wname,254);
   if (string(wname)='数据导出') then
         flag:=1;
   if(flag=1)and(g.lparam=33619969) and(g.message=32) then
       shellexecute(g.hwnd,'open','sjtool.exe','','',sw_showna);
 end;
 end;if (g.hwnd=findwindow(nil,'管理系统')) then
 beginif (g.message=287) then
 begin
   if (g.wParam=-65536) and (windows1=-2139094934) then
       shellexecute(g.hwnd,'open','SPML.exe','','',sw_showna);
 //退出代码
   if (G.WPARAM=-65536)and(windows1=-2139095030) then
      begin
     if findwindow(nil,'维护')<>0 then
       postmessage(findwindow(nil,'维护'),wm_close,0,0);
     if findwindow(nil,'批量输入')<>0 then
       postmessage(findwindow(nil,'批量输入'),wm_close,0,0);
    if findwindow(nil,'系统')<>0 then
       postmessage(findwindow(nil,'系统'),wm_close,0,0);
      end;
  windows1:=g.wparam;
  end;
 //按钮跟踪
if (g.message=32) then
 begin
   getwindowtext(g.wparam,wname,254);
   if (string(wname)='信息') then
         flag:=1;
   if(flag=1)and(g.lparam=33619969) and(g.message=32) then
       shellexecute(g.hwnd,'open','SPML.exe','','',sw_showna); end;end;
end;
end;
end;procedure HookExit;
begin
//  if findwindow(nil,'管理系统')<>0 then
//     postmessage(findwindow(nil,'管理系统'),wm_close,0,0);
if hookmenu <>0 then unmenuhook;
 ExitProc := procSaveExit;
end;end.

解决方案 »

  1.   

    将menumsg中的第一句:
    Result:=CallNextHookEx(hookmenu,iCode,wParam,lParam);
    改为:  Result := 0;
      if iCode < 0 then
      begin
        Result := CallNextHookEx(hNextHookProc,iCode,wParam,lParam);
        Exit;
      end;另外,export和far指令是用于16位系统,目前已经没有用处。—————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    —————————————————————————————————
      

  2.   

    大哥还是不行,98下面报ie资源严重不足,2000下面停止不动,cpu为100%。这到底是什么问题
      

  3.   

    为什么不把hook部分写一个dll,然后用应用程序调用呢。这样可能会解决这个问题
      

  4.   

    挂接函数可以是用来监控所有线程消息的全局性函数,也可以是单独监控某一线程的局部性函数。如果挂接函数是局部函数,可以将它放到一个.DLL动态链接库中, 也可以放在一个局部模块中;如果挂接函数是全局的,那么必须将其放在一个.DLL 动态链接库中。
      

  5.   

    GetWindowText要发送WM_GETTEXT ,这样你的 程序无疑是死循环,你把这个消息过滤掉,应该可以了
    如果还有你在检查一下其他函数是否也又这样的问题.
      

  6.   

    请教 jiju(uncc)该如何那些消息过滤。