How to Create a Process in C++ on Windows

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
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS
attribute with UpdateProcThreadAttribute.

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



Leave a reply



Submit