0

What I want to do is use the Windows C API from my own single command line application to create a brand new process. (A couple of them actually). I want to be able to name this process whatever I want and all I want it to do is do a sleep(30) to get me going.

Im having trouble doing this with the CreateFile() API as it wants me to specify an executable to run the new process from. What I am after is something a bit like Fork() on Linux.

How do I go about doing this? Do I have to do something complex like embed an exe that calls sleep() in the resource section of my program, drop it then run CreateProcess() on it?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • Are you just after a sleep command in Windows batch? http://stackoverflow.com/questions/735285/how-to-wait-in-a-batch-script – cup Mar 16 '14 at 12:33
  • No, sleep() is just a place holder for other code im going to put in the other processes. I want this to all be in C, no bat scripts. – BinDumpThunk Mar 16 '14 at 13:16
  • Windows doesn't support `fork`. You have execute a separate process or use threads. – nwellnhof Mar 16 '14 at 15:24
  • The usual solution is to launch another copy of the same executable, perhaps with a command-line argument to tell it what to do. I'm not sure what you mean by "name this process"; I guess you want something in particular to appear under Image Name in Task Manager? In that case, you'd could make a copy of your executable and launch that, but this might not be appropriate if the executable is very large. – Harry Johnston Mar 16 '14 at 23:25

1 Answers1

0

Reading around and talking to people, this is what I have found for anyone who has the same question as me.

CreateProcess() requires an executable to launch. There is no equivalent of fork() on windows. You need to pass it an executable file.

There are two ways of doing this: 1) embed a new executable in the resource section, drop it to disk and then call CreateProcess() on this from the main executable.

2) Have your main process call CreateProcess() on its own executable file, and handle what happens when it is called again dynamically. For example, if I run myApp.exe with no arguments, Perform its main tasks such as calling CreateFile() on its own executable. To tell that its not just running normally you could do something like this time handing the executable an argument. You can then handle this accordingly. eg if you then call myapp.exe with the argument "foo", the application then knows its being run differently and handle execution accordingly. Eg in main, when it see's the argument foo, it could walk its own PEB and change the process name to foo.exe (so it looks like a new executable) and then perform any actions you want.

I hope my explanation makes sense there.

Thanks for the help