Solved

command

Posted on 2004-11-01
205 Views
Last Modified: 2010-04-01
I have a visual c++ program that envokes the command prompt. The problem is every time I create a process the command prompt window opens, and it executes. This is all fine...however, if my program is going to execute a hundred commands, or even a thousand,  the command prompt window is constantly opening and closing. If this is the case, then the computer won't be usable while the program is running because the command prompt window constantly opens. Any suggestions????

500 pts
0
Question by:struggling_coder_3203
    16 Comments
     
    LVL 3

    Accepted Solution

    by:
    How do you create the process? Using system()? Maybe you can look at ShellExecute() instead? Unllike System(), ShellExecute() does not show the command prompt.
    0
     
    LVL 3

    Expert Comment

    by:Indrawati
    In addition, with ShellExecute you can also specify how you want your process' window to be (e.g. hidden, maximized, minimized, etc)
    0
     

    Author Comment

    by:struggling_coder_3203
    well right now i am using "CreateProcess" and "system"....
    0
     

    Author Comment

    by:struggling_coder_3203
    so with shell execute, i will be able to run my program in the background without having the command prompt pop up every couple seconds and steal focus???
    0
     
    LVL 3

    Expert Comment

    by:Indrawati
    Yes, you can specify whether you want you process' window to be hidden, maximized, etc2.

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecute.asp
    0
     

    Author Comment

    by:struggling_coder_3203
    how would i use ShellExecute to move a file from one folder to another??

    or un an .exe????
    0
     

    Author Comment

    by:struggling_coder_3203
    or run an .exe???
    0
     
    LVL 3

    Expert Comment

    by:Indrawati
    I don't think you can use ShellExecute directly to move files. To run an exe, just call ShellExecute with the lpOperation parameter as "open" and lpFile as the name of the executable.
    0
     

    Author Comment

    by:struggling_coder_3203
    well, but i am moving a lot of files in my program....
    0
     
    LVL 86

    Assisted Solution

    by:jkr
    If you are using "CreateProcess()", try to set 'SW_HIDE':

    DWORD ExecuteAndWaitForCompletion   (   LPSTR   pszCmd)
    {
       STARTUPINFO         si;
       PROCESS_INFORMATION pi;

       BOOL                bRes;

       DWORD               dwCode  =   0;

       ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

       si.cb           =   sizeof  (   STARTUPINFO);
       si.dwFlags      =   STARTF_USESHOWWINDOW;
       si.wShowWindow  =   SW_HIDE;

       bRes    =   CreateProcess   (   NULL,
                                       pszCmd,
                                       NULL,
                                       NULL,
                                       TRUE,
                                       NORMAL_PRIORITY_CLASS,
                                       NULL,
                                       NULL,
                                       &si,
                                       &pi
                                   );

       while   (   WAIT_OBJECT_0   !=  MsgWaitForMultipleObjects   (   1,
                                                                       &pi.hProcess,
                                                                       FALSE,
                                                                       INFINITE,
                                                                       QS_ALLINPUT
                                                                   )
               )
               {
                   while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
                           {
                               DispatchMessage     (   &msg);
                           }
               }

       GetExitCodeProcess  (   pi.hProcess,    &dwCode);

       CloseHandle (   pi.hProcess);
       CloseHandle (   pi.hThread);

       return  (   dwCode);
    }
    0
     
    LVL 3

    Assisted Solution

    by:Indrawati
    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    You could turn your project from Win32 Console Application to Win32 Application. Your main function turns to WinMain

    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)

    and if you did evaluate the commandline arguments in main, you now have to parse the lpCmdLine argument.

    In link settings you have to change option

           /subsystem:console

    to

           /subsystem:windows

    Regards, Alex
    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Simplest way I know is to use WinExec() function:

    WinExec(some command here, SW_HIDE);
    0
     
    LVL 22

    Expert Comment

    by:grg99
    Wait a second, if you're moving a file, isnt it important to know if the file move actually WORKED?

    I would write my own file move function, something along the lines of:

    Err = rename( oldname, newname );

    if( Err != NoError ) { printf("cant rename %s to %s, doing byte copy\n", oldname, newname );
    in = fopen( oldname, "r" );  out = fopen( newname, "w" );
    if( in == Error ){ printf("cant open %s for reading!!!\n", oldname ); return( -1 ); }
    else
    {
    if( Out == Error ){ printf("cant open %s for writing!!!\n", newname ); return( -2 ); }
    else
    {
        while( ! feof( in ) ) {   rd = fread( buf, inlen, &inact, in );
                                        if( rd < 0 ) { printf("Error reading input file!\n"); return(-3); }
                                        else { wt = fwrite( buf, inact, outact, out );
                                         if( wt < 0 ) { pritnf("Error writing!" ); return( -4 ); }
                                    }
            err = fclose( in );  if( err... )
             err = fclose( out );  if( err ... )
    }
    return(0);
    }

    // note, you have to declare all varaibles, check param lists and error checks for accuracy....
    0
     

    Author Comment

    by:struggling_coder_3203
    i cannot seem to get any of your suggestions to work for moving a file to a new directory...any examples???

    thanks

    0
     
    LVL 86

    Expert Comment

    by:jkr
    MoveFileEx ( "c:\\dir1\\sample.txt", "c:\\dir2\\sample2.txt", MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);

    would do the job...
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Live - One-on-One C++ Help from Top Experts

    Solve your toughest problems, fast.
    C++ experts are online now and ready to help you.

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now