Hook instance note

Source: Internet
Author: User

DLL part:

//// XXX. h

# Ifndef hook_api
# Define hook_api _ declspec (dllimport)
# Endif

Hook_api bool winapi setdipshook (DWORD dwthreadid );

 

//// XXX. cpp

# Define hook_api _ declspec (dllexport)
# Include "hook. H"
# Include <windows. h>
# Include "resource. H"
# Include <fstream>

Using namespace STD;

# Pragma data_seg ("shared ")
Hhook g_hhook = NULL;
DWORD g_dwthreadiddips = 0;
# Pragma data_seg ()

# Pragma comment (linker, "/section: shared, RWS ")

Lresult winapi getmsgproc (INT ncode, wparam, lparam );

Int_ptr winapi dlg_proc (hwnd, uint umsg, wparam, lparam );

Hinstance g_hinstdll = NULL;

Bool apientry dllmain (hmodule,
DWORD ul_reason_for_call,
Lpvoid lpreserved
)
{
Switch (ul_reason_for_call ){

Case dll_process_attach:
// DLL is attaching to the address space of the current process.
G_hinstdll = hmodule;
Break;

Case dll_thread_attach:
// A new thread is being created in the current process.
Break;

Case dll_thread_detach:
// A thread is exiting cleanly.
Break;

Case dll_process_detach:
// The Calling process is detaching the DLL from its address space.
Break;
}
Return true;
}

Bool winapi setdipshook (DWORD dwthreadid)
{
Bool Fok = false;

If (dwthreadid! = 0 ){

// Save our thread ID in a shared variable so that our getmsgproc
// Function can post a message back to thread when the server
// Window has been created.
G_dwthreadiddips = getcurrentthreadid ();

// Install the hook on the specified Thread
G_hhook = setwindowshookex (wh_getmessage, getmsgproc, g_hinstdll,
Dwthreadid );

Fok = (g_hhook! = NULL );
If (FOK ){
// The Hook was installed successfully; force a benign message
// The thread's queue so that the hook function gets called.
Fok = postthreadmessage (dwthreadid, wm_null, 0, 0 );
}
} Else {

// Make sure that a hook has been installed.
Fok = unhookwindowshookex (g_hhook );
G_hhook = NULL;
}

Return (FOK );
}

Lresult winapi getmsgproc (INT ncode, wparam, lparam ){

Static bool ffirsttime = true;

If (ffirsttime ){
// The DLL just got injected.
Ffirsttime = false;

// Uncomment the line below to invoke the debugger
// On the process that just got the injected DLL.
// Forcedebugbreak ();

// Create the DTIS server window to handle the client request.
Createdialog (g_hinstdll, makeintresource (idd_dialog1), null, dlg_proc );

// Tell the dips application that the server is up
// And ready to handle requests.
Postthreadmessage (g_dwthreadiddips, wm_null, 0, 0 );
}

Return (callnexthookex (g_hhook, ncode, wparam, lparam ));
}

//////////////////////////////////////// ///////////////////////////////////////

Void dlg_onclose (hwnd ){

Destroywindow (hwnd );
}

//////////////////////////////////////// ///////////////////////////////////////

Static const tchar g_szregsubkey [] =
Text ("software // Richter // desktop Item Position saver ");

//////////////////////////////////////// ///////////////////////////////////////

Void savelistviewitempositions (hwnd hwndlv ){
/*

Int nmaxitems = listview_getitemcount (hwndlv );

// When saving new positions, delete the old position
// Information that is currently in the registry.
Long L = regdeletekey (HKEY_CURRENT_USER, g_szregsubkey );

// Create the registry key to hold the info
Hkey;
L = regcreatekeyex (HKEY_CURRENT_USER, g_szregsubkey, 0, null,
Reg_option_non_volatile, key_set_value, null, & hkey, null );
Chassert (L = error_success );

For (INT nitem = 0; nitem <nmaxitems; nitem ++ ){

// Get the name and position of a listview item.
Tchar szname [max_path];
Listview_getitemtext (hwndlv, nitem, 0, szname, chdimof (szname ));

Point pt;
Listview_getitemposition (hwndlv, nitem, & pt );

// Save the name and position in the registry.
L = regsetvalueex (hkey, szname, 0, REG_BINARY, (pbyte) & PT, sizeof (PT ));
Chassert (L = error_success );
}
Regclosekey (hkey );
*/
Ofstream fout ("E:/1.txt ");
Fout <"save ";
Fout. Close ();

}

//////////////////////////////////////// ///////////////////////////////////////

Void restorelistviewitempositions (hwnd hwndlv ){
/*
Hkey;
Long L = regopenkeyex (HKEY_CURRENT_USER, g_szregsubkey,
0, key_query_value, & hkey );
If (L = error_success ){

// If the listview has autoarrange on, temporarily turn it off.
DWORD dwstyle = getwindowstyle (hwndlv );
If (dwstyle & lvs_autoarrange)
Setwindowlong (hwndlv, gwl_style, dwstyle &~ Lvs_autoarrange );

L = no_error;
For (INT nindex = 0; l! = Error_no_more_items; nindex ++ ){
Tchar szname [max_path];
DWORD cbvaluename = chdimof (szname );

Point pt;
DWORD cbdata = sizeof (PT), nitem;

// Read a value name and position from the registry.
DWORD dwtype;
L = regenumvalue (hkey, nindex, szname, & cbvaluename,
Null, & dwtype, (pbyte) & PT, & cbdata );

If (L = error_no_more_items)
Continue;

If (dwtype = reg_binary) & (cbdata = sizeof (PT ))){
// The value is something that we recognize; try to find
// An item in the listview control that matches the name.
Lv_findinfo lvfi;
Lvfi. Flags = lvfi_string;
Lvfi. psz = szname;
Nitem = listview_finditem (hwndlv,-1, & lvfi );
If (nitem! =-1 ){
// We found a match; change the item's position.
Listview_setitemposition (hwndlv, nitem, Pt. X, Pt. y );
}
}
}
// Turn autoarrange back on if it was originally on.
Setwindowlong (hwndlv, gwl_style, dwstyle );
Regclosekey (hkey );
}
*/
Ofstream fout ("E:/1.txt ");
Fout <"LOAD ";
Fout. Close ();
}

//////////////////////////////////////// ///////////////////////////////////////

Int_ptr winapi dlg_proc (hwnd, uint umsg, wparam, lparam ){

Switch (umsg ){

Case wm_app:
// Uncomment the line below to invoke the debugger
// On the process that just got the injected DLL.
// Forcedebugbreak ();

If (lparam = 1)
Savelistviewitempositions (hwnd) wparam );
Else
Restorelistviewitempositions (hwnd) wparam );
Break;
}

Return (false );
}

 

 

 

 

EXE testing:

Hwnd hwndlv =: getfirstchild (: findwindow ("progman", null )));

If (hwndlv = NULL)
Afxmessagebox ("zfq ");

Setdipshook (getwindowthreadprocessid (hwndlv, null ));

// Wait for the dips server window to be created.
MSG;
Getmessage (& MSG, null, 0, 0 );

// Find the handle of the hidden dialog box window.
Hwnddips =: findwindow (null, "hookserver ");

// Tell the dips window which listview window to manipulate
// And whether the items shocould be saved or restored.
: Sendmessage (hwnddips, wm_app, (wparam) hwndlv, 0 );

// Tell the dips window to destroy itself. Use sendmessage
// Instead of postmessage so that we know the window is
// Destroyed before the hook is removed.
: Sendmessage (hwnddips, wm_close, 0, 0 );

// Unhook the DLL, removing the dips dialog box procedure
// From the explorer's address space.
Setdipshook (0 );

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.