Corrected source code for windows app posted earlier to run a command 
from java without
opening a DOS window.

Example: To run command "doit argument", you would instead use:

nodos doit argument

-- 

Erb

==============================================================
"The only time I like in the morning is afternoon."
     - Russell D. Cooper

"If you do everything, then you're all done."
     - Melissa F. Cooper

"Most of you are familiar with the virtues of a programmer.
There are three, of course: laziness, impatience, and hubris."
     - Larry Wall
==============================================================
#include <windows.h>
#include <process.h>

static void getCmdName(LPSTR cmdLine, PCHAR cmdName) {
        PCHAR src = cmdLine;
        PCHAR dest = cmdName;
    BOOL count_space = TRUE;
        // Check for, and strip, quotes.
    while (TRUE)
        {
                if (*src == NULL) {
                        break;
                }
                else if (*src == '"') {
                        count_space = !count_space;
                        src++;
                }
                else if (count_space && *src == ' ') {
                        break;
                }
                else {
                        *dest++ = *src++;
                }
        }  // while

        *dest = 0;
}
        
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow) {
        PROCESS_INFORMATION pi;
    STARTUPINFO         si;

    memset( &si, 0, sizeof( si ) );
    si.cb = sizeof(si);
    si.dwFillAttribute = 0;
    si.dwFlags = STARTF_USESTDHANDLES;
    si.wShowWindow = SW_HIDE;
    si.hStdInput = ::GetStdHandle( STD_INPUT_HANDLE );
    si.hStdOutput = ::GetStdHandle( STD_OUTPUT_HANDLE );
    si.hStdError = ::GetStdHandle( STD_ERROR_HANDLE );

        // Get first arg, the command name:
        CHAR cmdName[MAX_PATH+1];
        getCmdName(lpCmdLine, cmdName);
        if (*cmdName == NULL) // nothing!
                return -1;

        // Search for separator.
        PCHAR start = cmdName;
        while (*start != NULL && *start != '\\')
                start++;
        // If found one, this is a path.
        // Otherwise, search.
        TCHAR fullName[MAX_PATH+1];
        if (*start != NULL) {
                // Check existence.
                if (GetFileAttributes(cmdName) < 0)
                        return -1;
                lstrcpy(fullName, cmdName);
        }
        else {
                DWORD len = SearchPath(NULL, cmdName, ".exe", MAX_PATH+1, fullName, 
NULL);
                if (len == 0)
                        len = SearchPath(NULL, cmdName, ".bat", MAX_PATH+1, fullName, 
NULL);
                if (len == 0)
                        len = SearchPath(NULL, cmdName, ".cmd", MAX_PATH+1, fullName, 
NULL);
                if (len == 0)
                        return -1;
        }

        // Hide window for Win32 CUI only.
    DWORD type;
        BOOL ret = GetBinaryType(fullName, &type);
        if (ret) {
                if (type == SCS_32BIT_BINARY) {
                        HANDLE file = CreateFile(
                                fullName, GENERIC_READ,
                                FILE_SHARE_READ, NULL,
                                OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

                        if (file) {
                                SetFilePointer(file, 60, NULL, FILE_BEGIN);
                                LONG offset;
                                DWORD dummy;
                                ReadFile(file, &offset, 4, &dummy, NULL);
                                SetFilePointer(file, offset, NULL, FILE_BEGIN);
                                IMAGE_NT_HEADERS32 headers;
                                ReadFile(file, &headers, sizeof(headers), &dummy, 
NULL);
                                if ( headers.OptionalHeader.Subsystem == 
IMAGE_SUBSYSTEM_WINDOWS_CUI)
                                    si.dwFlags |= STARTF_USESHOWWINDOW;
                                CloseHandle(file);
                        }
                }
        }
        
        BOOL ok = CreateProcess( NULL, lpCmdLine, NULL, NULL, 
                TRUE, 0, NULL, NULL, &si, &pi );

    if( !ok ) return -1;
    DWORD result = WaitForSingleObject
                (pi.hProcess,INFINITE);
    switch( result ) {
                case WAIT_FAILED:
                case WAIT_ABANDONED:
                case WAIT_TIMEOUT:
                return -1;
                default:
                        GetExitCodeProcess( pi.hProcess, &result );
                        return result;
        }
}

Reply via email to