CBT钩子实现解决程序间的冲突

可能在运行一个程序A时,将运行的程序与正在运行的某个程序B冲突。下面用一个CBT钩子实现当一个程序A启动时,关闭与本程序冲突的其它程序B

1、  首先写一个动态链接库CBTHooks。它实现一个全局CBT钩子监视程序A的启动(实际上是监视程序A中某窗口类的创建)。

2、  创建一个名为TestHookMFC EXE程序来为系统安装钩子。
CBTHooks

 

声明全局变量:

static HHOOK g_hHook = NULL;

static HANDLE g_hModule = NULL;

 

钩子过程:

LRESULT CALLBACK CbtProc(int nCode, WPARAM wParam, LPARAM lParam)

{

                  switch (nCode)

                  {

           case HCBT_CREATEWND:

           {

                    HWND hWnd = (HWND)wParam;

                    LPCBT_CREATEWND pcbt = (LPCBT_CREATEWND)lParam;

                    LPCREATESTRUCT pcs = pcbt->lpcs;

 

                    char clsName[255];

//从配置文件读取程序A中窗口的类名

                    char *winName = GetValFromINI("classname");

                    GetClassName(hWnd, clsName,255);

                    if(strcmp(clsName, winName) == 0)

                    {

                             //从配置文件读取程序B的文件绝对路径

                             char *ExeName = GetValFromINI("exe");

                             //关闭程序B的进程

                             KillProcByExe( ExeName);

                    }

                    break;

           }

}

return CallNextHookEx(g_hHook, nCode, wParam, lParam);

}

 

导出安装、卸载钩子函数:

//安装钩子

CBTHOOKS_API void InstallHook(void)

{

                  g_hHook = SetWindowsHookEx(WH_CBT, CbtProc, (HMODULE)g_hModule, 0);

}

        

 

//卸载钩子

CBTHOOKS_API void UninstallHook(void)

{

UnhookWindowsHookEx((HHOOK)g_hHook);

}

 

注:#define CBTHOOKS_API extern "C" __declspec(dllexport)

 

其它函数:

//杀死EXE相关进程

BOOL KillProcByExe( char* KillExe)

{

HANDLE hProcessSnap;

HANDLE hProcess;

PROCESSENTRY32 pe32;

DWORD dwPriorityClass = 0;

//创建进程快照

hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

if( hProcessSnap == INVALID_HANDLE_VALUE )

{

           return( FALSE );

}

pe32.dwSize = sizeof( PROCESSENTRY32 );

if( !Process32First( hProcessSnap, &pe32 ) )

{

           CloseHandle( hProcessSnap );     // Must clean up the snapshot object!

           return( FALSE );

}

//枚举进程

do

{

           if(GetProcessModules(pe32.th32ProcessID, KillExe))

           {

                    hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );

                    if( hProcess != NULL )

                    {

                             TerminateProcess(hProcess, 0);

                    }

                   

                    //CloseHandle( hProcessSnap );

                    //return( TRUE );

           }

} while( Process32Next( hProcessSnap, &pe32 ) );

CloseHandle( hProcessSnap );

return( TRUE );

}

 

//得到进程相关可执行文件位置

BOOL GetProcessModules( DWORD dwPID , char* KillExe)

{

HANDLE hModuleSnap = INVALID_HANDLE_VALUE;

MODULEENTRY32 me32;

//创建模块快照

hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );

if( hModuleSnap == INVALID_HANDLE_VALUE )

{

           return( FALSE );

}

// Set the size of the structure before using it.

me32.dwSize = sizeof( MODULEENTRY32 );

// Retrieve information about the first module,

// and exit if unsuccessful

if( !Module32First( hModuleSnap, &me32 ) )

{

           CloseHandle( hModuleSnap );     // Must clean up the snapshot object!

           return( FALSE );

}

if(strncmp(me32.szExePath, KillExe, sizeof(KillExe)) == 0)

           return( TRUE);

CloseHandle( hModuleSnap );

return( FALSE );

}

 

 

//读取配置文件

char* GetValFromINI(const char* KeyName)

{

//得到配置文件路径

char chrModule[1024];

GetModuleFileName((HMODULE)g_hModule, chrModule, sizeof(chrModule));

char* p = strrchr(chrModule, '//');

if(p != NULL)

{

           *(p+1) = '/0';

}

strcat(chrModule, "hook.ini");

//获得变量值

static char chrVal[1024];

GetPrivateProfileString( "hook", KeyName, NULL, chrVal, 1024, chrModule);

return chrVal;

}

 

TestHook:

声明函数指针:

typedef void (*PUninstallHook)();

typedef void (*PInstallHook)();

PInstallHook InstallHook;

PUninstallHook UninstallHook;

安装钩子:

void CTestHookDlg::OnButton1()

{

HMODULE hMod = LoadLibrary(_T("CBTHooks.dll"));

if(hMod != NULL)

{

           InstallHook = (PInstallHook)GetProcAddress(hMod, "InstallHook");

           if(InstallHook)

                    InstallHook();

}

}

卸载钩子:

void CTestHookDlg::OnButton2()

{

HMODULE hMod = LoadLibrary(_T("CBTHooks.dll"));

if(hMod != NULL)

{

           UninstallHook = (PInstallHook)GetProcAddress(hMod, "UninstallHook");

           if(UninstallHook)

                    UninstallHook();

}

}

 

你可能感兴趣的:(编程语言)