Call prgramm from a program and close calling program

Hello,

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.

I used a code from the msdn homepage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
/*
 * 
 */
int main(int argc, char** argv) {


    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    if(!CreateProcess("C:\\ProgramA\\ProgramA.exe",
        NULL,           // Command line
        NULL,           // Process handle not inheritable
        NULL,           // Thread handle not inheritable
        FALSE,          // Set handle inheritance to FALSE
        0,              // No creation flags
        NULL,           // Use parent's environment block
        "C:\\ProgramB", // changed the option from NULL to the folder where B is 
       &si,            // Pointer to STARTUPINFO structure
        &pi )           // Pointer to PROCESS_INFORMATION structure
    )
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return 0;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );


    return (EXIT_SUCCESS);
}


Could be that my idea is completly wrong, so I am glad about any advice.



Try chaning the current directory (to C:\ProgramA) before staring the program.
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.

http://msdn.microsoft.com/en-us/library/aa365530%28VS.85%29.aspx

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.
Ok, now it is working more or less...

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.

Thanks a lot for your help!!
Topic archived. No new replies allowed.