views:

132

answers:

2

Is there any way I can have a thread branch off into its own independent process? I know there's the CreateProcess function but as far as I can tell, you can only run external applications with it. Is what I'm asking for at all possible?

+3  A: 

That's not possible under Windows. On Posix platforms the desired effect could be achieved by fork()ing.

Alexander Gessler
Doesn't `fork()` work on Windows?
sbi
I believe Cygwin provides an implementation, also this answer given by Jed Smith is a bit more insightful than mine: http://stackoverflow.com/questions/1814903/running-fork2-from-windows-with-cygwin-possible
Alexander Gessler
The Windows kernel knows how to `fork()` but, as far as I know, this is not accessible directly through the Win32 interface. You have to use another API such as "Services For Unix" ( http://technet.microsoft.com/en-us/library/bb496506.aspx ) or Cygwin ( http://www.cygwin.com/ ).
Thomas Pornin
Alexander is right.http://msdn.microsoft.com/en-us/library/ms682516%28v=VS.85%29.aspx
TBH
@Thomas: or use Native API if you're willing to pay the price.
conio
See also http://www.eggheadcafe.com/software/aspnet/32040421/ntcreateprocess-and-fork.aspx for a short discussion of a _recent_ Cygwin implementation .
MSalters
+2  A: 

It is possible.

You could call CreateProcess with a dummy application and with the CREATE_SUSPENDED flag so it doesn't run immediately. Then you can use VirtualAllocEx to allocate memory space in the created process and WriteProcessMemory to write code and data into it. And then unsuspend the process to run it.

You can also use CreateRemoteThread to create a process running within the context of another existing process.

So what you want to do is possible, but it's really not a simple thing to do in a windows environment so you'd have to have a really good reason to want to do it.

John Burton