int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    static char szAppName[] = "MyApp";
    static char szWndClass[] = "MyWindowClass";
    static char szMutexName[] = "MyMutex"; // Use a unique name!

    WNDCLASS wc;
    HWND hwnd;
    MSG msg;
    HANDLE hMutex;

    // Get a mutex handle and fail the current application instance if the
    // mutex existed before the call to CreateMutex.
    hMutex = CreateMutex (NULL, FALSE, szMutexName);
    if ((hMutex != NULL) && (GetLastError () == ERROR_ALREADY_EXISTS)) 
    {
        MessageBox (NULL, "Another instance of this application is running",
        "Error", MB_OK | MB_ICONSTOP);
        
        CloseHandle (hMutex);
        return -1;
    }
    
    wc.style = 0;
    wc.lpfnWndProc = (WNDPROC) WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = szWndClass;
    RegisterClass (&wc);

    hwnd = CreateWindow (szWndClass, szAppName,
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, HWND_DESKTOP, NULL, hInstance, NULL);
    
    ShowWindow (hwnd, nCmdShow);
    UpdateWindow (hwnd);
    
    while (GetMessage (&msg, NULL, 0, 0)) 
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
    }
    
    // Close the mutex handle so the system will know we're done with it.
    CloseHandle (hMutex);
    
    return msg.wParam;
}