根据进程获得exe名称

#include<windows.h>

//定义出错类型,列举所有出错提示框的信息
const TCHAR *szErrorInfo[] = 
{
    _T("致命的应用程序错误"),
    _T("应用程序错误"),
    _T("Fatal Application Error"),
    _T("Application Error"),
    NULL,
};

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
    HANDLE hMutex = CreateMutex(NULL,FALSE,_T("SAVIOR"));
    if (hMutex)
    {
        if(ERROR_ALREADY_EXISTS == GetLastError())
        {
            return FALSE;
        }
    }

    TCHAR szPath[256];
    while (1)
    {
        HWND hWnd = NULL;        
        for (int i = 0;szErrorInfo[i];i++)
        {
            if (szErrorInfo[i])
            {
                hWnd = FindWindow(NULL,szErrorInfo[i]);
                if (hWnd)
                {
                    //查找到出错提示对话框
                    DWORD dwProcessID;
                    
                    //获取出错进程ID
                    GetWindowThreadProcessId(hWnd,&dwProcessID);
                    
                    //获取出错进程句柄
                    HMODULE hProc = (HMODULE)OpenProcess(0,FALSE,dwProcessID);
                    if (hProc)
                    {
                        //获取出错进程对应的EXE
                        GetModuleFileName(hProc,szPath,255);
                    }
                    
                    //关闭出错提示对话框
                    SendMessage(hWnd,WM_CLOSE, 0, 0);
                    Sleep(3000);

                    //重新启动出错应用程序
                    PROCESS_INFORMATION pi;    
                    if (CreateProcess(szPath,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&pi))
                    {
                        CloseHandle(pi.hProcess);
                    }
                }
            }
            Sleep(100);
        }        
        Sleep(1500);
    }
    return TRUE;
}

  

posted on 2014-02-26 15:22  涛涛宝贝  阅读(409)  评论(0编辑  收藏  举报

导航