Wincode.c

The code in wincode.c creates the main window and message queue for AutoXL. This is a fairly typical window-management code module; the interesting portion of this code occurs in the WinMain function. Notice that it increases the size of the message queue and that it initializes and uninitializes OLE.

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpszCmd, int iCmdShow)
{
    MSG msg;
    int iMsg = 96;
    
    // for OLE, enlarge message queue to be as large as possible
    while (!SetMessageQueue(iMsg) && (iMsg -= 8));

    // various initialization, including OLE
    g_hInstApp = hInst;
    msg.wParam = FALSE;

    if (!InitApplication(hPrevInst))
        goto ExitApplication;

    if (!InitOLE())
        goto ExitApplication;

    if (!InitInstance(iCmdShow))
        goto ExitApplication;

    // message loop
    while (GetMessage(&msg, NULL, (unsigned int) NULL, (unsigned int)
        NULL)) 
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

ExitApplication:
    // make sure OLE is cleaned up if init was done
    if (g_fOLEInit)
        OleUninitialize();

    return msg.wParam;
}

BOOL InitOLE()
{
    DWORD dwOleVer;
    
    dwOleVer = CoBuildVersion();
    
    // check the OLE library version
    if (rmm != HIWORD(dwOleVer)) 
    {
        MessageBox(NULL, L"Incorrect version of OLE libraries.", 
            g_szAppTitle, MB_OK | MB_ICONSTOP);
        return FALSE;
    }
    
    // could also check for minor version, but this application is
    // not sensitive to the minor version of OLE
    
    // initialize OLE, fail application if we can't get OLE to init.
    if (FAILED(OleInitialize(NULL))) 
    {
        MessageBox(NULL, L"Cannot initialize OLE.", g_szAppTitle, 
            MB_OK | MB_ICONSTOP);
        return FALSE;
    }
    

    // otherwise, init succeeded
    g_fOLEInit = TRUE;
    
    return TRUE;
}