Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

command

Posted on 2004-11-01
16
Medium Priority
?
208 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
Comment
Question by:struggling_coder_3203
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +3
16 Comments
 
LVL 3

Accepted Solution

by:
Indrawati earned 900 total points
ID: 12469109
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
ID: 12469125
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
ID: 12469189
well right now i am using "CreateProcess" and "system"....
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:struggling_coder_3203
ID: 12469194
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
ID: 12469223
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
ID: 12469238
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
ID: 12469240
or run an .exe???
0
 
LVL 3

Expert Comment

by:Indrawati
ID: 12469252
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
ID: 12469262
well, but i am moving a lot of files in my program....
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 600 total points
ID: 12469366
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
Indrawati earned 900 total points
ID: 12469413
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12472244
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
ID: 12472353
Simplest way I know is to use WinExec() function:

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

Expert Comment

by:grg99
ID: 12472418
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
ID: 12476838
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
ID: 12476892
MoveFileEx ( "c:\\dir1\\sample.txt", "c:\\dir2\\sample2.txt", MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);

would do the job...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

610 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