您的位置:程序门 -> delphi -> windows sdk/api



dll传参问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


dll传参问题[已结贴,结贴人:c2one]
发表于:2008-01-10 09:15:18 楼主
我向我的dll程序里面传进一个tform类型的参数,
如果在钩子回调里面使用就会让钩子失效掉,这是为什么?应该怎么改?
代码如下:

delphi(pascal) code
unit qqtitlehook; interface uses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, commctrl, strutils; var hkqqchat: hhook; hwqqchat: hwnd ; kid: integer; hwqqchatchild: hwnd; hwqqchatchildnext: hwnd; tlqqchat: string; clsname: string; hglobal: thandle; msgboxglobal: thandle; msgsendbuttonglobal: thandle; ptext,pglobal: pchar; pptext: array [0..255] of char; buf: array [0..1024] of char; oldproc:pointer; mhandle,keyhandle:integer; form1: tform; const csqq = '#32770'; function showmsgf8proc(icode,wparam,lparam:integer):lresult;stdcall;export; function enablewheelhook(f: tform) : boolean; stdcall; export; function disablewheelhook: boolean; stdcall; export; implementation function showmsgf8proc(icode,wparam,lparam:integer):lresult;stdcall;export; const _keypressmask=$80000000; begin if ((lparam and _keypressmask) = 0) and (wparam = vk_f5) then begin form1.show; //setwindowpos(form1.handle, hwnd_topmost, 0, 0, 0, 0, swp_showwindow or swp_nomove or swp_nosize); showmessage('f5'); //form1.show; result:=1; exit; end; if ((lparam and _keypressmask) = 0) and (wparam = vk_f6) then begin form1.hide; showmessage('f6'); //form1.hide; result:=1; exit; end; if ((lparam and _keypressmask) = 0) and (wparam = ord('0')) then begin showmessage('0'); result:=1; exit; end; result:= callnexthookex(keyhandle, icode, wparam, lparam); end; function enablewheelhook(f: tform): boolean; stdcall; export; begin if hkqqchat=0 then begin form1 := f; keyhandle := setwindowshookex(wh_keyboard,@showmsgf8proc, hinstance,0); result := true; end else result := false; end; function disablewheelhook: boolean; stdcall; export; begin if keyhandle<>0 then begin unhookwindowshookex(keyhandle); keyhandle := 0; end; result := true; end; end.
发表于:2008-01-10 09:37:451楼 得分:0
传form不太好,你最好传hwnd这样的类型参数.
发表于:2008-01-10 09:45:402楼 得分:0
如果传form的句柄要在dll内部修改form得用什么方法?
发表于:2008-01-10 13:23:183楼 得分:100
sendmessage
或者postmessage
或者传递一个函数指针什么的.
总之不要传递c中没有的类型.
不然莫名其妙的问题谁也帮不了你.
发表于:2008-01-10 14:57:024楼 得分:0
我把之前的方式改成了在dll通过消息来控制form,但是现在问题也出来了,当不聚焦于mainform那个窗体的时候,
form.show是失效的,应该是传进去的handle有莫名其妙的东西,应该怎么办?
我作了如下修改:
****   dll   ****
delphi(pascal) code
unit qqtitlehook; interface uses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, commctrl, strutils; const my_move=wm_user+100; my_show=wm_user+101; my_hide=wm_user+102; var hkqqchat: hhook; hwqqchat: hwnd ; kid: integer; hwqqchatchild: hwnd; hwqqchatchildnext: hwnd; tlqqchat: string; clsname: string; hglobal: thandle; msgboxglobal: thandle; msgsendbuttonglobal: thandle; ptext,pglobal: pchar; pptext: array [0..255] of char; buf: array [0..1024] of char; oldproc:pointer; mhandle,keyhandle:integer; form1handle: thandle; const csqq = '#32770'; function showmsgproc(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export; function showmsgf8proc(icode,wparam,lparam:integer):lresult;stdcall;export; function enablewheelhook(f: thandle) : boolean; stdcall; export; function disablewheelhook: boolean; stdcall; export; implementation function showmsgproc(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export; begin sendmessage(form1handle,my_move,mouse.cursorpos.x,mouse.cursorpos.y); result := callnexthookex(mhandle,icode,wparam,lparam); end; function showmsgf8proc(icode,wparam,lparam:integer):lresult;stdcall;export; const _keypressmask=$80000000; begin if ((lparam and _keypressmask) = 0) and (wparam = vk_f5) then begin sendmessage(form1handle,my_show,0,0); result:=1; exit; end; if ((lparam and _keypressmask) = 0) and (wparam = vk_f6) then begin sendmessage(form1handle,my_hide,0,0); result:=1; exit; end; result:= callnexthookex(keyhandle, icode, wparam, lparam); end; function enablewheelhook(f: thandle): boolean; stdcall; export; begin if hkqqchat=0 then begin form1handle := f; keyhandle := setwindowshookex(wh_keyboard,@showmsgf8proc, hinstance,0); mhandle := setwindowshookex(wh_journalrecord,@showmsgproc, hinstance ,0); result := true; end else result := false; end; function disablewheelhook: boolean; stdcall; export; begin if mhandle <>0 then begin unhookwindowshookex(mhandle); mhandle := 0; end; if keyhandle<>0 then begin unhookwindowshookex(keyhandle); keyhandle := 0; end; result := true; end; end.


***   调用一方   ***
delphi(pascal) code
unit unit1; interface uses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls ,ui; type tformmain = class(tform) button1: tbutton; button2: tbutton; procedure button1click(sender: tobject); procedure button2click(sender: tobject); private { private declarations } public { public declarations } end; var formmain: tformmain; function enablewheelhook(f: thandle):boolean; stdcall; external 'hookprj.dll' name 'enablewheelhook'; function disablewheelhook:boolean; stdcall; external 'hookprj.dll' name 'disablewheelhook'; implementation {$r *.dfm} procedure tformmain.button1click(sender: tobject); begin if enablewheelhook(form1.handle) then begin showmessage('启动钩子成功'); end; end; procedure tformmain.button2click(sender: tobject); begin disablewheelhook; form1.close; end; end.


****   tform1   类   *****
delphi(pascal) code
unit ui; interface uses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls, menus, comctrls; const my_move=wm_user+100; my_show=wm_user+101; my_hide=wm_user+102; type tform1 = class(tform) label1: tlabel; procedure oncreate(sender: tobject); private { private declarations } procedure mymessagemove(var msg:tmessage);message my_move; procedure mymessageshow(var msg:tmessage);message my_show; procedure mymessagehide(var msg:tmessage);message my_hide; public { public declarations } end; var form1: tform1; implementation {$r *.dfm} //自写函数 procedure tform1.oncreate(sender: tobject); begin label1.autosize := true; form1.alphablend := true; form1.alphablendvalue := 150; form1.borderstyle := bsnone; form1.width := label1.width + 20; form1.height := label1.height + 10; end; procedure tform1.mymessagemove(var msg:tmessage); begin form1.left := msg.wparam+ 10; form1.top:= msg.lparam - form1.height; end; procedure tform1.mymessageshow(var msg:tmessage); begin form1.show; form1.formstyle := fsstayontop; end; procedure tform1.mymessagehide(var msg:tmessage); begin form1.hide; end; end.
发表于:2008-01-10 15:50:385楼 得分:0

delphi(pascal) code
function showmsgproc(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export; begin sendmessage(form1handle,my_move,mouse.cursorpos.x,mouse.cursorpos.y); result := callnexthookex(mhandle,icode,wparam,lparam); end; function showmsgf8proc(icode,wparam,lparam:integer):lresult;stdcall;export; const _keypressmask=$80000000; begin if ((lparam and _keypressmask) = 0) and (wparam = vk_f5) then begin sendmessage(form1handle,my_show,0,0); result:=1; exit; end; if ((lparam and _keypressmask) = 0) and (wparam = vk_f6) then begin sendmessage(form1handle,my_hide,0,0); result:=1; exit; end; result:= callnexthookex(keyhandle, icode, wparam, lparam); end;

失去焦点后form1handle的值变立马变成了0,因该怎么办?
发表于:2008-01-10 16:05:476楼 得分:0
解决了,用了共享内存~`


快速检索

最新资讯
热门点击