| | |
starting program in background
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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
Hope this helps.
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.
C++ Syntax (Toggle Plain Text)
#include <string> #include <windows.h> HANDLE ExecProcess( const std::string& arg0, const std::string& args, LONGBOOL is_inherit_handles = TRUE ) { HANDLE result = INVALID_HANDLE_VALUE; std::string cmdline; STARTUPINFO startup_information = {0}; PROCESS_INFORMATION process_information = {0}; startup_information.cb = sizeof( STARTUPINFO ); cmdline = string( '\"' ) +arg0 +"\" " +args; if (CreateProcess( arg0.c_str(), // full path of program to execute cmdline.c_str(), // complete command line NULL, // no process attribute handle inheritance NULL, // no thread attribute handle inheritance is_inherit_handles, // such as stdin, etc. NORMAL_PRIORITY_CLASS, // NULL, // use the parent's environment NULL, // use the parent's current working directory &startup_information, // (all defaults) &process_information // resulting process info )) result = process_information.hProcess; CloseHandle( process_information.hThread ); return result; }
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
C++ Syntax (Toggle Plain Text)
WaitForSingleObject( my_childs_process_handle, INFINITE );
Hope this helps.
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...
Anyway, to fix a couple of simple errors in the last...
C++ Syntax (Toggle Plain Text)
#include <string> #include <windows.h> HANDLE ExecProcess( const std::string& arg0, const std::string& args, BOOL is_inherit_handles = TRUE ) { HANDLE result = INVALID_HANDLE_VALUE; std::string cmdline; STARTUPINFO startup_information = {0}; PROCESS_INFORMATION process_information = {0}; startup_information.cb = sizeof( STARTUPINFO ); cmdline = std::string( 1, '\"' ) +arg0 +"\" " +args; if (CreateProcess( arg0.c_str(), // full path of program to execute const_cast<char*>( cmdline.c_str()), // complete command line NULL, // no process attribute handle inheritance NULL, // no thread attribute handle inheritance is_inherit_handles, // such as stdin, etc. NORMAL_PRIORITY_CLASS, // NULL, // use the parent's environment NULL, // use the parent's current working directory &startup_information, // (all defaults) &process_information // resulting process info )) result = process_information.hProcess; CloseHandle( process_information.hThread ); return result; }
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:
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.
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.
•
•
Join Date: May 2008
Posts: 8
Reputation:
Solved Threads: 0
Do you not like php?
When I go to debug, I got the errors in my build log
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 ==========
•
•
Join Date: Mar 2008
Posts: 1,400
Reputation:
Solved Threads: 113
I think you need to disable unicode to remove those errors.
Try adding:
at the very beginning of your source code.
Try adding:
C++ Syntax (Toggle Plain Text)
#undef UNICODE
Last edited by William Hemsworth; May 29th, 2008 at 8:32 am. Reason: Solution
I need pageviews! most fun profile ever :)
•
•
•
•
I think you need to disable unicode to remove those errors.
Try adding:
at the very beginning of your source code.C++ Syntax (Toggle Plain Text)
#undef UNICODE
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.
![]() |
Similar Threads
- ambiguous part 3 (C)
- executing the normal program from linux.rc - different bahaviour (Kernels and Modules)
- Outlook 2003 and Instant Messengers Crash after starting up (Windows Software)
- Helping yourself: What to do before starting a new thread or posting a HiJackThis log (Viruses, Spyware and other Nasties)
- IE 6.0 not starting (Viruses, Spyware and other Nasties)
- Reload page Error message when starting Windows XP (Viruses, Spyware and other Nasties)
- Error message missing bridge.dll when starting windows (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Delete Operation: Debug Assertion Failed (HEAP_CORRUPTION_DETECTED)
- Next Thread: searching random numbers in a array
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






