Use of Windows SDK Hook, sdkhook

Source: Internet
Author: User

Use of Windows SDK Hook, sdkhook

Problems encountered when using SetWindowsHookEx

Function prototype

HHOOK WINAPI SetWindowsHookEx(  _In_  int idHook,  _In_  HOOKPROC lpfn,  _In_  HINSTANCE hMod,  _In_  DWORD dwThreadId);

  

WinHook. h

// WinHook.h: interface for the WinHook class.////////////////////////////////////////////////////////////////////////class WinHook  {public:WinHook();BOOL InstallHook(int idHook,HANDLE hInstance,HOOKPROC hookproc,int ThreadID);BOOL UnHook();LRESULT CallNextHook(int nCode,WPARAM wParam,LPARAM lParam);virtual ~WinHook();private:HHOOK hHook;};

  

WinHook. cpp

// WinHook.cpp: implementation of the WinHook class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "WinHook.h"//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////WinHook::WinHook(){}BOOL WinHook::InstallHook(int idHook,HANDLE hInstance,HOOKPROC hookproc,int ThreadID){if(hookproc == NULL) return NULL;hHook = SetWindowsHookEx(idHook,hookproc,(HINSTANCE)hInstance,ThreadID);return hHook != NULL;}BOOL WinHook::UnHook(){if(!hHook)return false;UnhookWindowsHookEx(hHook);hHook = NULL;return true;}LRESULT WinHook::CallNextHook(int nCode,WPARAM wParam,LPARAM lParam){return CallNextHookEx(hHook,nCode,wParam,lParam);}WinHook::~WinHook(){if(hHook)UnhookWindowsHookEx(hHook);}

  

Note that the callback function Hook Procedure

The callback function is prototype as follows:

LRESULT MyHookProc(int nCode,    WPARAM wParam,    LPARAM lParam);

  

According to MSDN

NCode[In]

Type:Int

Specifies whether the hook procedure must process the message. IfNCodeIsHC_ACTION, The hook procedure must process the message. IfNCodeIs less than zero, the hook procedure must pass the message toCallNextHookExFunction without further processing and shoshould return the value returnedCallNextHookEx.

 

If nCode is less than 0, call callNextHookex to return the value.

If nCOde is HC_ACTIOIN, we can process the code.

However, you need to know the message code for the Hook.

Next we will go to LParam.
LParam[In]

Type:LPARAM

A pointer toCWPRETSTRUCTStructure that contains details about the message.

Here, LPARAM points toCWPRETSTRUCTContains the Message Details.

 

The operation code is as follows:

StartHook and unHook

extern "C"__declspec(dllexport)BOOL InstallHook(){return wh.InstallHook(WH_CALLWNDPROCRET,hThisDLL,(HOOKPROC)MyHookProc,NULL);}extern "C"__declspec(dllexport)BOOL UnHook(){return wh.UnHook();}

  

The following is the operation code of the callback function.

LRESULT MyHookProc (int nCode, WPARAM wParam, LPARAM lParam) {if (nCode <0) return wh. callNextHook (nCode, wParam, lParam); switch (nCode) {case HC_ACTION: {PCWPRETSTRUCT hook_msg = (PCWPRETSTRUCT) lParam; if (hook_msg) {if (hook_msg-> message = WM_SETFOCUS) {// get the focus isAction = true;} else if (hook_msg-> message = WM_KILLFOCUS) {// loss of focus isAction = false; InvalidateRect (GetParent (hook_msg-> hwnd), NULL, true);} else if (hook_msg-> message = WM_PAINT) {// draw the customer zone if (isAction) {HDC dc = GetDC (GetParent (hook_msg-> hwnd); TextOut (dc, 0, 0, _ T ("123 ABCD "), sizeof (_ T ("123 ABCD"); ReleaseDC (GetParent (hook_msg-> hwnd), dc) ;}} else if (hook_msg-> message = WM_NCPAINT) {// plot non-customer zone HDC dc = GetWindowDC (GetParent (hook_msg-> hwnd); HICON hIcon = LoadIcon (HINSTANCE) hThisDLL, MAKEINTRESOURCE (IDI_ICON1 )); drawIcon (dc, 80, 0, hIcon); UpdateWindow (GetParent (hook_msg-> hwnd); ReleaseDC (GetParent (hook_msg-> hwnd), dc);} break;} default: break;} return wh. callNextHook (nCode, wParam, lParam );}

  

 




Windows sdk Programming

What is Windows SDK

The SDK is the software development kit (software development kit). It contains the documentation for Windows software development and the input library and header file of API functions (because the API is in the dynamic link library, these dynamic link libraries are part of the system and therefore do not need to be provided. The input library and header file are required so that you can use API functions in your program ). The early SDK was a separate package, which is now included in Visual C ++ and other development environments. If you have installed VC ++, you can start writing Windows programs. With the development of Windows systems, more and more sdks are available. We only need to grasp the most basic aspects. For other special topics, we will learn more based on our own interests and technical directions.

Don't be intimidated by the first Windows program

If you have already started, your textbooks should be the classic programming windows (petzold) or a similar book. But no matter which book, you will first face a basic Windows SDK program, which has dozens of lines. Although it is not long, it is much longer than the C version of hello world. Even worse, it is filled with strange variable types and constant definitions, but we don't need to be scared by it. Let's take a look at what is in it. First, there will be a # include <windows. h>. Well, it's no surprise. It's no different from # inclde <stdio. h>. Then there is a function declaration: lresult callback WndProc (HWND, UINT, WPARAM, LPARAM); it's a bit confusing, and several new words come out at once ", in addition, there are two modifiers before the function name, which were not previously encountered. Fortunately, we can also identify this as a function declaration. Next we will look at the WinMain function and a bunch of new words. I guess you may have started to get depressed. It is really difficult to read this program once, so it doesn't matter if you don't understand it. You can read the explanation in the book. This article does not require a complete analysis of this program. hfire cannot have petzold. Here hfire helps you analyze some strange things.

First, the Data Type of Windows. Although these data types seem unfamiliar, they are actually defined by the basic data type of C. For example, UINT is unsinged int, and PSTR is pointer to string. You can guess whether it is char *. Windows also has many system-defined structs, such as WNDCLASS and MSG, which can be understood as much as they are. Windows also has an important concept, handle. You can use a handle to operate Windows objects. HWND, HINSTANCE, and HDC are all handles.

Let's talk about the structure of the Windows program. Generally, there is a WinMain function as the entry point of the program. In WinMain, the window class is defined for message loop. A message loop is a common while loop in which messages are received and sent separately. Then, the window function WndProc can be set by name. Retrieve a message using a large switch structure, and write the Message Processing code under each case. The simplest Windows SDK program only needs to write these two functions. When your program has been written for a long time, you need to write the specific message processing code into a function so that it can be called when processing the message. You can even use C ++ to write the program. After you are familiar with this structure, you can make full use of it.

Others don't want to talk too much. It is very important to learn the SDK. Don't expect to clarify each line of code at the beginning.

Learning Methods

Of course, it is to write more programs. It is best to write one for each topic. From the first window, text is displayed to graphic display, controls, and dialog boxes, you can understand the connotation of Windows Programming by writing more. When the first part has learned almost the same thing, you can write a program that combines points. Finally, you will find that you can write a long program, and more than 1000 lines are not long ...... the remaining full text>

Differences between windows sdk and Visual C ++

The sdk is an Development Kit. windows sdk provides the widows system usage method. It provides a large number of system API functions, as well as some data structures and classes, you can regard the SDK as a user manual. Many software provides sdks, such as drivers, to allow other programs to use the software and provide software interfaces...
VC is a programming software for compiling windows applications .......
If you want to write a windows program, you can write it in any language. However, if you want to use a windows system, you must use the tools provided by the windows sdk. It is as if you cannot let your car go with your mind, you must manipulate the steering wheel and step on the throttling. the SDK provides tools for external control of the Software. Of course, the software tool is an API function, not a steering wheel...
What they share is that Microsoft products ....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.