tags:

views:

51

answers:

2

I want to create a process with syntax similar to the following,except that I don't want to create a thread

     hThread = CreateThread( 
        NULL,              // no security attribute 
        0,                 // default stack size 
        InstanceThread,    // thread proc
        (LPVOID) hPipe,    // thread parameter 
        0,                 // not suspended 
        &dwThreadId);      // returns thread ID 

But I've checked the reference for CreateProcess and a sample :

BOOL result = ::CreateProcess(
  L"C:\\Windows\\NOTEPAD.exe",
  NULL,
  NULL,
  NULL,
  FALSE,
  NORMAL_PRIORITY_CLASS,
  NULL,
  NULL,
  &startupInfo,
  &processInformation
);

It seems I must specify an existing executable to create a process? How can I create one by a callback similar to InstanceThread ?

+1  A: 

You cannot do this. Processes and threads are different. You cannot create a process by just giving the address of a function in your executable.

You could, however, create your process with some command-line argument that your process reads and then uses to call the target function you want.

What are you trying to achieve?

Chris Schmich
Then is it possible to create a thread that can still survive after the main thread is dead?
Alan
You can have your main thread wait for the completion of your background thread. Use `WaitForSingleObject` (http://msdn.microsoft.com/en-us/library/ms687032(VS.85).aspx) to join the threads, e.g. `WaitForSingleObject(threadHandle, INFINITE);`
Chris Schmich
No,I don't want the main thread to wait,but die(),leave the created thread still alive.
Alan
Your process won't terminate until *all* threads have exited.
Hans Passant
@Hans Passant: Technically true, but note that some CRT implementations on Windows call ExitProcess(), terminating all threads, after main() returns. As a result, Alan is partly right: he cannot return from main() and rely on the OS keeping his other thread alive. The main thread should die violently (TerminateThread)
MSalters
A: 

Perhaps look how cygwin implements fork(). Unix fork() is what you want here.

jilles