Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

simple hook question

Posted on 1999-07-21
15
Medium Priority
?
411 Views
Last Modified: 2013-12-03
Hi,
Since  the setwindowshookex()  have told the WINODWS SYSTEM the address and the handle of the application instance
of the hook procedure,why must some hook procedures be put into a dll?
 Thanks......
 
0
Comment
Question by:geeboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 3

Accepted Solution

by:
Laminamia063099 earned 200 total points
ID: 1403443
The system hooks are a shared resource, and installing one affects all applications.  For this reason they must be placed in libraries (Dll's).

0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 1403444
If you have any other questions, just ask :)
0
 
LVL 2

Expert Comment

by:zyqwert
ID: 1403445
Let's say you have a mouse hook and there is a mouse event for another process.  Windows is going to give the message to the other process, but first it wants to call your hook.  The address of your hook proc is not valid in this other process, so there is no way for windows to call your hook.
Since your hook is in a DLL, windows chooses to do a LoadLibrary of your dll -- in the other address space.  Now your dll is mapped into 2 address spaces, the one of the original program, and the one of the other process.  So now windows can call find what address your hook proc is mapped to in the other address space, and call it.
Since this is a new instance of your dll running in a different address space, it will have new copies of all global variables, just as if you were running two instances of an application.
Now, LoadLibrary makes more sense with dll's than with exe's.  It will work with exe's, but there are some features (like transparent thread-local storage) which only an exe can have.

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:geeboy
ID: 1403446
Thank both...
My key confusion is ,well,since it's WINDOWS that call the hook function not special application and in my opinion WINDOWS itself is a application and should have it own address.So,why WINDOWS map .dll into the application address that will be hooked instead of just map it into WINDOWS itself address and run it when necessary?
>>The address of your hook proc is not valid in this other process, so there is no way for windows to call your hook.

why the hook proc must be valid in other process?
as you said,for every necessry app,windows will do a map-working if there is a systme-wide hook exist,right?

Thanks again...






 
0
 

Author Comment

by:geeboy
ID: 1403447
Yeh,in short,the WINDOWS call the hook function from the aimed application's address or WINDOWS itself's address?
If the answer is the first,why need that fuss.why WINDOWS just map a hook fuction into its own address and call for every eligible message?

BTW: WINDOWS will do the LOADLIBRARY() for necessary application,right?

0
 

Author Comment

by:geeboy
ID: 1403448
BTW 2:can I load a .dll for another process like WINDOWS do?
0
 
LVL 1

Expert Comment

by:pavel030297
ID: 1403449
1. Not every Windows is an application. While it's close to truth for Win 3.11 and Win9x, WinNT/2000 is definitely not "just application".
2. Your hook code should execute on the context of the process/thread whose message was hooked, that's why windows maps your dll into that process.
3. Windows WILL do LoadLibrary as necessary.
4.You can load a dll to another process, but it's complicated and can be accomplished in different ways in Win9x and WinNT/2000.
0
 

Author Comment

by:geeboy
ID: 1403450
Thanks pavel.
Huh,well,I just wonder where should I put the setwindowshookex in ,in the .dll or .exe?

0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 1403451
SetWindowsHookEx should be in your application.  The Hook procedure should be in a dll.  Read this excerpt from MSDN, as it explains one simple way to do this:

"You can install a hook procedure by calling the SetWindowsHookEx function and specifying the type of hook calling the procedure, whether the procedure should be associated with all threads in the same desktop as the calling thread or with a particular thread, and a pointer to the procedure entry point.

You must place a global hook procedure in a DLL separate from the application installing the hook procedure. The installing application must have the handle to the DLL module before it can install the hook procedure. The LoadLibrary function, when given the name of the DLL, returns the handle to the DLL module. After you have the handle, you can call the GetProcAddress function to retrieve the address of the hook procedure. Finally, you use SetWindowsHookEx to install the hook procedure address in the appropriate hook chain. SetWindowsHookEx passes the module handle, a pointer to the hook-procedure entry point, and 0 for the thread identifier, indicating that the hook procedure should be associated with all threads in the same desktop as the calling thread. This sequence is shown in the following example.

HOOKPROC hkprcSysMsg;
static HINSTANCE hinstDLL;
static HHOOK hhookSysMsg;
 
hinstDLL = LoadLibrary((LPCTSTR) "c:\\windows\\sysmsg.dll");
hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "SysMessageProc");
hhookSysMsg = SetWindowsHookEx(WH_SYSMSGFILTER,
    hkprcSysMsg, hinstDLL, 0);
 
You can release a thread-specific hook procedure (remove its address from the hook chain) by calling the UnhookWindowsHookEx function, specifying the handle to the hook procedure to release. Release a hook procedure as soon as your application no longer needs it."


(Wow, I miss a lot of discussion when I don't have ISP connection at night...just during the day :(

Laminamia :)
0
 

Author Comment

by:geeboy
ID: 1403452
Thank you ,Laminamia ...
As you said:
1...>>SetWindowsHookEx should be in your application.

Huh,I have saw many times that the SetWindowsHookEx() is implimented in the .dll just like hook procedure.Maybe it doesn't matter where to put,right?

2...How the windows know where and what .dll should be used when I set a system-wide hook? I'm sure not in the register,right?

Laminamia,would you please give some hints on these?
best regards to all experts...


 
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 1403453
>>I have saw many times that the SetWindowsHookEx() is implimented in the .dll just like hook procedure.Maybe it doesn't matter where to put,right?
No, it doesn't matter much.  If it's in an application, and the hook procedure is one that someone else wrote for debugging or other purposes, you must call LoadLibrary and SetWindowsHookEX from your app.  If you write the dll, or someone else wrote it as such, it may be in your dll instead, and you just have to call LoadLibrary.  It changes from case to case.  MSDN recommends putting it in your application so that you can reuse it with more flexibility and follow a standard method.

>>...How the windows know where and what .dll should be used when I set a system-wide hook? I'm sure not in the register,right?
Not in the registry...
First, use LoadLibrary to load the DLL in which the Hook procedure resides.  This function will be called with the name of the Dll in mind, and will return a handle to the Dll.  You will be telling windows the name of the hook.  The following code demonstrates how to use the handle returned from load library to get the address of the hook procedure, whose name you must know, and use the address of the hook procedure to set the hook.  

HOOKPROC hkprcSysMsg;   //address of the hook proc
static HINSTANCE hinstDLL; //instance of the dll
static HHOOK hhookSysMsg; //handle to the hook
 
hinstDLL = LoadLibrary((LPCTSTR) "c:\windows\\sysmsg.dll"); //Loading dll...
hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "SysMessageProc"); //using dll handle to get the sysMessageProc, ie. the Hook procedure
hhookSysMsg = SetWindowsHookEx(WH_SYSMSGFILTER,
    hkprcSysMsg, hinstDLL, 0); //setting the hook using the hook procedure address and the application handle (the dll handle returned from load library)

Now, windows knows that you've installed a system wide hook (from the parameter 0 in the SetWindowsHookEx()) and that the hook procedure is found at address hkprcSysMsg and that that procedure is found in hinstDll.

This section from the listing I provided above restates this procedure, better than I can.

If you have any more questions, ask.  

 "The installing application must have the handle to the DLL module before it can install the hook procedure. The LoadLibrary function, when given the name of the DLL, returns the handle to the DLL module. After you have the handle, you can call the GetProcAddress function to retrieve the address of the hook procedure. Finally, you use SetWindowsHookEx to install the hook procedure address in the appropriate hook chain. SetWindowsHookEx passes the module handle, a pointer to the hook-procedure entry point, and 0 for the thread identifier, indicating that the hook procedure should be associated with all threads in the same desktop as the calling thread."
0
 

Author Comment

by:geeboy
ID: 1403454
Thank you very much,Laminamia,you elaborate your answer.
Btw:
I want to ask you a small question.
I have got the handle of a certain process and that process own a window.How can I get the hwnd of that window?What the API.
Yeh,I should fire another question for this,but just do this for convenience.
If you are willing to do the favor,I show my appreciation.


0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 1403455
I would be happy to answer any questions, but it seems that you've stumped me.  Let me look into this, leave a comment tomorrow to remind me, and if I haven't found something by then, post it in a question, ok?

Laminamia

0
 

Author Comment

by:geeboy
ID: 1403456
Oh,a bit forgotten.
As you said ,Laminamia,if I put my setwindowshookex() in my application.Then when it execute callnexthookex() in the hook procedure in the .dll.How should I put the first param in the callnexthookex()? It needs the "hhook" of the hook.But I get it from hhook=setwindowshookex() in the .exe.

what the solution?
0
 
LVL 3

Expert Comment

by:Laminamia063099
ID: 1403457
I don't know the answer to that one.  You may have to decide to put your SetWindowsHookEx in the dll after all.  This may be something that changes depending on the application.  Sorry I don't have a clearer answer :(

Laminamia :)
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question