advice on a launcher app (spawning process)

Started by
2 comments, last by noisecrime 19 years, 6 months ago
Hi, (Doh - thought i was in the general programming forum- can be moved if a mod wants to) For reasons I wont bother going into (but that can't be changed)I need to have a small launcher.exe to run my main program. Essentially the launcher.exe must do two things. First find and rename a speciifc file in the same directory, then launch the main.exe. However i've not worked with windows much and so much of what I need is new to me. I've tracked down the functions that I 'think' I should be using (via msdn), but i'm conerned that they may not be the most suitable. I came up with using the 'rename' function from stdio.h and _spawnl (process.h) to create and execute the main program. My code basically looks like

void main( void )
{
    int  result;
	char oldStr[] = "aCoolFileName";
	char newStr[] = "aNewFileName";
	char exeStr[] = "Main.exe";

	// Attempt to rename file: 
    result = rename( oldStr, newStr );

	// Maybe do something different based on result

	// Launch the main exe
	_spawnl( _P_NOWAIT, exeStr, NULL );
}
Now i'm quite happy with the renaming, that seams to work fine, i'm not so sure about the spawn command. From what i've read the P_NOWAIT appears the way to go, as I want this program to continue and termintate as soon as my main.exe is launched. So is there anything terribly wrong with this? I did find one issue in that whilst a shortcut of the launcher would work when dragged to the desktop, a shortcut generated from NSIS (installer app) had its start level a few directories up and led to the rename file failing. I managed to fix this from within the installer code, however I also considered using 'GetCurrentDirectory' so as to provide an absolute path. Anyone think its better to go with an absolute path? Finally I really need to make sure that only one instance of the launcher can be created at any one time, but further that the launcher can't be run, if the main.exe is running. That is I never want more than a single instance of main.exe running at one time. Any pointers as to the best way to go about doing this? I could simply store a flag in a file, but that seams a little flimsy and easily broken. Possibly better to check if the main.exe process is actually running or something similar. thanks
Advertisement
For the multiple-instance problem, you can try Mutex checking.
//Check for more than 1 instance    HANDLE hMutex = CreateMutex(NULL, TRUE, "program");    if(GetLastError() == ERROR_ALREADY_EXISTS)    {        MessageBox(hwnd,"Program is already running", "Error",MB_OK | MB_ICONERROR);        return 0;    }    else    {        //Run Program    }


Of course you should probably do more error checking but this is basically it. You will need to include windows.h, maybe another one that I forgot, and compile as normal.

You can also check out "CreateProcess()" and "WinExec()" under the Win32 API.
Quote:Original post by Xiachunyi
For the multiple-instance problem, you can try Mutex checking.

...code block...

Of course you should probably do more error checking but this is basically it. You will need to include windows.h, maybe another one that I forgot, and compile as normal.


//Check for more than 1 instance    HANDLE hMutex = CreateMutex(NULL, TRUE, "program");    DWORD dwError = GetLastError()    if(ERROR_ALREADY_EXISTS != dwError)    {        // Run Program    }    else    {        // Use could use FormatMessage() for a more specific error message        return 0;    }


That way you catch any type of error, and you can use the Windows API to help explain what error occured.

You could also use ShellExecute() to execute your program.

Thanks

That looks like what I need, I'll look up the stuff mentioned.

This topic is closed to new replies.

Advertisement