14. Hook and Database Access

Source: Internet
Author: User

I. Hook 

1. Local hooks 


OS senses mouse or Keyboard Events and generates corresponding messages. Place the messages to the application.ProgramIn the message queue, the application calls the getmessage function to retrieve the message, and then calls the dispatchmessage function to schedule the message to the OS, the OS processes the message by calling the application window process specified during the design window class.

You can use setwindowshookex to install a hook. It is similar to a road card or a guard. Before terminating, an application must call the unhookwindowshookex function to free system resources associated with the hook. 

The last installed hook is always in front of the hook chain. Via callnexthookex passes

Hook information to the next hook procedure in the current hook chain. A hook

Procedure can call this function either before or after processing the hook information.

The macros of the virtual keyboard start with "VK.




  //  2  
Lresult callback mouseproc (
Int Ncode, // Hook code
Wparam, // Message identifier
Lparam // Mouse coordinates
Return 1 ;

Lresult callback keyboardproc (
Int Code, // Hook code
Wparam, // Virtual-key code
Lparam // Keystroke-message information
If (Vk_f2 = Wparam)
Sendmessage (g_hwnd, wm_close, 0 , 0 );
Unhookwindowshookex (g_hmouse );
Unhookwindowshookex (g_hkeyboard );
Return 1 ;

// 1
Int Cxscreen, cyscreen;
Cxscreen = Getsystemmetrics (sm_cxscreen );
Cyscreen = Getsystemmetrics (sm_cyscreen );
Setwindowpos ( & Wndtopmost, 0 , 0 , Cxscreen, cyscreen, swp_showwindow );
Sethook (m_hwnd );

2. Global hooks

If you want all processes to call the hook program, you must put the code for installing the hook in the dynamic library. The global hooks are a shared resource, and installing one affects all applications

In the same desktop as the calling thread. All Global hook functions must be in

Libraries. Global hooks shocould be restricted to special-purpose applications or to use as

Development aid during application debugging. libraries that no longer need a hook

Shocould remove its hook procedure.

3. involved APIs

1) getmodulehandle

The getmodulehandle function retrieves a module handle for the specified module if

File has been mapped into the address space of the calling process.

2) setwindowshookex

Installan application-defined hook procedure into a hook chain.

There are two ways to obtain the third parameter of the function:

Method 1: Save the DLL handle passed in by the dllmain function.


Bool winapi dllmain (
Hinstance hinstdll, // handle to the DLL module
DWORD fdwreason, // reason for calling function
Lpvoid lpvreserved // Reserved
G_hinst = hinstdll;

 Method 2: Use the getmodulehandle function.

3) cwnd: setwindowpos

To change the size, position, and Z-order of child, pop-up, and top-level windows.

4) getsystemmetrics

Retrieves the specified system metric or system configuration setting.


4. write-in replication mechanism

To solve the problem of program uncertainty when multiple processes access the global shared variables in the same DLL, a copy mechanism is introduced.

Figure p749 replication mechanism during write

When a process wants to modify the data on the DLL data page (2) (the data can be shared by multiple processes), the OS will allocate a new page and set the data page (2) copy the data to the new page, and then disconnect the data page (2) to map the data space of the process, and map the new page to the address space of the process.

5. Create a Sharing Section

As shown in the previous section, because of the replication mechanism during writing, if multiple processes really want to share a variable, it becomes a problem.

There are two ways to create a truly meaningful global shared variable.

Method 1: create a shared Section

# Pragma data_seg ("mysec") // mysec indicates the node name.
Hwnd g_hwnd = NULL;
# Pragma data_seg ()
# Pragma comment (linker, "/section: mysec, RWS") // read, write, share

View the information list of each section in dumpbin [headers]

Method 2: defined in def Module

Library hook
Sethook @ 2
Mysec read write share

6. Get the password

Install the message hook wh_getmessage to obtain information about the wm_gettext message. You can obtain the password information in the text box from the additional parameters of the message.

Ii. Database

1. Several new terms about database


ODBC (Open Database Connectivity), Open Database interconnection. ODBC is a technology that emerged in the late 1980s s and early 1990s S. It provides a unified interface for customer software writing relational databases. ODBC provides a single API that can be used to process customer applications of different databases. Applications using ODBC APIs can communicate with any relational database with ODBC drivers.


Ole db, object link and embedded database. Ole db extends ODBC in two aspects. First, ole db provides a database programming COM interface; second, ole db provides an interface that can be used for relational and non-relational data sources. The two basic structures of ole db are ole db provider and ole db user program (consumer ).

Objects in OLE (Object Linking and Embedding) dB mainly include data source objects, stage objects, command objects, and row group objects. An Application Using ole db uses the following request sequence: initializing OLE to connect to the data source, issuing commands, processing results, and releasing data source objects.

3) ado

ActiveX Data Objects (ActiveX Data Object) is built on ole db. ADO is an ole db user program, that is, it is a consumer and a COM component. Ado simplifies ole db and provides automation support so that script languages like VBScript can also use ADO to access the database.

To use ADO technology to access the database, the actual calling process is: the ADO client program accesses the ole db provider through ado, which slows down the access.

Ado has three core objects:

(1) The connection object indicates the connection to the database, which manages the communication between the application and the database.

(2) The command object is used to process repeated queries, or to query the output or return parameter values that need to be checked in the stored procedure call. There is an activeconnection attribute that is used to reference the connection object.

(3) The recordset object is used to obtain data and store query results. These results are composed of data rows (records) and columns (fields. Each column is stored in a field object in the fields set of recordset. There is an activeconnection attribute that is used to reference the connection object.

When using ADO to access the database, VB is easier to use than VC.



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.