VC如何判断某进程是否在运行
1个回答
展开全部
//InstanceName 自定义用于表示应用程序的唯一标识名
//hWndClassName 窗体的注册类名 CreateWindow 函数中指定
//hWndWinName 窗体名 CreateWindow 函数中指定
//mOperate 操作判断变量
BOOL OperateMutex( LPCWSTR InstanceName,LPCWSTR hWndClassName, LPCWSTR hWndWinName, BOOL mOperate)
{
HANDLE hMutex;
hMutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,InstanceName);
switch(mOperate)
{
case APP_OPEN: //OPEN = 启动程序
if(hMutex==NULL)
{
hMutex = CreateMutex(NULL,FALSE,InstanceName);
if( (hMutex == NULL) || (GetLastError()==ERROR_ALREADY_EXISTS))
{
ActiveOldApp(hWndClassName,hWndWinName);
return FALSE; //程序已经启动或有错误发生
}
else
{
return TRUE;
} //第一次启动程序
}
else
{
ActiveOldApp(hWndClassName,hWndWinName);
return FALSE; //程序已经启动
}
break;
case APP_CLOSE: //CLOSE = 退出程序
ReleaseMutex(hMutex);
CloseHandle(hMutex);
break;
}
return TRUE;
}
//激活已启动的同一个程序
//参数:hWndClassName 窗体的注册类名 CreateWindow 函数中指定
// hWndWinName 窗体名 CreateWindow 函数中指定
BOOL ActiveOldApp( LPCWSTR hWndClassName, LPCWSTR hWndWinName)
{
HWND hWndApp,hWndPopup;
if (hWndApp = FindWindow(hWndClassName,hWndWinName))
{
//PutLog( "已经启动的实例的窗口句柄是%ld", hWndApp );
SendMessage( hWndApp, WM_ACTIVE, 0, 0 );
hWndPopup = GetLastActivePopup(hWndApp);
//PutLog( "GetLastActivePopup得到的窗口句柄是%ld", hWndPopup );
return TRUE;
}
else
{
return FALSE;
}
}
//hWndClassName 窗体的注册类名 CreateWindow 函数中指定
//hWndWinName 窗体名 CreateWindow 函数中指定
//mOperate 操作判断变量
BOOL OperateMutex( LPCWSTR InstanceName,LPCWSTR hWndClassName, LPCWSTR hWndWinName, BOOL mOperate)
{
HANDLE hMutex;
hMutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,InstanceName);
switch(mOperate)
{
case APP_OPEN: //OPEN = 启动程序
if(hMutex==NULL)
{
hMutex = CreateMutex(NULL,FALSE,InstanceName);
if( (hMutex == NULL) || (GetLastError()==ERROR_ALREADY_EXISTS))
{
ActiveOldApp(hWndClassName,hWndWinName);
return FALSE; //程序已经启动或有错误发生
}
else
{
return TRUE;
} //第一次启动程序
}
else
{
ActiveOldApp(hWndClassName,hWndWinName);
return FALSE; //程序已经启动
}
break;
case APP_CLOSE: //CLOSE = 退出程序
ReleaseMutex(hMutex);
CloseHandle(hMutex);
break;
}
return TRUE;
}
//激活已启动的同一个程序
//参数:hWndClassName 窗体的注册类名 CreateWindow 函数中指定
// hWndWinName 窗体名 CreateWindow 函数中指定
BOOL ActiveOldApp( LPCWSTR hWndClassName, LPCWSTR hWndWinName)
{
HWND hWndApp,hWndPopup;
if (hWndApp = FindWindow(hWndClassName,hWndWinName))
{
//PutLog( "已经启动的实例的窗口句柄是%ld", hWndApp );
SendMessage( hWndApp, WM_ACTIVE, 0, 0 );
hWndPopup = GetLastActivePopup(hWndApp);
//PutLog( "GetLastActivePopup得到的窗口句柄是%ld", hWndPopup );
return TRUE;
}
else
{
return FALSE;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询