//守护进程程序里面函数
void ProtectProc()
{
HANDLE hMutex = OpenMutex( MUTEX_ALL_ACCESS, FALSE, _T("_EMS_Server_Mutex") );
if( hMutex )
{
CloseHandle( hMutex );
}
else
{
WinExec( ".\\Mysql_Config.bat", SW_SHOW ); //需要保护的程序(我这是个批处理文件)
//(以下部分为其他方法打开进程)
//WinExec( ".\\Mysql_Config.bat ", SW_SHOW );
//system( ".\\Mysql_Config.bat " );
//char sExePath[256] = ".\\Mysql_Config.bat -sys";
//STARTUPINFO si;
//PROCESS_INFORMATION pi;
//ZeroMemory(&si, sizeof(si));
//si.cb = sizeof(si);
//si.wShowWindow=SW_SHOW;
//si.dwFlags=STARTF_USESHOWWINDOW;
//ZeroMemory(&pi, sizeof(pi));
//CreateProcess((LPCWSTR)sExePath, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
}
}
SetTimer( hWnd, 1, 3000, NULL );
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
case WM_TIMER:
{
ProtectProc();
}break;
//受保护程序中代码
//------------------------------
HANDLE hMutex = CreateMutex( NULL, FALSE, _T("_EMS_Server_Mutex") );
if( hMutex )
{
if( GetLastError()==ERROR_ALREADY_EXISTS )
{
CloseHandle( hMutex );
printf("已有一个实例在运行");
return 0;
}
}