我现在要做一个基于MFC的小程序,按一下“运行”按钮就检查系统的进程是否存在abc.exe这个进程,若不存在则启动abc.exe,若存在则不做启动动作。我上网查到CreateMutex可以创建互斥事件,可能是我方法的问题,就是好像没反应。
我上网看过msdn,其中有一段代码是这样的:#include <windows.h>
#include <stdio.h>
#include <conio.h>// This process creates the mutex object.int main(void)
{
HANDLE hMutex; hMutex = CreateMutex(
NULL, // default security descriptor
FALSE, // mutex not owned
TEXT("abc.exe")); // object name if (hMutex == NULL)
printf("CreateMutex error: %d\n", GetLastError() );
else
if ( GetLastError() == ERROR_ALREADY_EXISTS )
printf("CreateMutex opened an existing mutex\n");
else printf("CreateMutex created a new mutex.\n"); // Keep this process around until the second process is run _getch(); CloseHandle(hMutex); return 0;
}但是无论abc.exe是否已经运行,都是输出CreateMutex created a new mutex.我的按钮代码如下void CRun_exeDlg::OnRunButton()
{
// TODO: Add your control notification handler code here HANDLE hMutex;
hMutex = CreateMutex(
NULL, // default security descriptor
FALSE, // mutex not owned
TEXT("abc.exe")); // object name
if ( GetLastError() == ERROR_ALREADY_EXISTS )
MessageBox("程序已经运行!");
else MessageBox("程序没有运行!");
CloseHandle(hMutex);
}
但是都是弹出程序没有运行~我不知道哪里写错了,请各位大虾不吝赐教~~!
我上网看过msdn,其中有一段代码是这样的:#include <windows.h>
#include <stdio.h>
#include <conio.h>// This process creates the mutex object.int main(void)
{
HANDLE hMutex; hMutex = CreateMutex(
NULL, // default security descriptor
FALSE, // mutex not owned
TEXT("abc.exe")); // object name if (hMutex == NULL)
printf("CreateMutex error: %d\n", GetLastError() );
else
if ( GetLastError() == ERROR_ALREADY_EXISTS )
printf("CreateMutex opened an existing mutex\n");
else printf("CreateMutex created a new mutex.\n"); // Keep this process around until the second process is run _getch(); CloseHandle(hMutex); return 0;
}但是无论abc.exe是否已经运行,都是输出CreateMutex created a new mutex.我的按钮代码如下void CRun_exeDlg::OnRunButton()
{
// TODO: Add your control notification handler code here HANDLE hMutex;
hMutex = CreateMutex(
NULL, // default security descriptor
FALSE, // mutex not owned
TEXT("abc.exe")); // object name
if ( GetLastError() == ERROR_ALREADY_EXISTS )
MessageBox("程序已经运行!");
else MessageBox("程序没有运行!");
CloseHandle(hMutex);
}
但是都是弹出程序没有运行~我不知道哪里写错了,请各位大虾不吝赐教~~!
============
这个去掉
if(hSem) //信标对象创建成功。
{
//信标对象已经存在,则程序已有一个实例在运行。
if(ERROR_ALREADY_EXISTS==GetLastError())
{
CloseHandle(hSem); //关闭信号量句柄。
printf("CreateSemaphore opened an existing Semaphore\n");
}
else
{
printf("CreateSemaphore created a new Semaphore.\n");
}
}上面这段代码可以,如果用mutex确不行,神奇,看来对内核对像还是不了解
NULL, // default security descriptor
FALSE, // mutex not owned
TEXT("abc.exe")); // object name
楼主,你的 第二个参数要用TRUE才可以的哦!
的确啊,我对内核对象不怎么了解。菜鸟一个,呜呜呜~
刚刚我吧楼上的代码复制到button里面,但是现象还是我之前描述的那样,打开这个小程序后,无论我是否运行了abc.exe,我第一次点击按钮会弹出"CreateSemaphore created a new Semaphore",再次按就是"CreateSemaphore created a new Semaphore."
NULL, // default security descriptor
FALSE, // mutex not owned
TEXT("abc.exe")); // object name
楼主,你的 第二个参数要用TRUE才可以的哦!第二个参数的意义是,如创建进程希望立即拥有互斥体,则设为TRUE。一个互斥体同时只能由一个线程拥有
就是A和B两个软件,A软件里面有个“运行”按钮。点击“运行”按钮后,判断B软件是否已经正在运行了,如果已经运行了,则不再次启动B软件,否则就启动B软件~说白了就是防止B软件多重运行~但是前提是我已经不能对B软件进行修改了~只能改A软件~谢谢~~~~~~
/***********************************************/
/* funtion:进程是否存在
/* parameters:
/* returns:void
/***********************************************/
BOOL ExistProcess()
{
BOOL judge = FALSE;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32); //获得系统内所有进程快照 HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
WriteToLog("CreateToolhelp32Snapshot error");
return 0;
} //枚举列表中的第一个进程
BOOL bProcess = Process32First(hProcessSnap, &pe32);
while (bProcess)
{
//比较找到的进程名和我们要查找的进程名,一样则返回进程ID
if (strcmp(strupr(ExeName),strupr(pe32.szExeFile))== 0 )
judge = TRUE;
bProcess = Process32Next(hProcessSnap, &pe32); //继续查找
} CloseHandle(hProcessSnap); return judge;
}
=========
你想检查B程序是否已经运行,最简单的方法是用枚举进程:
HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
return FALSE;
}PROCESSENTRY32 pe32 = {0};
pe32.dwSize = sizeof(PROCESSENTRY32); // 遍历拍下来的所有进程
if (Process32First(hProcessSnap, &pe32))
{
do
{
if (pe32.th32ProcessID && strcmp(pe32.szExeFile, "abc.exe"))
{
//说明abc.exe已经运行
}
} while (Process32Next(hProcessSnap, &pe32));
}
CloseHandle(hProcessSnap); return TRUE;
}
哪你怎知它CreateMutex了,还有Mutex名为"abc.exe"呢?莫非调试过了abc.exe?
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
//AfxMessageBox("程序已经运行!",MB_ICONSTOP|MB_OK);
return FALSE;
}
我想问下那个"Watch"可以是我在“任务管理器”-“线程”里面看到的名称吗?如“QQ.exe”
谢了~
void CRun_exe2Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
HANDLE toolhelp=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32 processinfo;
int i=0;
CString str;
CString str2="";
BOOL start=Process32First(toolhelp,&processinfo);
while(start)
{
str2+=processinfo.szExeFile;
start=Process32Next(toolhelp,&processinfo);
}
if (str2.Find("abc.exe")==-1)
{
AfxMessageBox("abc.exe没用运行,按确定运行");
ShellExecute(this->m_hWnd,"open","c:\\abc.exe","","", SW_SHOW );
}
else
{
AfxMessageBox("abc.exe已经运行");
}
}用快照的办法完成了,谢谢各位~~