AppLog.docx

上传人:b****5 文档编号:7923355 上传时间:2023-01-27 格式:DOCX 页数:8 大小:17.05KB
下载 相关 举报
AppLog.docx_第1页
第1页 / 共8页
AppLog.docx_第2页
第2页 / 共8页
AppLog.docx_第3页
第3页 / 共8页
AppLog.docx_第4页
第4页 / 共8页
AppLog.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

AppLog.docx

《AppLog.docx》由会员分享,可在线阅读,更多相关《AppLog.docx(8页珍藏版)》请在冰豆网上搜索。

AppLog.docx

AppLog

AppLog.c

/****************************************************************

Modulename:

AppLog.c

Writtenby:

JonathanLocke

Notices:

Copyright(c)1995JeffreyRichter

Purpose:

Shellhooksdemonstrationapplication

****************************************************************/

 

#include"Win95ADG.h"/*SeeAppendixAfordetails*/

#include

#include

#pragmawarning(disable:

4001)/*Singlelinecomment*/

#include

#include"ShellHk.h"

#include"Resource.h"

 

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

 

//ForcealinkwiththeimportlibraryforShellHk.dll

#pragmacomment(lib,adgLIBBUILDTYPEadgLIBCPUTYPE"\\""ShellHk")

 

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

 

BOOLAppLog_OnInitDialog(HWNDhwnd,HWNDhwndFocus,LPARAMlParam){

adgSETDLGICONS(hwnd,IDI_APPLOG,IDI_APPLOG);

if(!

ShellHook_Start(GetDlgItem(hwnd,IDC_APPLOG))){

adgMB(__TEXT("Unabletostartshellhook"));

EndDialog(hwnd,IDCANCEL);

}

return(TRUE);//Acceptdefaultfocuswindow.

}

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

voidAppLog_OnSize(HWNDhwnd,UINTstate,intcx,intcy){

//Whentheuserresizesthemainwindow,wemustresizethelistbox

SetWindowPos(GetDlgItem(hwnd,IDC_APPLOG),NULL,0,0,cx,cy,SWP_NOZORDER);

}

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

voidAppLog_OnCommand(HWNDhwnd,intid,HWNDhwndCtl,UINTcodeNotify){

switch(id){

caseIDCANCEL:

//Allowsdialogboxtoclose

//Unhookshellhookbeforeexiting.

if(!

ShellHook_Stop())

adgMB(__TEXT("Unabletostopshellhook"));

EndDialog(hwnd,id);

break;

}

}

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

BOOLWINAPIAppLog_DlgProc(HWNDhwnd,UINTuMsg,WPARAMwParam,LPARAMlParam){

switch(uMsg){

//StandardWindowsmessages

adgHANDLE_DLGMSG(hwnd,WM_INITDIALOG,AppLog_OnInitDialog);

adgHANDLE_DLGMSG(hwnd,WM_COMMAND,AppLog_OnCommand);

adgHANDLE_DLGMSG(hwnd,WM_SIZE,AppLog_OnSize);

}

return(FALSE);//Wedidn'tprocessthemessage.

}

 

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

 

intWINAPIWinMain(HINSTANCEhinstExe,HINSTANCEhinstPrev,LPSTRlpszCmdLine,intnCmdShow){

adgWARNIFUNICODEUNDERWIN95();

adgVERIFY(-1!

=DialogBox(hinstExe,MAKEINTRESOURCE(IDD_APPLOG),

NULL,AppLog_DlgProc));

return(0);

}

 

ShellHk.c

/****************************************************************

Modulename:

ShellHk.c

Writtenby:

JonathanLocke

Notices:

Copyright(c)1995JeffreyRichter

Purpose:

Dllwhichsetsasystemshellhook.

****************************************************************/

#include"Win95ADG.h"/*SeeAppendixAfordetails*/

#include

#include

#pragmawarning(disable:

4001)/*Singlelinecomment*/

#include

#pragmawarning(disable:

4001)/*Singlelinecomment*/

//WemustdefineSHELLHKLIBAPIas'dllexport'beforeincludingShellHk.h.

//ShellHk.hwillseethatwehavealreadydefinedSHELLHKLIBAPIand

//willnot(re)defineitas'dllimport'.

#defineSHELLHKLIBAPI__declspec(dllexport)

#include"ShellHk.h"

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

HINSTANCEg_hinstDll=NULL;//ShellHk.dll'sinstancehandle

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

//CallingSetWindowsHookExwithathreadidofzerowillcausethisDLLtobe

//injectedintootherprocesses.Therefore,wemustdeclareashareddata

//sectionsothatallmappingsofourDLL(inallprocesses)sharethesame

//setofglobalvariables.

//#pragmacomment(lib,"kernel32")

#pragmadata_seg("Shared")

HHOOKg_hhook=NULL;//Hookhandleforsystemwideshellhook

HWNDg_hwndListBox=NULL;//Listboxinwhichtoinsertlogstrings

#pragmadata_seg()

//#pragmacomment(linker,"-section:

Shared,rws")

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

staticLRESULTWINAPIShellHook_HookProc(intnCode,WPARAMwParam,LPARAMlParam){

TCHARsz[128],szPath[128];

DWORDdwProcessID;

HWNDhwnd=(HWND)wParam;

DWORDdwThreadID=GetWindowThreadProcessId(hwnd,&dwProcessID);

LRESULTlResult=CallNextHookEx(g_hhook,nCode,wParam,lParam);

switch(nCode){

caseHSHELL_WINDOWCREATED:

caseHSHELL_WINDOWDESTROYED:

//GetModuleFileNameisbeingcalledinthecontextoftheprocessin

//whichthisDllhasbeeninjected.

if(!

GetModuleFileName(NULL,szPath,adgARRAY_SIZE(szPath)))

_tcscpy(szPath,__TEXT(""));

wsprintf(sz,__TEXT("%cpid=0x%08x,tid=0x%08x,path=%s"),

(nCode==HSHELL_WINDOWCREATED?

__TEXT('>'):

__TEXT('<')),

dwProcessID,dwThreadID,szPath);

ListBox_AddString(g_hwndListBox,sz);

break;

}

return(lResult);

}

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

BOOLWINAPIShellHook_Start(HWNDhwndListBox){

HHOOKhhook=NULL;

//ReturnFALSEifhookhasalreadybeeninstalled.

if(g_hhook!

=NULL)

return(FALSE);

adgASSERT(IsWindow(hwndListBox));

g_hwndListBox=hwndListBox;

//Giveuptheremainderofourthread'stimeslice.

//Thisgivesusabetterchanceofgettingallthewaythroughthecall

//toSetWindowsHookExandthevariableassignmenttog_hhookinoneshot.

//Ifwearepreemptedafterthehookisset,butbeforethevariableis

//updated,itispossibleforanotherthreadtoenterourhookfilter

//functionbeforethehookhandleisvalid.UnderWindowsNTthisisnot

//aproblem.UnderWindows95,nothavingavalidhookhandlewillcause

//CallNextHookExtofail.Ifthereissomereasonthatitiscritical

//thatyourapplicationsucceedincallingthenextfilterfunctionin

//thechain,theonlyrobustwaytowritethiscodeistousesomething

//liketheSWMRG(single-writer,multiple-readerguard)objectdeveloped

//inAdvancedWindows(MicrosoftPress).

Sleep(0);

//Setourkeyboardhook.

hhook=SetWindowsHookEx(WH_SHELL,ShellHook_HookProc,g_hinstDll,0);

//Ensurethatg_hhookisalwaysvalid(evenifwearepreemptedwhilst

//inthemiddleofwritingtoit)byupdatingthevariableatomically.

InterlockedExchange((PLONG)&g_hhook,(LONG)hhook);

return(g_hhook!

=NULL);

}

 

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

 

BOOLWINAPIShellHook_Stop(){

BOOLfOK=TRUE;

//Onlyuninstallthehookifitwassuccessfullyinstalled.

if(g_hhook!

=NULL){

fOK=UnhookWindowsHookEx(g_hhook);

g_hhook=NULL;

}

return(fOK);

}

 

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

 

BOOLWINAPIDllMain(HINSTANCEhinstDll,DWORDfdwReason,LPVOIDlpvReserved){

switch(fdwReason){

caseDLL_PROCESS_ATTACH:

g_hinstDll=hinstDll;

break;

}

return(TRUE);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 工学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1