Saturday, March 26, 2005


/*

Invisibly run a commond, probably a batch file.

created by : zlel
last edit : 26 Mar 2005 10:22:03
license : public domain

Sigh. When you really want something...
you just gotta code it yourself.

Compile under mingwc with
mingw32-gcc -o irun.exe irun.c -mwindows

*/

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance , \
HINSTANCE hPrevInstance , \
PSTR lpCmdLine , int nCmdShow)
{
PROCESS_INFORMATION ps;
static STARTUPINFO si;
BOOL res;
int commandlength;
char *commandline, *parameters, c, *mycommand;
int quoted, i, found;
char prefix[]="cmd /c ";

commandlength = strlen(GetCommandLine());
commandline = malloc(sizeof(char) * commandlength + 1);
mycommand = malloc(sizeof(char) * commandlength + \
1 + strlen(prefix) + 1);

strcpy(commandline, GetCommandLine());

quoted = 0;
found = 0;

for (i=0; i<strlen(commandline) && ; 0==found; i++) {
parameters = commandline + i;
c = parameters[0];
if (c == '"') quoted = 1 - quoted;
if (c == ' ' &&amp;amp;amp;amp; quoted==0) found = 1;
}

sprintf(mycommand, "%s %s", prefix, parameters);

si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;

if (0==found || !CreateProcess(NULL, \
mycommand, NULL, NULL, FALSE, \
0, NULL, NULL, &si, &ps)) {

MessageBox(NULL , TEXT("実行できません") , NULL , MB_OK);
}

free(mycommand);
free(commandline);
}

No comments: