User:Lucas Brooks/Researches/Windows 1.0 DR5 Programming

Programmatically Launch Apps[edit | edit source]

It is possible to programmatically run applications in Windows 1.0 DR5. This is done using the LoadModule API function.

HANDLE FAR PASCAL LoadModule(LPSTR lpszModName, LPSTR lpLoadBlock);

lpszModName is a far pointer to the module name string (without extension). lpLoadBlock is a far pointer to the module load block. This function returns a handle to the loaded module directly, and a handle to the main application window indirectly through the module load block.

Module Load Block[edit | edit source]

A module load block is a NEWPARMS structure (2 bytes followed by a null-terminated string). The first field is reserved for a HWND (handle to window) and the second field is the string of arguments to pass to the module. This load block will be passed directly to the LoadProc of the module to be loaded, which means on return, the hWnd field should contain a handle to the main application window if the module is a user application. It is up to you to set the visibility of that window (visible/hidden/iconic) through the ShowWindow function. The module load block must be passed as LPSTR, which means you will have to cast the LPNEWPARMS variable to LPSTR before calling LoadModule. It is suggested that you set the hWnd field of the load block to NULL before passing it to LoadModule, this way you can easily determine the validity of the HWND returned.

Example[edit | edit source]

HANDLE NEAR PASCAL RunApp(szName, szArgs, bIconic)
PSTR szName; /* name of module */
PSTR szArgs; /* arguments to pass */
BOOL bIconic; /* whether to start in iconic state */
{
    HANDLE hModule = (HANDLE)NULL;
    PNEWPARMS pLoadBlock = (PNEWPARMS)LocalAlloc(LPTR, sizeof(NEWPARMS) + lstrlen((LPSTR)szArgs));
    if (pLoadBlock)
    {
        pLoadBlock->hWnd = (HWND)NULL; /* not needed in this case because LPTR is used */
        lstrcpy((LPSTR)pLoadBlock->cmdLine, (LPSTR)szArgs);
        hModule = LoadModule((LPSTR)szName, (LPSTR)pLoadBlock);
        if (hModule && pLoadBlock->hWnd)
        {
            ShowWindow(pLoadBlock->hWnd, bIconic ? SHOW_ICONWINDOW : SHOW_OPENWINDOW);
        }
        Free((HANDLE)pLoadBlock);
    }
    return hModule;
}

>RunApp("FOO", "BAR 123", FALSE); /* runs "FOO.EXE BAR 123" normally */
>RunApp("FOO", "BAR 123", TRUE); /* runs "FOO.EXE BAR 123" in iconic mode */
>RunApp("FOO.EXE", "BAR 123", FALSE); /* invalid as FOO.EXE.EXE does not exist */