Click to See Complete Forum and Search --> : Sending User Msgs from DLL to APP
mitch_m
May 27th, 2004, 09:39 PM
OK, the rundown..
I have a dll in the same folder as my app that hooks the system. when it does what it needs to do, I want it to notify the App, so it can unhook the system.
I've read articles that say not to use WM_USER or WM_APP, but instead use registered window messages.
Ive read articles on this site about it, but im still confonused.
I'm using PostMessage, and when i tried with a WM_APP msg, it didint work. When i tried doing UWM (Reg Win Msgs) like the articles said, in my app, i dont know how to handle it. I tried in main callback where WM msgs are processed, but t wont let me.
assistance is greatly appreciated.
NoHero
May 28th, 2004, 06:47 AM
Yes use the RegisterWindowMessage function... the dll is registering the message ... And if the program tries to register this message to, they are the same:
// DLL and APP stuff stuff
UINT m_HookNotify = 0;
m_HookNotify = RegisterWindowMessage("Hook_NotifyMessage");
If the app uses the same string as the dll they will get the same message, so you easily can implement such an notify system.
Or you can also use "Callback functions" where the app passes a pointer to a callback function to the dll which should be called if something happens.
These are the two possabilities ...
Regards NoHero
mitch_m
May 28th, 2004, 08:50 AM
NoHero- When i put code block in my DLL i get the error:
error C2501: 'UINT' : missing storage-class or type specifiers
I used Static Const infront of UINT, but still same thing.
How do i handle m_HookNotify in my main app. Can i look for it in the callback switch as i would for WM_CLOSE, WM_DESTROY, WM_COMMAND?
thanks
mitch_m
May 28th, 2004, 09:00 AM
OK, so i got the DLL to compile by decalring it like this:
static const UINT m_HookNotify =
::RegisterWindowMessage("Hook_NotifyMessage");
Now, how do i handle it in my main app? If i just put m_HookNotify in the WndProc, it will say its an undeclared identifier. If i try to re-register it in the app (unnecessary, i know) it says i cant use a constant there.
I know im doing something wrong, I just cant put my finger on it
Bond
May 28th, 2004, 11:24 AM
There's no need to register a Windows message. Your DLL will get loaded into the address space of your process.
You can do something like this:
(in DLL)
#pragma data_seg(".shared")
HHOOK g_hHook = 0;
HWND g_hwnd = 0;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.shared,RWS")
...
EXPORT BOOL APIENTRY InstallTheHook(HWND hwnd, BOOL bInstall)
{
if (bInstall)
{
g_hHook = SetWindowsHookEx(WH_CBT, CbtProc, g_hLibrary, 0);
g_hwnd = hwnd;
return g_hHook != NULL;
}
else
{
return UnhookWindowsHookEx(g_hHook);
}
}
(in App)
InstallTheHook(hwnd, TRUE);
Define a message like you normally would in your DLL header file, which you will include into your main app:
#define UM_FROMDLL (WM_USER + 1001)
Then, in your DLL, you can just post messages to g_hwnd and they'll be directed to your application.
mitch_m
May 28th, 2004, 01:10 PM
Bond- Thats what I originally did, but it still doesnt work. My DLL is rather small, so I just wrote it in 1 .cpp file, with no.h files. It may be bad programming practice, but it shouldnt affect the outcome.
I put the line
#define UM_FROMDLL (WM_USER + 1001)
in my DLL .cpp file and in my main app along with the other defines.
I tried using both PostMessage and SendNotifyMessage, but they still do not work. My main app recieves nothing when i handle
case UM_FROMDLL:
I know my DLL sends the msg.
mitch_m
May 28th, 2004, 01:14 PM
Ah, i got it working now.
When i was setting the hook, i was passing the wrong HWND as my main app. stupid me.
bond, nohero- thanks!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.