Console problem

This is a discussion on Console problem within the Windows Programming forums, part of the Platform Specific Boards category; How can I send data to a command prompt process created with CreateProcess and with hidden window? Also I want ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Console problem

    How can I send data to a command prompt process created with CreateProcess and with hidden window? Also I want to make it close the console program when it is finished with the command I gave it. This is the code i used to make a hidden command prompt, it is mostly copied from different places:
    Code:
    #include <windows.h>
    #include <string>
    std::string getExeName(std::string strFullPathToExe)
    {
        int Position = strFullPathToExe.find_last_of("\\");
        strFullPathToExe = strFullPathToExe.erase(0, Position +1);
    
        return strFullPathToExe;
    }
    int ExecuteProcess(std::string &FullPathToExe, std::string &Parameters, int SecondsToWait) 
    {
        int iMyCounter = 0, iReturnVal = 0;
        DWORD dwExitCode;
    
        /* - NOTE - You could check here to see if the exe even exists */
    
        /* Add a space to the beginning of the Parameters */
        if (Parameters.size() != 0 )
        { 
            Parameters.insert(0," ");
        }
    
        /* When using CreateProcess the first parameter needs to be the exe itself */
        Parameters = getExeName(FullPathToExe).append(Parameters);
    
        /* 
            The second parameter to CreateProcess can not be anything but a char !!
            Since we are wrapping this C function with strings, we will create
            the needed memory to hold the parameters
        */
    
        /* Dynamic Char */
        char * pszParam = new char[Parameters.size() + 1];
    
        /* Verify memory availability */
        if (pszParam == 0)
        {
            /* Unable to obtain (allocate) memory */
            return 1;
        } 
        const char* pchrTemp = Parameters.c_str();
        strcpy(pszParam, pchrTemp);
    
        /* CreateProcess API initialization */
        STARTUPINFO siStartupInfo;
        PROCESS_INFORMATION piProcessInfo;
        SECURITY_ATTRIBUTES as;
        memset(&siStartupInfo, 0, sizeof(siStartupInfo));
        memset(&piProcessInfo, 0, sizeof(piProcessInfo));
        memset(&as, 0, sizeof(as));
        as.lpSecurityDescriptor=NULL;
        as.bInheritHandle=true;
        as.nLength=sizeof(as);
        siStartupInfo.dwFlags = STARTF_USESHOWWINDOW  | STARTF_USESTDHANDLES;
        siStartupInfo.wShowWindow = SW_HIDE;
        siStartupInfo.cb = sizeof(siStartupInfo);
    
        /* Execute */
        if ((CreateProcess(FullPathToExe.c_str(), pszParam, 0, 0, true,
                                CREATE_DEFAULT_ERROR_MODE|CREATE_NO_WINDOW, 0, 0, &siStartupInfo,
                                &piProcessInfo)) != false)
        { 
           /* A loop to watch the process. Dismissed with SecondsToWait set to 0 */
           GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);
    
           while (dwExitCode == STILL_ACTIVE && SecondsToWait != 0)
           {
               GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);
               Sleep(500);
               iMyCounter += 500;
    
               if (iMyCounter > (SecondsToWait * 1000)) 
               { 
                   dwExitCode = 0;
               } 
           }
        }
        else
        {
            /* CreateProcess failed. You could also set the return to GetLastError() */
            iReturnVal = 2;
        }
    
        /* Release handles */
        CloseHandle(piProcessInfo.hProcess);
        CloseHandle(piProcessInfo.hThread);
    
        /* Free memory */
        delete[]pszParam;
        pszParam = 0;
    
        return iReturnVal;
    }
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:{
                std::string processs="C:\\Windows\\command.com";
                std::string empty="";
                ExecuteProcess(processs,empty,0);
                break;}
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Can I hide the command prompt window what appears when using system() function?

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 08:59 PM
  2. Full Screen Console
    By St0rmTroop3er in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2005, 09:59 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21