Click here to Skip to main content
16,006,474 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to capture End Process of task manager Pin
Nishad S12-Apr-06 0:36
Nishad S12-Apr-06 0:36 
GeneralRe: How to capture End Process of task manager Pin
Stephen Hewitt12-Apr-06 4:40
Stephen Hewitt12-Apr-06 4:40 
AnswerRe: How to capture End Process of task manager Pin
Wessam Fathi12-Apr-06 1:03
Wessam Fathi12-Apr-06 1:03 
GeneralRe: How to capture End Process of task manager Pin
Nishad S12-Apr-06 1:33
Nishad S12-Apr-06 1:33 
GeneralRe: How to capture End Process of task manager Pin
Stephen Hewitt12-Apr-06 4:43
Stephen Hewitt12-Apr-06 4:43 
GeneralRe: How to capture End Process of task manager Pin
Wessam Fathi12-Apr-06 8:11
Wessam Fathi12-Apr-06 8:11 
GeneralRe: How to capture End Process of task manager Pin
Stephen Hewitt12-Apr-06 14:05
Stephen Hewitt12-Apr-06 14:05 
GeneralRe: How to capture End Process of task manager Pin
Wessam Fathi12-Apr-06 15:24
Wessam Fathi12-Apr-06 15:24 
Well I've used Detours extensively myself, when developing my graduation project back at college - it was an antivirus and a firewall package.

I used detours in the antivirus part, to intercept all calls to CreateProcess function, when the user tries to run any program it was blocked until user Allows / Denies.

It did work, I can send you videos for the application running if you want Smile | :)

Here is the code I used, that's only a bit demonstrating the technique not the whole code, so it definetely won't compile:


1. Detours management code encapsulated in a dll:
-------------------------------------------------


BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hInst = (HINSTANCE)hModule;

//intercept the API functions of the host program
Intercept();

hook = NULL;

//request a unique message number form the sytem for inter processes messaging
WM_HOOKMESSAGE = RegisterWindowMessage("{398E8909-327E-4ce8-B523-012AA80808A0}");

return TRUE;
case DLL_PROCESS_DETACH:
//if we had a successful interception, de-intercept the detour function
if(CreateProcessW_T && CreateProcessA_T)
DeIntercept();

//if we had a successful windows hook, un install that hook
if(hWndServer != NULL)
ClearHook(hWndServer);

return TRUE;
}
return TRUE;
}


//hook install function
CREATEHOOK_API bool SetHook(HWND hWnd)
{
if(hWndServer != NULL)
return FALSE; // already hooked!

hook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)MyProc, hInst, 0);

if(hook != NULL)
{ /* success */
hWndServer = hWnd;
return TRUE;
} /* success */

return FALSE; // failed to set hook
}


//hook un-install functions
CREATEHOOK_API bool ClearHook(HWND hWnd)
{
if(hWnd != hWndServer || hWnd == NULL)
return FALSE;

BOOL unhooked = UnhookWindowsHookEx(hook);

if(unhooked)
hWndServer = NULL;

return unhooked;
}


//this is a dummy hook proc, since the main use of the hook is to
//load our library to all processes in the system
static LRESULT CALLBACK MyProc(int nCode, WPARAM wParam, LPARAM lParam)
{
return CallNextHookEx(hook, nCode, wParam, lParam);
}


//API interception installation function
CREATEHOOK_API int Intercept()
{
//intercept the CreateProcessW function
CreateProcessW_T
=(BOOL(WINAPI *)(LPCWSTR, LPWSTR, LPSECURITY_ATTRIBUTES,
LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, LPCWSTR, LPSTARTUPINFOW, LPPROCESS_INFORMATION))DetourFunction((PBYTE)CreateProcessW,
(PBYTE)CreateProcessW_D);



//intercept the CreateProcessA function
CreateProcessA_T
=(BOOL(WINAPI *)(LPCTSTR, LPTSTR, LPSECURITY_ATTRIBUTES,
LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, LPCTSTR, LPSTARTUPINFO, LPPROCESS_INFORMATION))DetourFunction((PBYTE)CreateProcessA,
(PBYTE)CreateProcessA_D);

return 0;
}


//API interception un-install function
CREATEHOOK_API int DeIntercept()
{

//Remove the interception of CreateProcessW function
DetourRemove((PBYTE)CreateProcessW_T, (PBYTE)CreateProcessW_D);

//Remove the interception of CreateProcessA function
DetourRemove((PBYTE)CreateProcessA_T, (PBYTE)CreateProcessA_D);

CreateProcessW_T = NULL;
CreateProcessA_T = NULL;

return 0;
}


2. Windows application that used the dll to intercept all calls to CreateProcess:
----------------------------------------------------------------------------


//locating and loading the hooking dll
char strPath[MAX_PATH];
GetModuleFileName(NULL, strPath, MAX_PATH);
PathRemoveFileSpec(strPath);
strcat(strPath, "\\CreateHook.dll");
hModDLL = LoadLibrary(strPath);

if(hModDLL != NULL)
{
//obtain a pointer to the hooking finstallation function
PTRSETHOOK startHook = (PTRSETHOOK)GetProcAddress(hModDLL, "SetHook");

//if obtained a valid pointer run the function
if(startHook)
{
startHook(hWnd);
bStatus = true; //set interception state to enabled
}
else
{
MessageBox(NULL, "Failed to start hooking", "Fatal Error", MB_ICONSTOP);
exit(9);
}
}
else
{
MessageBox(NULL, "Failed to load hooking DLL", "Fatal Error", MB_ICONSTOP);
exit(9);
}

--
Wessam Fathi
GeneralRe: How to capture End Process of task manager Pin
Stephen Hewitt12-Apr-06 18:17
Stephen Hewitt12-Apr-06 18:17 
GeneralRe: How to capture End Process of task manager Pin
Wessam Fathi13-Apr-06 2:40
Wessam Fathi13-Apr-06 2:40 
Question[Message Deleted] Pin
Taruni11-Apr-06 23:45
Taruni11-Apr-06 23:45 
AnswerRe: Display the text in the center Pin
Cedric Moonen11-Apr-06 23:51
Cedric Moonen11-Apr-06 23:51 
AnswerRe: Display the text in the center Pin
Nishad S12-Apr-06 0:01
Nishad S12-Apr-06 0:01 
QuestionHelp me ! Question for variables in stack! Pin
Best Kiluyar11-Apr-06 23:37
Best Kiluyar11-Apr-06 23:37 
AnswerRe: Help me ! Question for variables in stack! Pin
Rage12-Apr-06 1:23
professionalRage12-Apr-06 1:23 
AnswerRe: Help me ! Question for variables in stack! Pin
Wessam Fathi12-Apr-06 1:28
Wessam Fathi12-Apr-06 1:28 
QuestionDynamic cast problem Pin
Abyss11-Apr-06 23:32
Abyss11-Apr-06 23:32 
AnswerRe: Dynamic cast problem Pin
Ștefan-Mihai MOGA12-Apr-06 0:44
professionalȘtefan-Mihai MOGA12-Apr-06 0:44 
AnswerRe: Dynamic cast problem Pin
sunit512-Apr-06 0:50
sunit512-Apr-06 0:50 
GeneralRe: Dynamic cast problem Pin
toxcct12-Apr-06 1:14
toxcct12-Apr-06 1:14 
GeneralRe: Dynamic cast problem Pin
sunit512-Apr-06 1:39
sunit512-Apr-06 1:39 
GeneralRe: Dynamic cast problem Pin
Abyss12-Apr-06 5:49
Abyss12-Apr-06 5:49 
AnswerRe: Dynamic cast problem Pin
Rage12-Apr-06 1:31
professionalRage12-Apr-06 1:31 
AnswerRe: Dynamic cast problem Pin
Stephen Hewitt12-Apr-06 4:44
Stephen Hewitt12-Apr-06 4:44 
QuestionCryptography Pin
kiran janaswamy11-Apr-06 23:27
kiran janaswamy11-Apr-06 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.