通过SDK函数实现WIN32应用程序的唯一实例。

类别:VC语言 点击:0 评论:0 推荐:

//FORMMAIN.CPP

#include <windows.h>
#include "ExeMutex.h"

static LPCSTR szAppName = TEXT("HelloWin") ;

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

 


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
    
    
  if(!OperateMutex(szAppName,szAppName,szAppName,OPEN))
   {return FALSE;}
 
  HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
  
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

    RegisterClass(&wndclass);
    
     hwnd = CreateWindow (szAppName,                  // window class name
                          szAppName,      // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters
    
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;

 
    
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return 0 ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  
    
     switch (message)
     {
   
 
     case WM_DESTROY:
          OperateMutex(szAppName,szAppName,szAppName,CLOSE);
    PostQuitMessage (0);
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}
   
//头文件


/* 定义用于判断操作类型的常量 */
const bool OPEN = 1; 
const bool CLOSE = 0;

//函数声明
BOOL OperateMutex(LPCSTR,LPCTSTR,LPCTSTR,BOOL);
BOOL ActiveOldApp( LPCSTR,LPCSTR);
 
 
//判断当前应用程序运行实例是否存在的函数体
BOOL OperateMutex( LPCSTR InstanceName,          //自定义用于表示应用程序的唯一标识名
       LPCSTR hWndClassName,   //窗体的注册类名 CreateWindow 函数中指定
       LPCSTR hWndWinName,    //窗体名  CreateWindow 函数中指定
       BOOL mOperate)                //操作判断变量
 {
 HANDLE hMutex;
 
 hMutex = OpenMutex(MUTEX_ALL_ACCESS,FALSE,InstanceName);
    

 switch(mOperate)          
 {
 case 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 CLOSE:     //CLOSE = 退出程序
  ReleaseMutex(hMutex);
  CloseHandle(hMutex);
  break;
  }
 return TRUE;
 }


// 激活已启动的程序
BOOL ActiveOldApp( LPCSTR hWndClassName,   //窗体的注册类名 CreateWindow 函数中指定
       LPCSTR hWndWinName)           //窗体名  CreateWindow 函数中指定
{
 HWND hWndApp,hWndPopup;
    if (hWndApp = FindWindow(hWndClassName,hWndWinName))
 {
  hWndPopup = GetLastActivePopup(hWndApp);
        BringWindowToTop(hWndApp);
        if ( IsIconic(hWndPopup) )
   { ShowWindow(hWndPopup, SW_RESTORE); }
  else
   { SetForegroundWindow(hWndPopup); }
 }
 else
 {
  return FALSE;
 }
 return TRUE;
}

 

 

本文地址:http://com.8s8s.com/it/it3004.htm