This function is called by the system as the initial entry point for Windows CE-based applications.

int WINAPI WinMain(
  HINSTANCE hInstance, 
  HINSTANCE hPrevInstance, 
  LPWSTR lpCmdLine, 
  int nShowCmd 
); 

Parameters

hInstance
[in] Handle to the current instance of the application.
hPrevInstance
[in] Handle to the previous instance of the application. For a Win32-based application, this parameter is always NULL.

If you need to detect whether another instance already exists, create a uniquely named mutex using the CreateMutex function. CreateMutex will succeed even if the mutex already exists, but the GetLastError function will return ERROR_ALREADY_EXISTS. This indicates that another instance of your application exists, because it created the mutex first.

lpCmdLine
[in] Pointer to a null-terminated string that specifies the command line for the application, excluding the program name.
nShowCmd
[in] Specifies how the window is to be shown.

由msdn的解释可知,要保证只运行程序的一个实例,可以采用创建互斥对象的方法。根据函数GetLastError

的返回值可以判断是否已经有一个实例。

例如在程序的InitInstance函数开头部分加入如下语句:

BOOL CMulticardTestApp::InitInstance()
{
 // InitCommonControlsEx() is required on Windows XP if an application
 // manifest specifies use of ComCtl32.dll version 6 or later to enable
 // visual styles.  Otherwise, any window creation will fail.
 INITCOMMONCONTROLSEX InitCtrls;
 InitCtrls.dwSize = sizeof(InitCtrls);
 // Set this to include all the common control classes you want to use
 // in your application.
 InitCtrls.dwICC = ICC_WIN95_CLASSES;
 InitCommonControlsEx(&InitCtrls);

HANDLE  hMutex =::CreateMutex(NULL,FALSE,_T("Testapp"));

 if(GetLastError()==ERROR_ALREADY_EXISTS)
 {
  AfxMessageBox(IDS_ALREADY_EXISTS);
  return FALSE;
 }