• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

Hook functions cause NT 4.0 to crash. Why?

NT 4.0 always crashes using these functions. Why ?

xy.cpp
------

#include <windows.h>
#include <winuser.h>
#include <stdio.h>

extern "C" void sysmon(void);
extern "C" LRESULT CallWndProc(int nCode,WPARAM wParam, LPARAM lParam);

int main(void)
{
printf("\nHidOpApp started...");
HOOKPROC hproc;
static HINSTANCE hDLL;
static HHOOK hmsg;
if ((hDLL=LoadLibrary((LPCTSTR)"xy.dll"))==NULL)
   printf("%c\nDLL not found !",7);
   else
if ((hproc=(HOOKPROC)GetProcAddress(hDLL,"_CBTProc"))==NULL)
   printf("%c\nFunction not found in DLL !",7);
   else
if ((hmsg=SetWindowsHookEx(WH_CBT,hproc,hDLL,0))==NULL)
   printf("%c\nHook not installed !",7);
FreeLibrary(hDLL);
printf("\nHidOpApp finished.\nPress RETURN ...");
while (getchar()==-1);
return 0;
}

xy.dll
------

#include <stdio.h>
#include <owl\owlpch.h>

extern "C" LRESULT _export CallWndProc(int nCode,WPARAM  wParam,LPARAM lParam);

BOOL WINAPI DllEntryPoint(HINSTANCE hinstDll,uint32 fdwReason,LPVOID lpvReserved)
      {
      if (fdwReason==DLL_PROCESS_ATTACH) printf("\nProcess Attach DLL");
         else
      if (fdwReason==DLL_THREAD_ATTACH) printf("\nThread Attach DLL");
      else
      if (fdwReason==DLL_PROCESS_DETACH) printf("\nProcess Detach DLL");
      else
      if (fdwReason==DLL_THREAD_ATTACH) printf("\nThread Detach DLL");
      else printf("\nUnknown action DLL");
   return TRUE;
   }

extern "C" LRESULT _export CBTProc(int nCode,WPARAM wParam,LPARAM lParam)
      {
      printf("\nCallWndProc wurde aufgerufen !");
      if (nCode==HC_ACTION) printf("\nCWP must act !");
         else printf("\nCWP mustn't act !");
      if (wParam==0) printf("\nMessage sent by this process !");
         else printf("\nMessage sent by other process !");
      return 0;
      }
0
Torsten021897
Asked:
Torsten021897
  • 3
  • 3
1 Solution
 
serge010697Commented:
If NT crashes during while (getchar()==-1); this is because you called
FreeLibrary before. You don't have to do that, because your CBTProc doesn't exist after that. Call it right before return statement.

0
 
Torsten021897Author Commented:
It's not because of the while(getchar()==-1) loop, I've tried that ! Whatever kind of loop you install, it spills out lots of Dr. Watsons, and the system just hangs until you kill all the Dr. Watsons and sometimes the Explorer too. I think the fault lies more in that the hook can't properly access the hook procedure CBTProc.
0
 
serge010697Commented:
One more thing I've noticed: you forget to use WINAPI prefix for CBTProc.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Torsten021897Author Commented:
The help-topic on hooks describes this function just as it is, and if I include the WINAPI keyword, I can't do an _export from the DLL !
0
 
serge010697Commented:
Where exactly NT crashes?
0
 
Torsten021897Author Commented:
NT crashes as soon as I try to invoke any other commands after the 'SetWindowsHookEx(...);' statement has been executed and I try to open a new Window, or try to change to another application while the program is running.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now