How to create process in Windows ( C++ ) to run another section of code?
Is there a way to create a process in windows and provide a function etc. to be executed by it similar to threads?
No. But what you can do is have your program run another copy of itself, passing it command line parameters to tell it what to do as needed. You can use GetModuleFileName()
to get the full path to the EXE file of the current process. Then, in your main()
/WinMain()
, if any command line parameters are present, run the appropriate function and then exit, otherwise run your main logic normally.
How do I call ::CreateProcess in c++ to launch a Windows executable?
Something like this:
STARTUPINFO info={sizeof(info)};
PROCESS_INFORMATION processInfo;
if (CreateProcess(path, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
WaitForSingleObject(processInfo.hProcess, INFINITE);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
How to create a new process in C++ code under Windows?
Hope this helps: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx
It is explained quite detailed.
Create new process, then send it a command later, on Windows 10
Generally, Inter-Process communication is something that both processes must define in order to work. You can't just send "commands" to another application if the other application is not designed to receive it (or easily one program would ruin the other).
Probably you tried cmd.exe /c dir
which instantly executes "dir" on cmd.exe but this works because cmd.exe has the /c parameter, not because you can do that generally. Since cmd can execute batch files, you may try this or this or this.
Generally, after both applications agree on what to send, the next step is how to transfer data. In Windows, this can be done with File Mapping, Sockets, Pipes etc.
How to create a new process in C/C++ and get this new process handle in WinXp?
CreateProcess()
returns a PROCESS_INFORMATION
structure which contains the process handle. See here and here.
Whilst ShellExecute()
does not give you the information that you need, ShellExecuteEx()
returns the process handle in the SHELLEXECUTEINFO
structure. See here and here.
I would suggest that you look at the MSDN documentation for the API functions that you're using as it's very useful.
Difference between creating new process and child process in C (Windows)
In Windows, processes don't have parents. Some tools read the InheritedFromUniqueProcessId
value, but this does not tell you which process started your process. It only tells you where handles and other attributes were inherited from. In practice however, this value is usually set to the ID of the process that started the child process.
On Vista and above, you can change the InheritedFromUniqueProcessId
value by calling CreateProcess
with the STARTUPINFOEX
structure filled in appropriately: create an attribute list with InitializeProcThreadAttributeList
, and add a
attribute with
PROC_THREAD_ATTRIBUTE_PARENT_PROCESSUpdateProcThreadAttribute
.
On XP, there is no official way of doing this. You can try to use NtCreateProcess
or RtlCreateUserProcess
, but these don't set up the Win32 subsystem properly so your program might not run.
Related Topics
Is Substitution Performed on a Variadic Parameter Pack Type If the Pack Is Empty
How to Use Wndproc as a Class Function
Questions Regarding C++ Non-Pod Unions
Right Justifying Output Stream in C++
Differencebetween If (Null == Pointer) VS If (Pointer == Null)
Do You Know Tool Building Tree of Include Files in Project\File
Why Isn't Memcpy Guaranteed to Be Safe for Non-Pod Types
Generate Dependencies for a Makefile for a Project in C/C++
How Would One Call Std::Forward on All Arguments in a Variadic Function
Most Efficient Way to Check If All _M128I Components Are 0 [Using <= Sse4.1 Intrinsics]
Inheriting and Overriding Functions of a Std::String
Vs: Unexpected Optimization Behavior with _Bitscanreverse64 Intrinsic
Throwing the Fattest People Off of an Overloaded Airplane
How Does Url Within Function Body Get Compiled