[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

system()

Posted on 2004-11-09
12
Medium Priority
?
274 Views
Last Modified: 2010-05-18
hi guys, this is my code as it is:

#include <stdlib.h>

int main(){
      system("CMD.exe /c explorer C:\\");

      return 0;
}


it does as it should apart from the system waits for the command to finish, which is indefinate...
I was wondering of there's a method of calling commands to the system without the application waiting for an exit.
Cheers in advance.
0
Comment
Question by:OliWarner
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 5

Expert Comment

by:van_dy
ID: 12541774
I think on windows, you
have CreateProcess() call which
can do the jog or you. take a look into
its manual and see if it helps.

hope this helps,
van_dy
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12542169
Try using one of the 'spawn' functions. They allow a parameter of _P_WAIT to wait for a function to complete or _P_NOWAIT not to wait.

// Perhaps.
    _spawnlp(_P_WAIT, "explore","cmd.exe","/c","explorer","C:\\", NULL);

The above will almost certainly not work but it might be a good start.

Paul
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12542241
Hi OliWarner,
Try the start command:

#include <stdlib.h>

int main(){
     system("CMD.exe /c start explorer C:\\");

     return 0;
}


Cheers!

Stefan
0
Technology Partners: 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!

 
LVL 22

Expert Comment

by:grg99
ID: 12543378
You probably can ditch the "cmd.exe /c" part, that's exactly what system() runs for you.

Note that a user can hijack this by having their own "explorer.exe" or .bat in their PATH.

0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12543406
Stefan,

Thanks for the 'start' hint !! I didnt know about that. W98 didnt need it but now I'm on XP I hit this problem all over the place.

Paul
0
 
LVL 16

Author Comment

by:OliWarner
ID: 12544599
Yeah start is the one that worked. Cheers for all the effort guys.
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 12544676
If you want a sample of how to use the CreateProcess call look at this PAQ

http://www.experts-exchange.com/Programming/Q_21184160.html#12419704

Change the following line as shown

     if ( !CreateProcess ( NULL, "explorer.exe c:\\"  , NULL , NULL , FALSE , 0 , NULL , NULL, &si, &pi ) )
                                             ^^^^^^^^^^^

HTH
0
 
LVL 16

Author Comment

by:OliWarner
ID: 12544716
what do i need to #include for that?
0
 
LVL 16

Author Comment

by:OliWarner
ID: 12544722
ignore that, just read the other thread
0
 
LVL 16

Author Comment

by:OliWarner
ID: 12544798
just three final things...

what do you reckon is the more efficient method of doing this... the "start" way or the "createprocess" way?
Is there a way i can stop the command line app box popping up momentarily as the process is launched?
How do I add an icon to the compiled file in MS VS?
0
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 12544914
>> what do you reckon is the more efficient method of doing this... the "start" way or the "createprocess" way?

CreateProcess is the better way of doing it - system is a throwback to DOS days and is included for ANSI compatibility.

>> Is there a way i can stop the command line app box popping up momentarily as the process is launched?

If you use CreateProcess the command window will only popup if you compile your app as a console app. If you create it as a windows app (Win32 Application in VC project window) you won't see the conosle. See code below

>> How do I add an icon to the compiled file in MS VS?
Add a resource file to the project. On the Menu: Insert->Resource
Choose ICON
Draw / paint / cut and paste the icon you want. This will be for 32X32 sized icons - for large icon view. You need to add an icon for small icon view too
In the ICON resource window click the icon next to the Device drop down (looks like a magnifying glass on the bottom left of a page)
You will get the option to create a diffent size ICON - choose 16X16
Create the image for the small ICON that you want.
Save the resource file and add it to your project.
Compile and that should do the trick.

Here is the code for the Win32 Implementation of the CreateProcess sample


#include "windows.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
      STARTUPINFO si ;
      PROCESS_INFORMATION pi ;

      ZeroMemory ( &si, sizeof ( STARTUPINFO ) );
      si.cb = sizeof ( STARTUPINFO ) ;
      si.dwFlags = STARTF_USESHOWWINDOW ;
      si.wShowWindow = SW_SHOW;

      if ( !CreateProcess ( NULL, "explorer.exe c:\\"  , NULL , NULL , FALSE , 0 , NULL , NULL, &si, &pi ) )
      {
            char szError[256] ;

            wsprintf ( szError, "Error: %ld\n", GetLastError ( ) );
            MessageBox ( NULL, szError, NULL, MB_OK ) ;
      }

      return 0;
}



0
 
LVL 12

Expert Comment

by:stefan73
ID: 12560826
OliWarner,
> what do you reckon is the more efficient method of doing this... the
> "start" way or the "createprocess" way?

CreateProcess is what start uses under the hood. Perhaps it's possible to call start with /B to avoid the window - but I'm not sure.

On the other hand, with start you avoid having to glance at the gaping horrors of Windows' internals :-)

Stefan
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

873 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