Sometimes, we need to run only one instance of our program, the author of their own procedures have such a situation, so I have to explore some. After a while, finally small harvest, do not dare to enjoy, in pole published for your reference.
Since it is fundamentally to solve the problem, for Windows programs, from the WinMain function portal, this is because the SDK in the VC way to write the most transparent program, and WinMain is as the VC compiler to generate EXE file default entry function.
WinMain's function prototype:
int WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
);
There are four parameters in WinMain, where the second parameter hprevinstance is a hinstance that represents the handle of an instance of the program running. This parameter is always null on the WIN32 system, as explained by MSDN. However, we can also detect whether another instance already exists by using the Greatemutex function to create a uniquely named mutex object.
Greatemutex's function prototype:
HANDLE CreateMutex(
LPSECURITY_ATTRIBUTES lpMutexAttributes,
BOOL bInitialOwner,
LPCTSTR lpName
);
The first parameter, Lpmutexattributes, points to a pointer to a security_attributes structure that determines whether the handle that the child process inherits from the function is allowed to inherit. If this parameter is null (empty), it is not allowed to be inherited.
The second parameter binitialowner, if this argument is true and is created by the caller (referring to the CreateMutex function), then the invoked thread (the thread that invoked the CreateMutex function) obtains the original ownership. In addition, the invoked thread does not get the original ownership. To determine whether a mutex is created by the caller, see the Return Values section.
The third function, lpname, points to a null-terminated string that is used to name the mutex, and the number of characters in the name is limited to the MAX_PATH number character. At the same time this name is case-sensitive. If the parameter lpname is null (empty), an unnamed mutex is created. If the argument lpname the string to which it points and any of the following: existing event,semaphore,waitable timer,job,or file-mapping object, The function will fail with the call (as soon as possible) and the GetLastError function will return the ERROR_INVALID_HANDLE constant. This result is caused by the allocation of duplicate namespaces for these mutexes (these can cause duplicate namespaces to be viewed in MSDN through Search CreateMutex).
Returns a value that, if the function succeeds, the return value is a handle to a newly created mutex. If the function fails, the return value is null (empty). If the resulting mutex is a mutex that is named before this function (before the CreateMutex function is called) and exists, then the return value is the handle of the existing object, and the call to the GetLastError function (as soon as possible) returns Error_already _exists. In any case, when the second caller (note 1) has limited access, CreateMutex will fail, along with the use of GetLastError to return error_access_denied, Then the caller should use the OpenMutex function (more information can be viewed in MSDN, which is now sufficient to ensure that our program runs only one instance).