#include #include #include long win32_execute_binary(char *cwd, char *cmd, char *envp[]) { HANDLE hndl, opfp=0; int i,r,append2file=0; STARTUPINFO StartupInfo={0}; PROCESS_INFORMATION ProcessInformation={0}; GetStartupInfo(&StartupInfo); StartupInfo.dwFlags = STARTF_USESHOWWINDOW; StartupInfo.wShowWindow = SW_HIDE; StartupInfo.cb = sizeof(STARTUPINFO); /* add new ENV variables - not thread safe tho. */ for (i=0;envp && envp[i];i++) { _putenv(envp[i]); } /* Something to do with bInheritHandles being FALSE, as when it is TRUE, it works. */ CreateProcess(0, cmd, 0, 0, FALSE, DETACHED_PROCESS | NORMAL_PRIORITY_CLASS, 0, cwd, &StartupInfo, &ProcessInformation); hndl = ProcessInformation.hProcess; if (!hndl) r = GetLastError(); /* wait for it to finish */ { DWORD w; w= WaitForSingleObject(hndl, INFINITE); if (w == WAIT_FAILED) r = GetLastError(); } // handles must be closed as not used if (ProcessInformation.hProcess) CloseHandle(ProcessInformation.hProcess); if (ProcessInformation.hThread) CloseHandle(ProcessInformation.hThread); return (long)hndl; } int main(int arc, char *argv[], char *envp[]) { long ret = 0; ret = win32_execute_binary("/home/Administrator", "ls", envp); printf("Return val = %ld\n", ret); exit(0); }