I would like to start a program B with an autorun.inf when I plug in an USB stick. The program is on the HDD, however, there seems to be a dependency between the USB stick and the program, as I can't remove the stick savely without closing the program before. So, I thought about making a small program (A) on the USB stick, that just opens the main program (B), then I close (A) and (B) keeps running.
So far I tried to use the CreateProcess function. I get to start program (B), however, it still depends on program A, so that A does not close and the USB cannot be removed.
Two things: As kbw states, it could be that you are inheriting the working directory to the spawned process because you do not set one using the STARTUPINFO structure.
The other thing is: You are waiting for the spawned process to finish. You must not. If you do (like you are doing now), the USB stick will continue to be in use because Windows is running your EXE from the USB key. You must either spawn B and exit, or your application must be launched from another part of the file system, like the temp directory.
A way to achieve the latter would be for your application to copy itself to TEMP, spawn a copy from there, and exit. The spawned copy would then launch process B and wait for it.
Thx for the first help, but I still need some more help.
Try chaning the current directory (to C:\ProgramA) before staring the program.
How can I change the directory??
I was searching for it and found SetCurrentDirectory on the msdn site, which is included in the mscorlib.dll which comes with VS. As I use Netbeans with GCC I don't have this library and couldn't find a download for it on msdn.
Is there a another library I could use with similar functions or should I search the dll and just copy it in my \bin folder?
A way to achieve the latter would be for your application to copy itself to TEMP, spawn a copy from there, and exit. The spawned copy would then launch process B and wait for it.
I erased the waiting function from programA for the spawned programmB, so, program A exists (process finished in task manager) and prgram B keeps running. But I still cannot savely remove the USB, so that there is still a dependency, I guess making a copy to the temp folder and then starting B, should lead to the same result?
SetCurrentDirectory() is a function of kernel32.dll, and it is declared in windows.h. But this is not what you need because this function sets the current directory for the calling process, not an external process.
I also checked the STARTUPINFO structure and I was wrong in thinking that the working directory was set using this structure. Instead it is set in the call to CreateProcess(). My bad.
If after removing WaitForSingleObject() and setting the directory of program B to something outside the USB stick you still cannot remove the USB stick, then you are missing something.
Is program B located in the USB stick too? If so, that is a problem. You need to run it from a location outside the USB stick.
I put the folder of the process that I start on the HDD, which can be defined in the parameters of CreateProcess() (have already be done before, see code in first post) and then I set the current directory for both applications (A and B) on the C:\ drive.
It is not always possbile to remove the USB stick savely, but at least in my experience this is a general Windows problem, often I can't remove it savely, even all programs accesing files from the USB are closed. So, in the worst case, I just take out the USB even Windows cannot disconnect it.