Single Instance Application

Discussion in 'Win32' started by shabbir, Jun 14, 2007.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    There are 2 ways to make your Application Single Instance.

    1. Close the previously running executable
    2. Close the new instance with passing necessary info from the older version.

    First option is not preferred because you may have lots of information already accumulated in the running executable and so its always preferred to go with the second option.

    Here is the code for it.
    Code:
    #define MAX_LOADSTRING 100
    #define WM_USER_MESSAGE WM_USER + 1
    
    #define OPTION_2
    
    #ifndef OPTION_2
    	#define OPTION_1
    #endif
    
    
    #include <windows.h>
    
    TCHAR szWindowClass[MAX_LOADSTRING] = "Single Instance";
    TCHAR szTextToDisplay[MAX_LOADSTRING] = "Single Instance Application in Win32";
    
    ATOM MyRegisterClass(HINSTANCE hInstance);
    BOOL InitInstance(HINSTANCE, int);
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    	MSG msg;
    
    	HWND hPrevWnd = FindWindow(szWindowClass,NULL);
    
    	if(hPrevWnd)
    	{
    #ifdef OPTION_1
    		// Closes the first Instance
    		::SendMessage(hPrevWnd,WM_CLOSE,NULL,NULL);
    #endif
    
    #ifdef OPTION_2
    		// Send a user message to the previous window
    		::SendMessage(hPrevWnd,WM_USER_MESSAGE,NULL,NULL);
    		return FALSE;
    #endif
    	}
    
    	MyRegisterClass(hInstance);
    
    	if (!InitInstance (hInstance, nCmdShow)) 
    	{
    		return FALSE;
    	}
    
    	while (GetMessage(&msg, NULL, 0, 0)) 
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }
    
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX wcex;
    
    	wcex.cbSize = sizeof(WNDCLASSEX); 
    
    	wcex.style			= CS_HREDRAW | CS_VREDRAW;
    	wcex.lpfnWndProc	= (WNDPROC)WndProc;
    	wcex.cbClsExtra		= 0;
    	wcex.cbWndExtra		= 0;
    	wcex.hInstance		= hInstance;
    	wcex.hIcon			= NULL;
    	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
    	wcex.lpszMenuName	= NULL;
    	wcex.lpszClassName	= szWindowClass;
    	wcex.hIconSm		= NULL;
    
    	return RegisterClassEx(&wcex);
    }
    
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;
    
       hWnd = CreateWindow(szWindowClass, szWindowClass, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    
       if (!hWnd)
       {
          return FALSE;
       }
    
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    
       return TRUE;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	PAINTSTRUCT ps;
    	HDC hdc;
    
    	switch (message) 
    	{
    		case WM_USER_MESSAGE:
    			MessageBox(hWnd,"WM_USER_MESSAGE Recieved from the Next Instance. ",szWindowClass,MB_OK);
    			break;
    		case WM_PAINT:
    			hdc = BeginPaint(hWnd, &ps);
    			// TODO: Add any drawing code here...
    			RECT rt;
    			GetClientRect(hWnd, &rt);
    			DrawText(hdc, szTextToDisplay, strlen(szTextToDisplay), &rt, DT_CENTER);
    			EndPaint(hWnd, &ps);
    			break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
       }
       return 0;
    }
    
    You can switch from Option 2 to Option 1 Just comment out #define OPTION_2

    Now with WM_USER_MESSAGE you can send the data in lParam, wParam you need to send to the previous window to update itself.
     
  2. parvez.yu

    parvez.yu New Member

    Joined:
    Feb 14, 2008
    Messages:
    100
    Likes Received:
    0
    Trophy Points:
    0
    i many a times faced problem now understood what to do
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice