我創建了一個系統範圍的鉤DLL和我的DLL我試圖得到通知每次一個新的進程創建或銷燬。
當有新的進程被檢測到時,我希望能夠向調用程序發送消息,無論是布爾值還是自定義對象。 我該怎麼做?
目前我正在使用一個文件來記錄所有名字!這是可怕的 這是迄今爲止代碼:我怎樣才能從DLL通信到調用應用程序在c + +
DEF文件
;LIBRARY
; Def file
EXPORTS
InstallHook
UninstallHook
dllapi.h
void InstallHook(void);
void UninstallHook(void);
dllmain.cpp
#include "stdafx.h"
using namespace std;
HINSTANCE currentProcessHandle;
HHOOK hookID;
string str = "1";
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,LPVOID lpReserved)
{
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
currentProcessHandle = hModule;
return TRUE;
}
LRESULT CALLBACK HookProcedure(int nCode, WPARAM wparam, LPARAM lparam)
{
if (nCode < 0) return CallNextHookEx(hookID, nCode, wparam, lparam);
std::ofstream outfile;
CBT_CREATEWND *CBTHOOKCREATE;
RECT *CBTRECTPTR;
RECT CBTRECT;
wstring Message;
CBTHOOKCREATE = (CBT_CREATEWND*) lparam;
LPWSTR str = L" ";
outfile.open(("d:\\test.txt"), std::ios_base::app);
if (nCode >= 0) {
switch (nCode)
{
case HCBT_CREATEWND:
outfile << *(CBTHOOKCREATE->lpcs->lpszName) << " " << CBTHOOKCREATE->lpcs->lpszName << " Created!~ " << endl;
//cout << "Created!~" << endl;
break;
case HCBT_DESTROYWND:
outfile << "Destroied!~" << endl;
//cout << "Destroied!~" << endl;
break;
default:
//cout << "sth else" << endl;
break;
}
}
outfile.close();
return 0;
}
void InstallHook(void)
{
hookID = SetWindowsHookEx(WH_CBT, HookProcedure, currentProcessHandle, 0);
}
void UninstallHook(void)
{
UnhookWindowsHookEx(hookID);
}
掛鉤消費類控制檯應用程序
// Hook Executer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "..\Dll\dllapi.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int num = -1;
cout << "1.Install Hook"<<endl
<< "2.Unistall Hook"<<endl
<< "0.Exit";
do{
cin >> num;
if (num ==1)
{
InstallHook();
}
else
{
UninstallHook();
}
getchar();
system("cls");
cout << "1.Install Hook" << endl
<< "2.Unistall Hook" << endl
<< "0.Exit";
} while (num != 0 && num < 3);
return 0;
}