?
Solved

Hook functions cause NT 4.0 to crash. Why?

Posted on 1997-02-27
6
Medium Priority
?
311 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 150 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
Technology Partners: 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

770 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