dimanche 12 juillet 2009

exec() - CreateProcess() test

Here is a simple code that launches an application on the SD card.

#include <stdio.h>
#include <windows.h>
/*
** CarTrek 600
** CreateProcess() test
*/
#define prog_name TEXT("exec")
void msg(char *str1, char *str2)
{
static char txtbuf[512];
static wchar_t wtxtbuf[512];

sprintf(txtbuf, "%s %s (GetLastError()=%ld)", str1, str2, GetLastError());
mbstowcs(&wtxtbuf[0], &txtbuf[0], strlen(txtbuf)+1);
MessageBox(0, &wtxtbuf[0], prog_name, MB_OK);
}
/*===================================================================*/
PROCESS_INFORMATION ProcessInformation;
char *progname = "\\SDMMC\\gosm_arm_good.exe";

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
wchar_t argv0[80];
wchar_t wtxtbuf[128];

GetModuleFileName (NULL, argv0, sizeof (argv0) / sizeof (argv0[0]));
MessageBox(0, argv0, L"CeGCC says...", MB_OK);

mbstowcs(&wtxtbuf[0], progname, strlen(progname)+1);
if (!CreateProcess(&wtxtbuf[0], &wtxtbuf[0], NULL, NULL, FALSE, 0, NULL, NULL, NULL, &ProcessInformation))
msg(progname, "failed");
else msg(progname, "started");
}

NB: I was not able to launch '\Windows\explore.exe' by this method (and I don't know why nor what it should give).