Does that mean from the VB application I need to specifically call the dllMain method? If so, what do I put as the DWORD aReason and LPVOID aVoid parameters?
Main Topics
Browse All TopicsFollowing on from my question at: http://www.experts-exchang
The call to SetWindowsHookEx function seems to be failing. I've checked the dllMain method doesn't seem to get called at any time either:
BOOL WINAPI dllMain(HINSTANCE aHInstance, DWORD aReason, LPVOID aVoid){
MessageBox(0, "Start", "", 0);
if(aReason==DLL_PROCESS_AT
hInstance=aHInstance;
MessageBox(0, "On", "", 0);
}
if(aReason==DLL_PROCESS_DE
removeKeyHook();
MessageBox(0, "Off", "", 0);
}
return true;
}
Thanks,
Uni
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
No, your VB code is OK. Dll hook code must be changed. WinMain is called by Windows when it loads Dll to a process address space.
Just change Dll code. First try to replace HINSTANCE parameter with result of LoadLibrary. In Dll you know Dll name, this should not be problem. If this doesn't help, move hook callback function to another Dll, load it using LoadLibrary and use handle returned by LoadLibrary in SetWindowsHookEx call.
>>Just change Dll code. First try to replace HINSTANCE parameter with result of LoadLibrary.
I don't call LoadLibrary inside the DLL?
>>If this doesn't help, move hook callback function to another Dll, load it using LoadLibrary and use handle returned by LoadLibrary in SetWindowsHookEx call.
This would mean I'd have a VB application and two DLL's???
I don't see any LoadLibrary call in your code.
Try this:
__declspec(dllexport) bool setKeyHook(){
if(messageHook==NULL){
HMODULE h = LoadLibrary("Hook.dll");
HOOKPROC hProc = (HOOKPROC)GetProcAddress(h
messageHook=SetWindowsHook
if(messageHook==NULL){
return false;
}
}
return true;
}
If this still doesn't work, try to use second library.
Agan, please check return code and call GetLastError after every function.
This is getting quite confusing now...
>>I don't see any LoadLibrary call in your code.
That's because there isn't one...
My VB calls LoadLibrary on hook.dll to load the dll, why do I need to call:
HMODULE h = LoadLibrary("Hook.dll");
from within the Hook.dll (which would load itself?) when it has already been loaded by the VB application.
No, the case must match. Let me put it like that: A DLL entry point has some advantaged, so it won't hurt to have one. However there are some limitations about what you are allowed to do there. I have the habit to add one, even if only for debugging purposes. And no, no need to export it nor to call it directly, the system loader will do that for you.
Business Accounts
Answer for Membership
by: AlexFMPosted on 2007-09-14 at 09:06:48ID: 19892775
Looking at your code, I guess it fails because you pass incorrect HINSTANCE parameter to the SetWindowsEx function. This should be result of LoadLibrary function and not HINSTANCE passed to DllMain.
Try to get the same library handle using LoadLibrary. If this doesn't help, place hook callback function to separate Dll. Dll which is called by VB client belongs to this client process. For global hook, Windows needs to inject Dll to another processes, this is why LoadLibrary is used.
For most Windows API you can get additional error information by using GetLastError, this helps to find the reason of the fault.