#include <stdio.h>
#include <Windows.h>
#pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" )
static BOOL mkProc(const char *path, LPSTR cmd) {
STARTUPINFO info={sizeof(info)};
PROCESS_INFORMATION processInfo;
return CreateProcess(path, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo);
}
static const char *commands[] = {
"C:\\Program Files\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe",
"C:\\Program Files\\Tencent\\QQMusic\\QQMusic.exe",
NULL
};
BOOL check_exsit(LPSTR test){
HANDLE m_hMutex = CreateMutex(NULL, FALSE, test); //创建互斥量
// 如果程序已经存在并且正在运行
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(m_hMutex);
m_hMutex = NULL;
return true;
}
return false;
}
int main(int argc, char **argv) {
for (int i = 0; commands[i]; ++i) {
LPSTR cmd = (LPSTR) commands[i];
if(check_exsit(cmd)){
//激活已经打开的窗口
}
else{
if (!mkProc(NULL, cmd)) {
fprintf(stderr, "ERROR: cannot spawn %s\n", cmd);
}
}
}
return 0;
}
HANDLE m_hMutex = CreateMutex(NULL, TRUE, test); //创建互斥量
// 如果程序已经存在并且正在运行 if (GetLastError() == ERRORALREADYEXISTS) {
} return false; }