Solved

Hook functions cause NT 4.0 to crash. Why?

Posted on 1997-02-27
6
309 Views
Last Modified: 2010-04-10
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
Comment
Question by:Torsten021897
[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
  • 3
  • 3
6 Comments
 
LVL 1

Accepted Solution

by:
serge010697 earned 50 total points
ID: 1161877
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
 

Author Comment

by:Torsten021897
ID: 1161878
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
 
LVL 1

Expert Comment

by:serge010697
ID: 1161879
One more thing I've noticed: you forget to use WINAPI prefix for CBTProc.
0
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!

 

Author Comment

by:Torsten021897
ID: 1161880
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
 
LVL 1

Expert Comment

by:serge010697
ID: 1161881
Where exactly NT crashes?
0
 

Author Comment

by:Torsten021897
ID: 1161882
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

734 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