starting program in background

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 8
Reputation: asianguyjtran is an unknown quantity at this point 
Solved Threads: 0
asianguyjtran asianguyjtran is offline Offline
Newbie Poster

starting program in background

 
0
  #1
May 27th, 2008
hi all
i'm new to c++ and decided to tackle c++ by doing a small project

i would like to write a program that will start other programs in the background and get its pid number

so far, i have used system() but i get the output of that program

anyone care to mentor me?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: starting program in background

 
0
  #2
May 27th, 2008
Link.

Check my last three posts for code.

Enjoy!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 8
Reputation: asianguyjtran is an unknown quantity at this point 
Solved Threads: 0
asianguyjtran asianguyjtran is offline Offline
Newbie Poster

Re: starting program in background

 
0
  #3
May 27th, 2008
i just looked at that link and it appears that that is for linux.

btw. is this for windows?

does CreateProcess() work?
Last edited by asianguyjtran; May 27th, 2008 at 10:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: starting program in background

 
0
  #4
May 27th, 2008
I'm sorry. You said "pid" and I immediately went into *nix mode.

In Windows the terminology is that each process has a handle. Which is the same thing. Go figure.

Anyway, yes, CreateProcess() is what you want. Here's a little function to automate it a little for simple (usual) cases.
  1. #include <string>
  2. #include <windows.h>
  3.  
  4. HANDLE ExecProcess(
  5. const std::string& arg0,
  6. const std::string& args,
  7. LONGBOOL is_inherit_handles = TRUE
  8. ) {
  9. HANDLE result = INVALID_HANDLE_VALUE;
  10. std::string cmdline;
  11. STARTUPINFO startup_information = {0};
  12. PROCESS_INFORMATION process_information = {0};
  13.  
  14. startup_information.cb = sizeof( STARTUPINFO );
  15. cmdline = string( '\"' ) +arg0 +"\" " +args;
  16.  
  17. if (CreateProcess(
  18. arg0.c_str(), // full path of program to execute
  19. cmdline.c_str(), // complete command line
  20. NULL, // no process attribute handle inheritance
  21. NULL, // no thread attribute handle inheritance
  22. is_inherit_handles, // such as stdin, etc.
  23. NORMAL_PRIORITY_CLASS, //
  24. NULL, // use the parent's environment
  25. NULL, // use the parent's current working directory
  26. &startup_information, // (all defaults)
  27. &process_information // resulting process info
  28. ))
  29. result = process_information.hProcess;
  30.  
  31. CloseHandle( process_information.hThread );
  32. return result;
  33. }

You can use the returned handle to obtain information about the child process and wait for specific events. For example, to wait for the child to terminate, use
  1. WaitForSingleObject( my_childs_process_handle, INFINITE );

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: starting program in background

 
0
  #5
May 28th, 2008
Well good grief. Is there some sort of timeout on editing posts? I haven't been gone that long.

Anyway, to fix a couple of simple errors in the last...
  1. #include <string>
  2. #include <windows.h>
  3.  
  4. HANDLE ExecProcess(
  5. const std::string& arg0,
  6. const std::string& args,
  7. BOOL is_inherit_handles = TRUE
  8. ) {
  9. HANDLE result = INVALID_HANDLE_VALUE;
  10. std::string cmdline;
  11. STARTUPINFO startup_information = {0};
  12. PROCESS_INFORMATION process_information = {0};
  13.  
  14. startup_information.cb = sizeof( STARTUPINFO );
  15. cmdline = std::string( 1, '\"' ) +arg0 +"\" " +args;
  16.  
  17. if (CreateProcess(
  18. arg0.c_str(), // full path of program to execute
  19. const_cast<char*>(
  20. cmdline.c_str()), // complete command line
  21. NULL, // no process attribute handle inheritance
  22. NULL, // no thread attribute handle inheritance
  23. is_inherit_handles, // such as stdin, etc.
  24. NORMAL_PRIORITY_CLASS, //
  25. NULL, // use the parent's environment
  26. NULL, // use the parent's current working directory
  27. &startup_information, // (all defaults)
  28. &process_information // resulting process info
  29. ))
  30. result = process_information.hProcess;
  31.  
  32. CloseHandle( process_information.hThread );
  33. return result;
  34. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 8
Reputation: asianguyjtran is an unknown quantity at this point 
Solved Threads: 0
asianguyjtran asianguyjtran is offline Offline
Newbie Poster

Re: starting program in background

 
0
  #6
May 28th, 2008
Thank you for that snippet.

I'll have to study it for a while to get the hang of things since I've been working with php for a long time now

how would you call the function, ExecProcess()?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: starting program in background

 
0
  #7
May 28th, 2008
Ah, you've been brain-damaged by PHP. My condolences... er,

Oh, so you know PHP? Cool!




Call it with the full path to your program and the command line arguments exactly as you would type them at the prompt:

ExecProcess( "C:\WINDOWS\notepad.exe", "myfile.txt" );

If you don't like the idea of the new program having access to your open file descriptors, set the last argument to FALSE.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 8
Reputation: asianguyjtran is an unknown quantity at this point 
Solved Threads: 0
asianguyjtran asianguyjtran is offline Offline
Newbie Poster

Re: starting program in background

 
0
  #8
May 28th, 2008
Do you not like php?

When I go to debug, I got the errors in my build log

1>------ Build started: Project: VanaManager, Configuration: Debug Win32 ------
1>Compiling...
1>ExecProcess.cpp
1>c:\documents and settings\asianguyjtran\desktop\vanamanager\vanamanager\execprocess.cpp(29) : error C2664: 'CreateProcessW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>VanaManager.cpp
1>c:\documents and settings\asianguyjtran\desktop\vanamanager\vanamanager\execprocess.cpp(29) : error C2664: 'CreateProcessW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Generating Code...
1>Build log was saved at "file://c:\Documents and Settings\asianguyjtran\Desktop\VanaManager\VanaManager\Debug\BuildLog.htm"
1>VanaManager - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,400
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 113
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: starting program in background

 
0
  #9
May 29th, 2008
I think you need to disable unicode to remove those errors.
Try adding:
  1. #undef UNICODE
at the very beginning of your source code.
Last edited by William Hemsworth; May 29th, 2008 at 8:32 am. Reason: Solution
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,822
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: starting program in background

 
0
  #10
May 29th, 2008
Originally Posted by williamhemswort View Post
I think you need to disable unicode to remove those errors.
Try adding:
  1. #undef UNICODE
at the very beginning of your source code.
You're right, but if the OP wants to use unicode (no idea why, but: )
You could call CreateProcess like this:
CreateProcess(L"yourexe.exe", L"parameters", ....etc);
Last edited by niek_e; May 29th, 2008 at 8:35 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC