Re: VC++ NT SHUT DOWN

4 отговора 236 прегледа
#1

Re: VC++ NT SHUT DOWN

Thread handle mojesh da go vzemesh s OpenThread (iska thread ID). Ako stava duma za tekushtija thread - GetCurrentThread

Za OpenThreadToken.
Windows XP Professional: If the computer is joined to a workgroup and the "Force network logons using local accounts to authenticate as Guest" policy is enabled, the function fails. Note that this policy is enabled by default for a computer running Windows XP Professional that is joined to a workgroup.

Tova li e sluchaja ?

Отговор
#2

Re: VC++ NT SHUT DOWN

Btw, zashto ti trjabva da set-nesh na konkreten thread token-a ?
Set-ni go na process-a i mu kaji, che se apply-va za vseki thread v ramkite na tozi process. Posle ako se naloji, shte kajesh RevertToSelf(). ( Dano tova da e problema, de, toest dano pravilno da sym te razbral )

Eto malko code.

/*++

Routine Name:
AdjustThreadPrivilege

Routine Description:
Enable or disable a privalage for the calling thread.

Arguments
pPrivilegeName Name of the privalage
Enable True to enable the privalage. False to disable.

Return:
TRUE on Success; else FALSE.

--*/
BOOL AdjustProcessPrivilege(PCTSTR pPrivilegeName, BOOL Enable)
{
DWORD ErrorCode = NO_ERROR;
HANDLE hToken = NULL;
TOKEN_PRIVILEGES NewPrivileges;
LUID Luid;

ITASSERT(pPrivilegeName != NULL);

// Open the security token of the current process
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
{
ErrorCode = GetLastError();
DBGMSG(DBG_ERROR, TEXT("OpenThreadToken failed. Error: %u"), ErrorCode);
goto CleanUp;
}

// Convert privilege name to an LUID.
if (!LookupPrivilegeValue(NULL, pPrivilegeName, &Luid))
{
ErrorCode = GetLastError();
DBGMSG(DBG_ERROR, TEXT("LookupPrivilegeValue failed. Error: %u"), ErrorCode);
goto CleanUp;
}

// Construct NewPrivileges to enable / disable the privilege.
NewPrivileges.PrivilegeCount = 1;
NewPrivileges.Privileges[0].Luid = Luid;
NewPrivileges.Privileges[0].Attributes = (Enable) ? SE_PRIVILEGE_ENABLED : 0;

// Adjust the privileges.
if (!AdjustTokenPrivileges(hToken, FALSE, &NewPrivileges, 0, NULL, NULL))
{
ErrorCode = GetLastError();
DBGMSG(DBG_ERROR, TEXT("AdjustTokenPrivileges failed. Error: %u"), ErrorCode);
goto CleanUp;
}

CleanUp:
if (hToken != NULL)
{
CloseHandle(hToken);
}

if (ErrorCode == NO_ERROR)
{
return TRUE;
}

SetLastError(ErrorCode);
return FALSE;
}

Отговор
в отговор на manski #3

Re: VC++ NT SHUT DOWN

BLAGODARJA!!!
Az ne sum siguren kakvo to4no mi trjabva !! Prosto iskam v edna moja programka da shut downa Win-a . S W9X problemi njama ,no se okaza 4e za NT mi trjabva AdjustTokenPrivileges s ... i taka se zagubih v help-a i sled ,2h neuspeshni opiti reshih da popitam :)) !!!
BLAGODARJA oshte vednug - tazi ve4er shte probvam - dano stane!!

Отговор
#4

Re: VC++ NT SHUT DOWN

Ami na OpenThreadTojken triabva da mu slojish i TOKEN_QUERY. Eto ti napravo nesto deto bachka.


int main(int argc, char* argv[])
{
HANDLE hToken; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure

BOOL fResult; // system shutdown flag

// Get the current process token handle so we can get shutdown
// privilege.

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return 2;

// Get the LUID for 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 shutdown privilege for this process.

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);

// Cannot test the return value of AdjustTokenPrivileges.

if (GetLastError() != ERROR_SUCCESS)
return 2;

// Display the shutdown dialog box and start the time-out countdown.

fResult = InitiateSystemShutdown(NULL, NULL, 20, TRUE, TRUE);

if (!fResult)
return 2;

// Disable shutdown privilege.

tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);

if (GetLastError() != ERROR_SUCCESS)
return 2;

return 0;
}

Отговор
в отговор на dsgfsd #5

Re: VC++ NT SHUT DOWN

BLAGODARJ!!!
Primera raboti perfectno!!!!
Ne se setih zda polzvam OpenProcessToken ,no v helpa pisheshe za OpenTreadToken ... :(((
BLAGODARJA!!!

Отговор