1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <iostream>
#include <dirent.h>
#include <windows.h>
#include <winbase.h>
#define JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE 0x00002000
#define CREATE_PRESERVE_CODE_AUTH_LEVEL 0x02000000
using namespace std;
int main(int argc, char** argv)
{
int res = 0;
// C'est un répertoire où se situe le fichier exe que je veux lancer mais à vous de le changer
res = chdir("C:\\Program Files (x86)\\test_hello world\\bin");
if (res != 0)
{
cout << "error";
}
cout << "Enter in directory ok" << endl;
PROCESS_INFORMATION pi = {0};
STARTUPINFO si = {0};
si.cb = sizeof(si);
HANDLE ghJob = CreateJobObject( NULL, NULL); // GLOBAL
if ( ghJob == NULL)
{
cout << "CreateJobObject failed " << GetLastError() << endl;
return -1;
}
else
{
cout << "CreateJobObject OK" << endl;
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli = {{{{ 0 }}}};
// Configure all child processes associated with the job to terminate when the
jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if ( 0 == SetInformationJobObject( ghJob, JobObjectExtendedLimitInformation, &jeli, sizeof(jeli)))
{
CloseHandle(ghJob);
cout << "SetInformationJobObject failed " << GetLastError() << endl;
return -1;
}
}
// nom de l'executable à lancer qui se trouve dans le répertoire "C:\\Program Files (x86)\\test_hello world\\bin"
std::string exe = "toto.exe";
res = CreateProcess(NULL,
(char*)exe.c_str(),
NULL,
NULL,
TRUE,
//CREATE_NEW_CONSOLE,
CREATE_NO_WINDOW | CREATE_BREAKAWAY_FROM_JOB | CREATE_PRESERVE_CODE_AUTH_LEVEL,
NULL,
NULL,
&si,
&pi);
if (res == 0)
{
cout << "CreateProcess: GetlastError = " << GetLastError() << endl;
}
else cout << "CreateProcess OK (" << res << ")" << endl;
if (ghJob && pi.hProcess)
{
if (0 == AssignProcessToJobObject( ghJob, pi.hProcess))
{
cout << "AssignProcessToJobObject failed " << GetLastError();
//return -1;
}
else cout << "AssignProcessToJobObject OK" << endl;
}
else cout << "Cannot AssignProcessToJobObject (CreateProcess failed...)" << endl;
//normalement il faut fermer les handle mais c'est juste pour le test...
system("pause");
return 0;
} |
Partager