kptn
Messages postés25Date d'inscriptionjeudi 20 novembre 2003StatutMembreDernière intervention 2 janvier 2006 5 déc. 2003 à 09:48
tu as tout à fait raison. J'avais créé ce programme, initialement, pour windows 98. Et comme je n'ai pas pris le temps de le mettre à jour, il est inutilisable en l'état sous win2k et +.
Qu'a cela ne tienne, en attendant de remettre à jour le code voici ce qu'il faut y mettre pour qu'il tienne compte des privilèges :
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
ReportError("OpenProcessToken");
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
ReportError("AdjustTokenPrivileges");
}
et à la place de l'ExitWindowsEx existant, il faut le remplacer par :
ExitWindowsEx(EWX_POWEROFF | EWX_FORCEIFHUNG, 0);
enfin la méthode ReportError est celle qui est communément employée :
void ReportError(char *szTitle)
{
int nerr = GetLastError();
if (nerr == 0) return;
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
nerr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
5 déc. 2003 à 09:48
Qu'a cela ne tienne, en attendant de remettre à jour le code voici ce qu'il faut y mettre pour qu'il tienne compte des privilèges :
void SetPrivilege(void)
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
// Get a token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
ReportError("OpenProcessToken");
// Get the LUID for the shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Get the shutdown privilege for this process.
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges.
ReportError("AdjustTokenPrivileges");
}
et à la place de l'ExitWindowsEx existant, il faut le remplacer par :
ExitWindowsEx(EWX_POWEROFF | EWX_FORCEIFHUNG, 0);
enfin la méthode ReportError est celle qui est communément employée :
void ReportError(char *szTitle)
{
int nerr = GetLastError();
if (nerr == 0) return;
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
nerr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
MessageBox(NULL, (LPCTSTR)lpMsgBuf, szTitle, MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
4 déc. 2003 à 15:44