- #include <windows.h>
- BOOL SetPrivilege(
- HANDLE hToken, // access token handle
- LPCTSTR lpszPrivilege, // name of privilege to enable/disable
- BOOL bEnablePrivilege // to enable or disable privilege
- )
- {
- TOKEN_PRIVILEGES tp;
- LUID luid;
- if ( !LookupPrivilegeValue(
- NULL, // lookup privilege on local system
- lpszPrivilege, // privilege to lookup
- &luid ) ) // receives LUID of privilege
- {
- printf("LookupPrivilegeValue error: %u\\n", GetLastError() );
- return FALSE;
- }
- tp.PrivilegeCount = 1;
- tp.Privileges[0].Luid = luid;
- if (bEnablePrivilege)
- tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
- else
- tp.Privileges[0].Attributes = 0;
- // Enable the privilege or disable all privileges.
- if ( !AdjustTokenPrivileges(
- hToken,
- FALSE,
- &tp,
- sizeof(TOKEN_PRIVILEGES),
- (PTOKEN_PRIVILEGES) NULL,
- (PDWORD) NULL) )
- {
- printf("AdjustTokenPrivileges error: %u\\n", GetLastError() );
- return FALSE;
- }
- if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
- {
- printf("The token does not have the specified privilege. \\n");
- return FALSE;
- }
- return TRUE;
- }
- int WINAPI WinMain( __in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd )
- {
- HANDLE hToken;
- if (!OpenProcessToken(GetCurrentProcess(),
- TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_PRIVILEGES, &hToken))
- return -1;
- if(!SetPrivilege(hToken, SE_SYSTEMTIME_NAME, TRUE))
- {
- return -1;
- }
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/06122012852.html
来源: http://www.codesnippet.cn/detail/06122012852.html