C++ 判斷進程是否存在

來源:互聯網
上載者:User

標籤:原理   ready   tla   reg   dep   message   func   hot   popup   

原文:http://blog.csdn.net/u010803748/article/details/53927977?locationNum=2&fps=1


一、判斷指定程式名的進程是否存在

BOOL EnumWindows( WNDENUMPROC lpEnumFunc, // pointer to callback function LPARAM lParam // application-defined value);

The EnumWindows function enumerates all top-level windows on the screen by passing the handle to each window, in turn, to an application-defined callback function. EnumWindows continues until the last top-level window is enumerated or the callback function returns FALSE.

view plain

BOOL CALLBACK IpEnumFunc(HWND hwnd,LPARAM lParam)
{
char wndName[100];
::GetWindowText(hwnd,wndName,sizeof(wndName));
if(wndName!="")
{
if(strcmp(wndName,name1)==0)
{
WndHnd=hwnd;
flag=1;

}
}
return 1;
}



二、判斷指定進程名的進程是否存在



view plain

DWORD GetProcessidFromName(LPCTSTR name)
{
PROCESSENTRY32 pe;
DWORD id=0;
HANDLE hSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
pe.dwSize=sizeof(PROCESSENTRY32);
if(!Process32First(hSnapshot,&pe))
return 0;
while(1)
{
pe.dwSize=sizeof(PROCESSENTRY32);
if(Process32Next(hSnapshot,&pe)==FALSE)
break;
if(strcmp(pe.szExeFile,name)==0)
{
id=pe.th32ProcessID;

break;
}


}
CloseHandle(hSnapshot);
return id;
}

如果傳回值不為零,則存在,否則不存在。



三、VC判斷程式調用的外部進程是否結束



view plain

PROCESS_INFORMATION pi;
STARTUPINFO si;
memset(&si,0,sizeof(si));
si.cb=sizeof(si);
si.wShowWindow=SW_HIDE;
si.dwFlags=STARTF_USESHOWWINDOW;
bool fRet=CreateProcess(NULL,str.GetBuffer(str.GetLength()),NULL,FALSE,NULL,NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,NULL,NULL,&si,&pi);

///判斷

DWORD ExitCode;

ExitCode=STILL_ACTIVE;
while(ExitCode==STILL_ACTIVE)
{
GetExitCodeProcess(pi.hProcess,&ExitCode);
}

四、VC判斷進程是否存在?比如我想知道記事本是否運行,要用到哪些函數?

view plain

enProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,aProcesses[i]);
// 取得特定PID的進程名
if (hProcess )
{
if ( EnumProcessModules(hProcess,&hMod,sizeof(hMod), &cbNeeded))
{
GetModuleBaseName( hProcess, hMod,szProcessName,sizeof(szProcessName));
//將取得的進程名與輸入的進程名比較,如相同則返回進程PID
if(!stricmp(szProcessName, InputProcessName))
{
CloseHandle(hProcess);
return aProcesses[i];
}
}
}//end of if (hProcess)
}//end of for
//沒有找到相應的進程名,返回0
CloseHandle(hProcess);
return 0;
}

也可以枚舉得到所有進程的應用程式名稱,然後和知道應用程式名稱比較判斷。



五、實現程式只運行一次的方法

實現程式只運行一次的方法很多,但是原理都是一樣的,就是運行第一次的時候設定一個標記,每次啟動並執行時候檢查該標記,如果有就說明已經運行了。

具體實現:

1、在程式初始化的時候 (InitInstance()) 枚舉所有的視窗,尋找本程式的執行個體是否存在
2、在主視窗初始化的時候在本視窗的屬性列表中添加一個標記,以便程式尋找.


部分關鍵代碼 :

1、在App的InitInstance()中枚舉所有視窗,尋找本程式執行個體

view plain

HWND oldHWnd = NULL;
EnumWindows(EnumWndProc,(LPARAM)&oldHWnd); //枚舉所有啟動並執行視窗
if(oldHWnd != NULL)
{
AfxMessageBox( "本程式已經在運行了 ");
::ShowWindow(oldHWnd,SW_SHOWNORMAL); //啟用找到的前一個程式
::SetForegroundWindow(oldHWnd); //把它設為前景視窗
return false; //退出本次運行
}



2、添加EnumWndProc視窗過程函數://添加的標識只運行一次的屬性名稱

view plain

CString g_szPropName = "Your Prop Name "; //自己定義一個屬性名稱
HANDLE g_hValue = (HANDLE)1; //自己定義一個屬性值

BOOL CALLBACK EnumWndProc(HWND hwnd,LPARAM lParam)
{
HANDLE h = GetProp(hwnd,g_szPropName);
if( h == g_hValue)
{
*(HWND*)lParam = hwnd;
return false;
}
return true;
}

3、在主視窗的 OnInitDialog()中添加屬性 //設定視窗屬性
SetProp(m_hWnd,g_szPropName,g_hValue);

再次啟動時,先檢查當前存在的所有視窗,如果有標題相同的,則把先前啟動並執行視窗當成當前視窗
我的程式如下:

view plain

HWND hWnd_Exist;
hWnd_Exist=::GetDesktopWindow();
hWnd_Exist=::GetWindow(hWnd_Exist,GW_CHILD);
for(;;)
{
if(hWnd_Exist==NULL)
{
break;
}
char s[256];
memset(s,0,256);
::SendMessage(hWnd_Exist,WM_GETTEXT,255,(LONG)s);
if(strstr(s, "****** ")!=NULL)
break;
hWnd_Exist=::GetWindow(hWnd_Exist,GW_HWNDNEXT);
}

if(hWnd_Exist != NULL)
{
::ShowWindow(hWnd_Exist,SW_SHOWNORMAL);
::SetForegroundWindow(hWnd_Exist);
exit(0);
}


聲明一個全域 CMutex 變數:







CMutex mutexApp(FALSE, _T( "VPOS2000Server ")); //用此互斥量阻止多個執行個體

在你的 CWinApp 類的重載函數: InitInstance 中加入如下代碼:

view plain

if (!mutexApp.Lock(1))
return FALSE;
::CreateMutex(NULL, TRUE, m_pszExeName);
if(ERROR_ALREADY_EXISTS == GetLastError())
{
CWnd* pPrevHwnd = CWnd::GetDesktopWindow()-> GetWindow(GW_CHILD);
while(pPrevHwnd)
{
if(::GetProp(pPrevHwnd-> GetSafeHwnd(), m_pszExeName))
{
if(pPrevHwnd-> IsIconic())
{
pPrevHwnd-> ShowWindow(SW_RESTORE);
}

pPrevHwnd-> SetForegroundWindow();
pPrevHwnd-> GetLastActivePopup()-> SetForegroundWindow();
return FALSE;
}
pPrevHwnd = pPrevHwnd-> GetWindow(GW_HWNDNEXT);
}
TRACE( "Could not fond frevious instance main window ! ");
return FALSE;
}


建立一個全域的互斥量,每次啟動時檢查是否存在。





view plain

BOOL CRTDBApp::OnlyOneInstance()
{
if(::CreateMutex(NULL, TRUE, "onlyone ") == NULL )
{
TRACE0( "CreateMutex error. ");
return FALSE;
};
if( ::GetLastError() == ERROR_ALREADY_EXISTS) {

CWnd* pPrevWnd = CWnd::FindWindow(NULL, "onlyonehwnd ");
if(pPrevWnd)
{
if( pPrevWnd-> IsIconic())
pPrevWnd-> ShowWindow(SW_RESTORE);

pPrevWnd-> SetForegroundWindow();

pPrevWnd-> GetLastActivePopup()-> SetForegroundWindow();
return FALSE;
}

};

return TRUE;
}

C++ 判斷進程是否存在

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.