15.1 Starting a Control Panel Application

There are three ways to start a Control Panel application:

The user can open Control Panel and start an application by double-clicking the application icon.

The user or an application can open Control Panel by using a command-line argument that specifies the name of the application to start. When the Control Panel application closes, Control Panel automatically closes.

An application can send a WM_CPL_LAUNCH message to Control Panel while Control Panel is running. When the Control Panel application closes, Control Panel sends back a WM_CPL_LAUNCHED confirmation message. For more information about these messages, see the Microsoft Windows Programmer's Reference, Volume 3.

The following example shows how an application can start Control Panel and the Printers application from the command line by using the WinExec function:

WinExec("control.exe printers", SW_SHOWNORMAL)

When Control Panel starts, it immediately displays the Printers application. After the Printers application finishes, Control Panel ends.

The following example shows a function that starts a Control Panel application by using the WM_CPL_LAUNCH message:

BOOL StartApplet(LPSTR lpszName, HWND hwndMine)
{
    HANDLE hAppletName;        /* global-object handle for app name   */
    HWND hwndCPL;              /* handle of Control Panel window      */
    LPSTR lpszAppletName;      /* name of the application             */
    BOOL  fStartedCPL = FALSE; /* application started by CONTROL.EXE? */

    /*
     * Allocate a global, sharable memory block to hold the
     * application-name string.
     */

    hAppletName = GlobalAlloc(GMEM_MOVEABLE | GMEM_NOT_BANKED,
        lstrlen(lpszName) + 1);
    if(hAppletName == (HANDLE) NULL)
        return FALSE;
    lpszAppletName = GlobalLock(hAppletName);
    lstrcpy(lpszAppletName, lpszName);
    GlobalUnlock(hAppletName);

    /*
     * Get the Control Panel window handle and start Control Panel, if
     * necessary.
     */

    if((hwndCPL = FindWindow((LPSTR) "CtlPanelClass",
            (LPSTR) "Control Panel")) == (HWND) NULL) {
        WinExec("control.exe", SW_SHOWNA);
        hwndCPL = FindWindow((LPSTR) "CtlPanelClass",
            (LPSTR) "Control Panel");
       



      if(!hwndCPL) {
            GlobalFree(hAppletName);
            return FALSE;
        }
        fStartedCPL = TRUE;
    }

    /* Start the application and end Control Panel, if started. */

    SendMessage(hwndCPL, WM_CPL_LAUNCH, (WPARAM) hwndMine,
        (LPARAM) lpszAppletName);
    if(fStartedCPL)
        SendMessage(hwndCPL, WM_CLOSE, 0, 0L);
    GlobalFree(hAppletName);
    return TRUE;
}