Determines whether the specified process or program has a method summary of vc etc
- 2020-04-01 21:34:45
- OfStack
First, determine whether the process with the specified program name exists
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.
Determines whether a process with the specified process name exists
If the return value is not zero, it exists, otherwise it does not exist.
VC to determine whether the external process called by the program is finished
Iv. Does VC judge the existence of the process? Let's say I want to know if notepad works, what functions do I use?
You can also enumerate the application names of all processes and compare them with the application names you know.
Five, the implementation of the program only run the method
There are many ways to implement a program that runs only once, but the principle is the same: set a flag the first time you run it, and check it every time you run it.
Specific implementation:
1. At program initialization time (InitInstance ()) Enumerate all the Windows to find if an instance of the program exists
2. When the main window is initialized, add a mark in the property list of this window for the program to find.
Some key codes:
1. Enumerate all Windows in the InitInstance() of the App to find the instance of the program
2. Add EnumWndProc window procedure function
3. In the main window OnInitDialog() adds the property // sets window properties
SetProp (m_hWnd, g_szPropName g_hValue);
When it starts again, check all the existing Windows, and if any have the same title, treat the previously run window as the current window
My procedure is as follows:
Declare a global & PI; CMutex Variables:
--------------------------------------------------------------------------------
CMutex MutexApp (FALSE, VPOS2000Server _T (" ")); // block multiple instances with this mutex
In your CWinApp Class overloaded function : InitInstance Add the following code :
Create a global mutex and check for its existence each time you start.
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.
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;
}
Determines whether a process with the specified process name exists
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;
}
If the return value is not zero, it exists, otherwise it does not exist.
VC to determine whether the external process called by the program is finished
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);
/// judgment
DWORD ExitCode;
ExitCode=STILL_ACTIVE;
while(ExitCode==STILL_ACTIVE)
{
GetExitCodeProcess(pi.hProcess,&ExitCode);
}
Iv. Does VC judge the existence of the process? Let's say I want to know if notepad works, what functions do I use?
enProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE,aProcesses[i]);
// Gets the process name & PI for a particular PID.
if (hProcess )
{
if ( EnumProcessModules(hProcess,&hMod,sizeof(hMod), &cbNeeded))
{
GetModuleBaseName( hProcess, hMod,szProcessName,sizeof(szProcessName));
//Compares the obtained process name with the input process name, and returns the process PID if the same;
if(!stricmp(szProcessName, InputProcessName))
{
CloseHandle(hProcess);
return aProcesses[i];
}
}
}//end of if (hProcess)
}//end of for
//No process name was found, return 0& PI;
CloseHandle(hProcess);
return 0;
}
You can also enumerate the application names of all processes and compare them with the application names you know.
Five, the implementation of the program only run the method
There are many ways to implement a program that runs only once, but the principle is the same: set a flag the first time you run it, and check it every time you run it.
Specific implementation:
1. At program initialization time (InitInstance ()) Enumerate all the Windows to find if an instance of the program exists
2. When the main window is initialized, add a mark in the property list of this window for the program to find.
Some key codes:
1. Enumerate all Windows in the InitInstance() of the App to find the instance of the program
HWND oldHWnd = NULL;
EnumWindows(EnumWndProc,(LPARAM)&oldHWnd); //Enumerate all running Windows
if(oldHWnd != NULL)
{
AfxMessageBox( " The program is already running ");
::ShowWindow(oldHWnd,SW_SHOWNORMAL); //Activate the previous program found
::SetForegroundWindow(oldHWnd); //Set it as the foreground window
return false; //Exit this operation
}
2. Add EnumWndProc window procedure function
CString g_szPropName = "Your Prop Name "; //Define a property name yourself
HANDLE g_hValue = (HANDLE)1; //Define an attribute value yourself
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. In the main window OnInitDialog() adds the property // sets window properties
SetProp (m_hWnd, g_szPropName g_hValue);
When it starts again, check all the existing Windows, and if any have the same title, treat the previously run window as the current window
My procedure is as follows:
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);
}
Declare a global & PI; CMutex Variables:
--------------------------------------------------------------------------------
CMutex MutexApp (FALSE, VPOS2000Server _T (" ")); // block multiple instances with this mutex
In your CWinApp Class overloaded function : InitInstance Add the following code :
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;
}
Create a global mutex and check for its existence each time you start.
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;
}