Solved

Hook functions cause NT 4.0 to crash. Why?

Posted on 1997-02-27
6
305 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
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now