win32 equivalent of system()

This is a discussion on win32 equivalent of system() within the Windows Programming forums, part of the Platform Specific Boards category; Hi All, I'm a unix programmer and I'm trying to find the best way of running an external programm using ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    win32 equivalent of system()

    Hi All,

    I'm a unix programmer and I'm trying to find the best way of running an external programm using win32 and getting it's exit status. So, what is the equivalent of using System()?

    I've noticed that there is a _popen() function, but I don't need to communicate with the process - just call it and get the exit status.

    Any ideas?

    rotis23

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    CreateProcess()
    WaitForSingleObject()
    GetExitCodeProcess()
    CloseHandle()

    Search board or web for samples.

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    There's a System() function in Windows. It's not the best way of doing things, though.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Thanks for the pointer.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    I'm having a couple problems with GetExitCodeProcess. My code:

    Code:
    if(CreateProcess(fullPath,args, 0, 0, false,
    		CREATE_DEFAULT_ERROR_MODE, 0, 0, &siStartupInfo,
    			&piProcessInfo) != false)
    	{
    		/* A loop to watch the process. Dismissed with SecondsToWait set to 0 */
    		GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);
    
    		while (dwExitCode == STILL_ACTIVE && SecondsToWait != 0)
    		{
    			GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);
    			Sleep(500);
    			iMyCounter += 500;
    
    			if (iMyCounter > (SecondsToWait * 1000))
    			{
    				dwExitCode = 0;
    			}
    		}
    	}
    	else
    	{
    		iReturnVal = 2;
    	}
    I'm getting back error 127 (ERROR_PROC_NOT_FOUND) and the process is completing successfully.


    I guess I should use waitforsingleobject?

    rotis23

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> I'm getting back error 127 (ERROR_PROC_NOT_FOUND) <<

    How? The code you posted does not contain a call to GetLastErrror(). Your code looks fine.

    >> I guess I should use waitforsingleobject? <<

    Yes. WaitForSingleObject is simpler and you don't have to worry about the child process returning STILL_ACTIVE.
    Code:
    if (CreateProcess(...))
    {
         if (WAIT_OBJECT_0 == WaitForSingleObject(pi.hProcess, cSeconds * 1000))
         {
              // Process is finished
              GetExitCodeProcess(...);
         }
         else
         {
              // Timeout or error
         }
    
         CloseHandle(pi.hThread);
         CloseHandle(pi.hProcess);
    }

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Thanks

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    I've changed to use WaitForSingleObject but I'm still getting back error 127 (ERROR_PROC_NOT_FOUND) and the process is completing successfully.

    Any ideas? Could it be the way I'm calling CreateProcess?

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    1 more question (I still have the problem above).

    When running the above code froma service - how do I stop the command window popping up when the process is created?

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    To stop the window popping up use DETACHED_PROCESS as an argument to CreateProcess.

    Google doesn't know about error 127 though.

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I'm assuming the 127 is being returned by GetLastError()? When do you call GetLastError()? GetLastError() will only return a valid value immediately after an API call has failed. Is this a 16bit or DOS process you are launching?

    Have a look at this thread:
    http://groups.google.com/groups?thre...eld.com&rnum=2
    Last edited by anonytmouse; 08-25-2004 at 02:11 AM.

  12. #12
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Thanks for your help anonytmouse.

    That thread doesn't really shed any light - I've done a good amount of googling.

    I'm actaully calling pscp (Putty Secure Copy) to send a file via ssh to a unix box. I guess that 16bit?????

    I call getlasterror directly after successfully calling getexitcodeprocess:

    Code:
    /* CreateProcess API initialization */
    	STARTUPINFO siStartupInfo;
    	PROCESS_INFORMATION piProcessInfo;
    	memset(&siStartupInfo, 0, sizeof(siStartupInfo));
    	memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    	siStartupInfo.cb = sizeof(siStartupInfo);
    	siStartupInfo.dwFlags = STARTF_USESHOWWINDOW;
      	siStartupInfo.wShowWindow = SW_HIDE;
    
    if(CreateProcess(fullPath,args,NULL,NULL, false,
    		CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &siStartupInfo,
    			&piProcessInfo) != false)
    	{
    
    		if (WAIT_OBJECT_0 == WaitForSingleObject(piProcessInfo.hProcess,timeout * 1000))
    		{
    			if(GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode))
    			{
    				printf("exited - %d - %d\n",dwExitCode,GetLastError());
    			}
    			else
    			{
    				dwExitCode = GetLastError();
    			}
    		}
    		else
    		{
    			//timeout or error
    			dwExitCode = GetLastError();
    		}
    
    		CloseHandle(piProcessInfo.hProcess);
    		CloseHandle(piProcessInfo.hThread);
    	}
    	else
    	{
    		dwExitCode = GetLastError();
    	}

  13. #13
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by anonytmouse
    GetLastError() will only return a valid value immediately after an API call has failed.
    unless otherwise stated in MSDN.

    Some API functions will call other API functions which may fail and use SetLastError(). However, the parent function may recover and return successfully. Why @err is 127 after GetExitCodeProcess() returns, I can't even guess.

  14. #14
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by rotis23
    I'm actaully calling pscp (Putty Secure Copy) to send a file via ssh to a unix box. I guess that 16bit?????
    32
    hello, internet!

  15. #15
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Ah - OK - I see - the function suceeded and therefore getlasterror doesn't mean anything.

    I'm trying to find out why the return status of of getexitcodeprocess is 0 - even though the process suceeds in what it's doing (scp'ing a file).

    hmmmmmmm

    I guess this could be that pscp is not returning it's exit status correctly. I'll try and dig around more - thanks for all your help so far.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux equivalent to Win32 ShellExecute
    By BobS0327 in forum Linux Programming
    Replies: 4
    Last Post: 06-07-2006, 05:35 AM
  2. Opinions on custom system build
    By lightatdawn in forum Tech Board
    Replies: 2
    Last Post: 10-18-2005, 05:15 AM
  3. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 08:38 AM
  4. School Mini-Project on C/C++ (Need Your Help)..
    By EazTerence in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 02:08 AM
  5. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21