• C

system()

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.
LVL 16
OliWarnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

van_dyCommented:
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
PaulCaswellCommented:
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
stefan73Commented:
Hi OliWarner,
Try the start command:

#include <stdlib.h>

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

     return 0;
}


Cheers!

Stefan
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

grg99Commented:
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
PaulCaswellCommented:
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
OliWarnerAuthor Commented:
Yeah start is the one that worked. Cheers for all the effort guys.
0
Julian HansenCommented:
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
OliWarnerAuthor Commented:
what do i need to #include for that?
0
OliWarnerAuthor Commented:
ignore that, just read the other thread
0
OliWarnerAuthor Commented:
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
Julian HansenCommented:
>> 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stefan73Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

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.