1

I need a very simple program to run on any version of Windows, let's say >= Win 98, without requiring any pre-installed framework like dotnet. I thought C would be a great idea to do this.

The program should start a process from the parent directory by using a system command.

Start C program (invisible) > program starts process > program exits

This is how it looks:

#include <stdio.h>
#include <stdlib.h>

int main() {
    system("..\\someprogram.exe");
    return 0;
}

I call this program from a Flash projector, which only allows to start programs in a specific subfolder "fscommand" – but I have to start a process located in the same directory as the projector.

Anyway, it works fine! But the C program opens a command box, then starts the process and leaves the command box open as long as the process runs. So here is how it should work, in order how i would appreciate it:

  1. Do not open a command box at all (I'd like that, really ;)
  2. Both 3) and 4)
  3. Close the command box after starting the process (exit the C program)
  4. Open the command box minimized by default

I can't change any Windows settings for the C executable or use a shortcut, as this will run directly from a CD later.

I use Open Watcom to compile my program. Both image types (target options) that produce an executable (Character-mode Executable / Windowed Executable) have the same result.

Bert
  • 13
  • 3
  • Did you see this? http://stackoverflow.com/questions/411247/running-a-cmd-or-bat-in-silent-mode A batch file should do the trick. – beatgammit Apr 17 '11 at 09:07
  • or this? http://superuser.com/questions/62525/run-a-completly-hidden-batch-file – Tarnay Kálmán Apr 17 '11 at 09:18
  • Unfortunately Flash does not allow to call an executable with arguments for security reasons. Only exe- and bat-calls without arguments are possible. (That was one of the greatest ideas Adobe ever had.) I had a nice solution using a batch file instead of the C-program. This was working when starting the bat manually. But when calling it from Flash, it just didn't start the process. -.- – Bert Apr 17 '11 at 11:00

6 Answers6

1

I did a google search and found http://www.ntwind.com/software/utilities/hstart.html

Your using a console app, you could change it to a windows app using winmain()

You can use a shortcut to a file in the same folder, not sure why your discounting that method.

start will give you a fork so your intermediate app can close - not sure about win98 tho.

system("start ..\\someprogram.exe");

Instead of system you can use createProcess to launch the app, theis will avoid the system commands console.

STARTUPINFO si;
PROCESS_INFORMATION pi;

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

// Start the child process. 
if( !CreateProcess( "..\\someprogram.exe",   // module name 
    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
    NULL,           // Use parent's starting directory 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
) 
{
    printf( "CreateProcess failed (%d).\n", GetLastError() );
    return;
}

// Wait until child process exits.  In your case you don't care to wait anyway
// WaitForSingleObject( pi.hProcess, INFINITE );

// Close process and thread handles. 
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
Greg Domjan
  • 13,307
  • 6
  • 39
  • 59
  • Using WinMain() sounds logical, but still produces a command box. :( Addes line: `#include ` and `int WINAPI WinMain (HINSTANCE hI, HINSTANCE hPrI, PSTR szCmdLine, int iCmdShow)` – Bert Apr 17 '11 at 11:42
  • This little change `system("start ..\\someprogram.exe");` fits #3 of the behaviors I listet. :) I didn't try the rest of it, because I found a much simpler solution in the meantime. – Bert Apr 21 '11 at 18:17
0

I think the _exec and/or _spawn functions do what you need, though I'm not sure.

If not, you can always use CreateProcess, though it can be a little more tedious in some ways.

user541686
  • 189,354
  • 112
  • 476
  • 821
0

You could (for example) use hstart instead of your own program to start that exe.

(This would result in no black box at all.)

Tarnay Kálmán
  • 6,682
  • 5
  • 41
  • 55
  • This would be great, but Flash doesn't support arguments for application calls (see my comment above). :( – Bert Apr 17 '11 at 11:10
0

CreateProcess with CREATE_NO_WINDOW flag is what you want, but I want to add something. To support also cmd style commands (such as DIR, SET, ... ) which have no executables and can't be passed to CreateProcess alone, you should call cmd.exe /C someprogram where someprogram is name of executable, bat file, or command.

Mihran Hovsepyan
  • 10,110
  • 13
  • 57
  • 108
0

The console window shows up because you built your program as a console application. I don't know how to avoid that in C, but in Delphi is was a simple {$Console Off} pragma in the project file.

GCC has a command line option -mwindows, which I think achieves the same, so you could search into this direction.

Roland Illig
  • 37,193
  • 10
  • 75
  • 113
  • Sounds good to me, though I didn't find a way to do something like that in Open Watcom. :) – Bert Apr 21 '11 at 18:20
0

A friend came up with a completely different solution. Now I use AutoIt with a short compiled script to start the process. This is very simple and the launcher process is completely invisible. :)

filename = ..\someprogram.exe

if FileExist(filename) {    
    Run, open %filename%
}
Bert
  • 13
  • 3