Solved

Hook functions cause NT 4.0 to crash. Why?

Posted on 1997-02-27
6
310 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

724 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