Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/articles/win32-tutorials/)
-   -   Single Instance Application (http://www.go4expert.com/articles/single-instance-application-t4681/)

shabbir 14Jun2007 13:06

Single Instance Application
 
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: CPP

#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.

parvez.yu 6Mar2008 14:52

Re: Single Instance Application
 
i many a times faced problem now understood what to do


All times are GMT +5.5. The time now is 05:57.