每隔x秒从.exe内部调用函数.exe

时间:2014-06-10 19:33:54

标签: c++ dll

我已经用C ++编写了一个.dll来进行一些检查(如果打开窗口,如果运行就杀掉进程等)。它导出了一个函数。此函数导入到.exe。当我启动exe时,一切都很顺利! 但是我想问一下,有没有办法每隔x秒循环一次dll函数,以便它可以一直执行检查?

提前谢谢!

void killMe(const char *filename)
{
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof (pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes)
{
    if (strcmp(pEntry.szExeFile, filename) == 0)
    {
        HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
            (DWORD)pEntry.th32ProcessID);
        if (hProcess != NULL)
        {
            TerminateProcess(hProcess, 9);
            CloseHandle(hProcess);
        }
    }
    hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
}



extern "C" __declspec(dllexport)
void Chek(void) {

for (int x = 0; x < 1; x++){
    HWND Find = FindWindow(0, "NotePad");
    if (Find != 0){
        killMe("notepad.exe");
        Chek();
    }
}

}

BOOL APIENTRY DllMain(HMODULE hModule,
DWORD _reason,
LPVOID lpReserved
){

switch (_reason)
{
case DLL_PROCESS_ATTACH:

    Chek();



case DLL_THREAD_ATTACH:

case DLL_THREAD_DETACH:

case DLL_PROCESS_DETACH:

    break;
}

return TRUE;
}

1 个答案:

答案 0 :(得分:0)

您可以使用计时器或在线程中执行此操作。例如,您可以在DllLoad上创建一个线程,dll可以是这样的:

while( continueKilling )
{
    killMe( "test" );
    Sleep( 5000 ); // 5 seconds
}