貌似很简单,但是我把程序抄了一遍,但是不能运行。源代码如下#include <windows.h>HANDLE Handle; //int main() int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance HINSTANCE hPrevInstance, // handle to previous instance LPSTR lpCmdLine, // pointer to command line int nCmdShow // show state of window ) {
HANDLE m_hMutex=CreateMutex(NULL,FALSE, m_pszAppNameXXXXX);
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
AfxMessageBox("程序已经在运行!");
return FALSE;
}
Handle = CreateMutex(NULL, TRUE, _T("互斥对象名"));
if (Handle == NULL)
return FALSE;
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
AfxMessageBox("程序已运行!", MB_ICONSTOP);
return FALSE;
}
但有个问题要注意:如果原来的程序在服务等以system权限的进程中运行,新的进程以exe来运行,这样是不行的,需要创建一个具有everyone权限的对象。
//int main()
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
)
{
Handle = CreateMutex(NULL, TRUE, "互斥对象名");
if (Handle == NULL)
return FALSE;
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
AfxMessageBox("程序已运行!");
return FALSE;
}
return 0;}错误如下:
E:\mydoc_jhs\编程\JUOne\JustOne.cpp(19) : error C2065: 'AfxMessageBox' : undeclared identifier问题可能是很简单,但是我还没有试过Windows编程,不会改。