I am trying to set security of PROCESS_TERMINATE. This is the code:
CreateProcess("C:\\ADP\\SQLBase\\dbntsrv.exe", NULL, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, "C:\\ADP\\SQLBase", &si, &pi);
if(SetSecurityInfo(pi.hProcess, SE_KERNEL_OBJECT, PROCESS_TERMINATE, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
MessageBox(NULL, "process_terminate granted", NULL, MB_OK);
}
else
{
MessageBox(NULL, "process_terminate not granted", NULL, MB_OK);
}
//--------------------- Permission to query for info to use GetExitCode -------------------------
if(SetSecurityInfo(pi.hProcess, SE_KERNEL_OBJECT, PROCESS_QUERY_INFORMATION, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
MessageBox(NULL, "process_query_information granted", NULL, MB_OK);
}
else
{
MessageBox(NULL, "process_query_information not granted", NULL, MB_OK);
}
LPDWORD lpExitCode;
GetExitCodeProcess(pi.hProcess, lpExitCode);
Here SetSecurityInfo for for PROCESS_TERMINATE fails and I get an Unhandled Exception..(KERNEL32.dll):Access Violation for
GetExitCodeProcess(pi.hProcess, lpExitCode);
Why does this happen? Thank You
The access violation is because of this code:
Here you declare that
lpExitCodeis a pointer, but you don’t make it point at anything. WhenGetExitCodeProcesstries to write to*lpExitCodeit results in an access violation.The correct approach is like this:
I also don’t believe that you need to call
SetSecurityInfoat all. The process handle thatCreateProcessreturns should have sufficient rights.You will need to wait for the spawned process to terminate before you can expect to get an exit code. This is because
GetExitCodeProcessis asynchronous. You can wait like this:And do remember to check all your API calls for errors.