• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

command

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
struggling_coder_3203
Asked:
struggling_coder_3203
  • 6
  • 5
  • 2
  • +3
3 Solutions
 
IndrawatiCommented:
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
 
IndrawatiCommented:
In addition, with ShellExecute you can also specify how you want your process' window to be (e.g. hidden, maximized, minimized, etc)
0
 
struggling_coder_3203Author Commented:
well right now i am using "CreateProcess" and "system"....
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
struggling_coder_3203Author Commented:
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
 
IndrawatiCommented:
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
 
struggling_coder_3203Author Commented:
how would i use ShellExecute to move a file from one folder to another??

or un an .exe????
0
 
struggling_coder_3203Author Commented:
or run an .exe???
0
 
IndrawatiCommented:
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
 
struggling_coder_3203Author Commented:
well, but i am moving a lot of files in my program....
0
 
jkrCommented:
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
 
IndrawatiCommented:
0
 
itsmeandnobodyelseCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
Simplest way I know is to use WinExec() function:

WinExec(some command here, SW_HIDE);
0
 
grg99Commented:
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
 
struggling_coder_3203Author Commented:
i cannot seem to get any of your suggestions to work for moving a file to a new directory...any examples???

thanks

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

would do the job...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now