我是靠谱客的博主 想人陪棒棒糖,最近开发中收集的这篇文章主要介绍用VC6.0实现定时关机功能OSVERSIONINFOLUIDOpenProcessTokenLookupPrivilegeValueAdjustTokenPrivileges,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


                                                                   定时关机功能的实现

SetTimer设定一个定时器,在定时器消息映射函数OnTimer中用CTime::GetCurrentTime得到当前系统时间,判断是否到达要求时间,如果到达则关机。  
  OSVERSIONINFO   OsVersionInfo;       //
包含操作系统版本信息的数据结构  
  OsVersionInfo.dwOSVersionInfoSize   =   sizeof(OSVERSIONINFO);  
  GetVersionEx(&OsVersionInfo);         //
获取操作系统版本信息  
 if(OsVersionInfo.dwPlatformId   !=   VER_PLATFORM_WIN32_WINDOWS)   //
对于Windows NT系统
  {  
          HANDLE   hToken;  //  
指向 access token的指针。 
          TOKEN_PRIVILEGES   tkp;     //
保存用户权限列表的数据结构。
           
          //   Get   a   token   for   this   process.  
得到一个进程的access token,并将它放入到hToken中。 
           
          if   (!OpenProcessToken(GetCurrentProcess(),    
                  TOKEN_ADJUST_PRIVILEGES   |   TOKEN_QUERY,   &hToken))    
                  AfxMessageBox("OpenProcessToken");    
                   
          //   Get   the   LUID   for   the   shutdown   privilege.  
得到关机权限的LUID,并且放入到TOKEN_PRIVILEGES结构体的第一个列表项中。  
                   
          LookupPrivilegeValue(NULL,   SE_SHUTDOWN_NAME,    
                  &tkp.Privileges[0].Luid);    
           
          tkp.PrivilegeCount   =   1;     //   one   privilege   to   set  
将关机权限对应的LUID的对应属性改为可用   SE_PRIVILEGE_ENABLED;    
          tkp.Privileges[0].Attributes   =   SE_PRIVILEGE_ENABLED;    
                   
          //   Get   the   shutdown   privilege   for   this   process.  
调整进程的access token,使它具有shutdown权限的可用属性。 
                   
          AdjustTokenPrivileges(hToken,   FALSE,   &tkp,   0,    
                  (PTOKEN_PRIVILEGES)NULL,   0);    
           
          //   Cannot   test   the   return   value   of   AdjustTokenPrivileges.    
           
          if   (GetLastError()   !=   ERROR_SUCCESS)    
                  AfxMessageBox("AdjustTokenPrivileges");    
                   
          //   Shut   down   the   system   and   force   all   applications   to   close.    
                   
          if   (!ExitWindowsEx(EWX_SHUTDOWN   |EWX_POWEROFF,   0))    
                        //EWX_REBOOT,EWX_LOGOFF             (uFlags)  
                //EWX_FORCE,EWX_FORCEIFHUNG     (Value)  
                        AfxMessageBox("ExitWindowsEx");    
           
                   
  }  
  else  
  {  
          //Windows98,
调用ExitWindowsEx()函数重新启动计算机  
          ExitWindowsEx(EWX_SHUTDOWN   |EWX_POWEROFF,0);     //
可以改变第一个参数,实现注销用户、    
          //
关机、关闭电源等操作  
                   
                   
  } 

下面是MSDN上面的相关资料。

1.什么是Access Token

   Access Token用来标识一个用户,其中包括用户的SID和用户所属组的SID,还包括这个用户和所属用户组的所拥有的权限列表。

当用户输入用户名和密码登陆到Windows之后,Windows会创建一个Acess Token,用来标识这个用户。 在这个用户下创建的进程都将得到一份这个Access Token的备份。当进程需要访问一个可以加密的对象(如文件,事件等)时,windows利用这个Access Token来跟对象的Security Descriptor进行比较,以确认这个进程是否有权限来访问这个对象。(关于SIDSecurity Descriptor请参阅其他资料)

 

OSVERSIONINFO

The OSVERSIONINFO data structure contains operating system version information. The information includes major and minor version numbers, a build number, a platform identifier, and descriptive text about the operating system. This structure is used with the GetVersionEx function.用来保存操作系统版本的一个结构。

typedef struct _OSVERSIONINFO{
    DWORD dwOSVersionInfoSize;
    DWORD dwMajorVersion;
    DWORD dwMinorVersion;
    DWORD dwBuildNumber;
    DWORD dwPlatformId;
    TCHAR szCSDVersion[ 128 ];
} OSVERSIONINFO;
 

Members

dwOSVersionInfoSize

Specifies the size, in bytes, of this data structure. Set this member to sizeof(OSVERSIONINFO) before calling the GetVersionEx function.

dwMajorVersion

Identifies the major version number of the operating system. For example, for Windows NT version 3.51, the major version number is 3; and for Windows NT version 4.0, the major version number is 4.

dwMinorVersion

Identifies the minor version number of the operating system. For example, for Windows NT version 3.51, the minor version number is 51; and for Windows NT version 4.0, the minor version number is 0.

dwBuildNumber

Windows NT: Identifies the build number of the operating system.

Windows 95: Identifies the build number of the operating system in the low-order word. The high-order word contains the major and minor version numbers.

dwPlatformId

Identifies the operating system platform. This member can be one of the following values:

Value

Platform

VER_PLATFORM_WIN32s

Win32s on Windows 3.1.

VER_PLATFORM_WIN32_WINDOWS

Win32 on Windows 95 or Windows 98.

For Windows 95, dwMinorVersion is zero.

For Windows 98, dwMinorVersion is greater than zero.

VER_PLATFORM_WIN32_NT

Win32 on Windows NT.

 

 

 

szCSDVersion

Windows NT: Contains a null-terminated string, such as "Service Pack 3", that indicates the latest Service Pack installed on the system. If no Service Pack has been installed, the string is empty.

Windows 95: Contains a null-terminated string that provides arbitrary additional information about the operating system.

 

 

下面是MSDN上面对TOKEN_PRIVILEGES的解释:

The TOKEN_PRIVILEGES structure contains information about a set of privileges for an access token. 这个结构用来包含一个access token用户的权限列表。

typedef struct _TOKEN_PRIVILEGES { // tp

    DWORD PrivilegeCount;

    LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY];

} TOKEN_PRIVILEGES;

下面是对成员的介绍:

Members

PrivilegeCount

Specifies the number of entries in the Privileges array.这个成员用来跟数组配套。

Privileges

Specifies an array of LUID_AND_ATTRIBUTES structures. Each structure contains the LUID and attributes of a privilege. The attributes of a privilege can be a combination of the following values:

这是一个LUID_AND_ATTRIBUTES结构的数组。每个结构包含一个LUID和权限的属性,属性由下列值组成:

Attribute

Description

SE_PRIVILEGE_ENABLED_BY_DEFAULT

 

The privilege is enabled by default.

SE_PRIVILEGE_ENABLED

 

The privilege is enabled.

SE_PRIVILEGE_USED_FOR_ACCESS

 

The privilege was used to gain access to an object or service. This flag is used to identify the relevant privileges in a set passed by a client application that may contain unnecessary privileges.

 

下面是MSDN上对LUID的解释:

LUID

An LUID is a 64-bit value guaranteed to be unique only on the system on which it was generated. The uniqueness of a locally unique identifier (LUID) is guaranteed only until the system is restarted. 它是一个64位的值,一个LUID在系统里面是唯一的,直到系统重启。

An LUID is not for direct manipulation. Applications are to use functions and structures to manipulate LUID values. 一个LUID是不能被直接操纵的,应用程序通过调用函数和结构来操纵它的值。

typedef LARGE_INTEGER LUID

 

 

 

 

OpenProcessToken

The OpenProcessToken function opens the access token associated with a process. 这个函数打开跟一个进程相关的access token

BOOL OpenProcessToken(
  HANDLE ProcessHandle, // handle to process
  DWORD DesiredAccess,  // desired access to process
  PHANDLE TokenHandle   // pointer to handle of open access token
);
 

Parameters

ProcessHandle

Identifies the process whose access token is opened.

DesiredAccess

Specifies an access mask that specifies the requested types of access to the access token. These requested access types are compared with the token's discretionary access-control list (ACL) to determine which accesses are granted or denied. The following access rights have been defined for access tokens.

Value

Meaning

TOKEN_ADJUST_DEFAULT

Required to change the default ACL, primary group, or owner of an access token.

TOKEN_ADJUST_GROUPS

Required to change the groups specified in an access token.

TOKEN_ADJUST_PRIVILEGES

Required to change the privileges specified in an access token.

TOKEN_ALL_ACCESS

Combines the STANDARD_RIGHTS_REQUIRED standard access rights and all individual access rights for tokens.

TOKEN_ASSIGN_PRIMARY

Required to attach a primary token to a process in addition to the SE_CREATE_TOKEN_NAME privilege.

TOKEN_DUPLICATE

Required to duplicate an access token.

TOKEN_EXECUTE

Combines the STANDARD_RIGHTS_EXECUTE standard access rights and the TOKEN_IMPERSONATE access right.

TOKEN_IMPERSONATE

Required to attach an impersonation access token to a process.

TOKEN_QUERY

Required to query the contents of an access token.

TOKEN_QUERY_SOURCE

Required to query the source of an access token.

TOKEN_READ

Combines the STANDARD_RIGHTS_READ standard access rights and the TOKEN_QUERY access right.

TOKEN_WRITE

Combines the STANDARD_RIGHTS_WRITE standard access rights and the TOKEN_ADJUST_PRIVILEGES, TOKEN_ADJUST_GROUPS, and TOKEN_ADJUST_DEFAULT access rights.

 

TokenHandle

Pointer to a handle identifying the newly-opened access token when the function returns.

Return Values

If the function succeeds, the return value is nonzero.如果函数调用成功,那么返回非零值

If the function fails, the return value is zero. To get extended error information, call GetLastError. 如果调用失败,返回0.

 

LookupPrivilegeValue

The LookupPrivilegeValue function retrieves the locally unique identifier (LUID) used on a specified system to locally represent the specified privilege name. 这个函数用来重新得到在当前系统中,权限名字所对应的LUID号。

BOOL LookupPrivilegeValue(
  LPCTSTR lpSystemName,
                   // address of string specifying the system
  LPCTSTR lpName,  // address of string specifying the privilege
  PLUID lpLuid     // address of locally unique identifier
);
 

Parameters

lpSystemName

Pointer to a null-terminated string specifying the name of the system on which the privilege name is looked up. If a null string is specified, the function attempts to find the privilege name on the local system. 指定要在哪个系统中查找权限所对应的LUID,如果为NULL,则表示默认为当前系统。

lpName

Pointer to a null-terminated string that specifies the name of the privilege, as defined in the WINNT.H header file. For example, this parameter could specify the constant SE_SECURITY_NAME, or its corresponding string, "SeSecurityPrivilege". 指定所要查询的权限的名字。

lpLuid

Pointer to a variable that receives the locally unique identifier by which the privilege is known on the system specified by the lpSystemName parameter. 指定用来保存LUID的结构的指针。

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

 

AdjustTokenPrivileges

The AdjustTokenPrivileges function enables or disables privileges in the specified access token. Enabling or disabling privileges in an access token requires TOKEN_ADJUST_PRIVILEGES access. 这个函数用来使某个权限能用或者禁止某个权限。要求第一个参数要有TOKEN_ADJUST_PRIVILEGES

BOOL AdjustTokenPrivileges(
  HANDLE TokenHandle,  // handle to token that contains privileges
  BOOL DisableAllPrivileges,
                       // flag for disabling all privileges
  PTOKEN_PRIVILEGES NewState,
                       // pointer to new privilege information
  DWORD BufferLength,  // size, in bytes, of the PreviousState buffer
  PTOKEN_PRIVILEGES PreviousState,
                       // receives original state of changed
                       // privileges
  PDWORD ReturnLength  // receives required size of the
                       // PreviousState buffer
);
 

Parameters

TokenHandle

Identifies the access token that contains the privileges to be modified. The handle must have TOKEN_ADJUST_PRIVILEGES access to the token. If the PreviousState parameter is not NULL, the handle must also have TOKEN_QUERY access.

DisableAllPrivileges

Specifies whether the function disables all of the token's privileges. If this value is TRUE, the function disables all privileges and ignores the NewState parameter. If it is FALSE, the function modifies privileges based on the information pointed to by the NewState parameter.

NewState

Pointer to a TOKEN_PRIVILEGES structure that specifies an array of privileges and their attributes. If the DisableAllPrivileges parameter is FALSE, AdjustTokenPrivileges enables or disables these privileges for the token. If you set the SE_PRIVILEGE_ENABLED attribute for a privilege, the function enables that privilege; otherwise, it disables the privilege.

If DisableAllPrivileges is TRUE, the function ignores this parameter.

BufferLength

Specifies the size, in bytes, of the buffer pointed to by the PreviousState parameter. This parameter can be NULL if the PreviousState parameter is NULL.

PreviousState

Pointer to a buffer that the function fills with a TOKEN_PRIVILEGES structure containing the previous state of any privileges the function modifies. This parameter can be NULL.

If you specify a buffer that is too small to receive the complete list of modified privileges, the function fails and does not adjust any privileges. In this case, the function sets the variable pointed to by the ReturnLength parameter to the number of bytes required to hold the complete list of modified privileges.

ReturnLength

Pointer to a variable that receives the required size, in bytes, of the buffer pointed to by the PreviousState parameter. This parameter can be NULL if PreviousState is NULL.

Return Values

If the function succeeds, the return value is nonzero. To determine whether the function adjusted all of the specified privileges, call GetLastError, which returns one of the following values when the function succeeds:

Value

Description

ERROR_SUCCESS

The function adjusted all specified privileges.

ERROR_NOT_ALL_ASSIGNED

The token does not have one or more of the privileges specified in the NewState parameter. The function may succeed with this error value even if no privileges were adjusted. The PreviousState parameter indicates the privileges that were adjusted.

 

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The AdjustTokenPrivileges function cannot add new privileges to the access token. It can only enable or disable the token's existing privileges. To determine the token's privileges, call the GetTokenInformation function.

Note that the NewState parameter can specify privileges that the token does not have, without causing the function to fail. In this case, the function adjusts the privileges that the token does have, ignores the other privileges, and returns success. Call the GetLastError function to determine whether the function adjusted all of the specified privileges. The PreviousState parameter indicates the privileges that were adjusted.

The PreviousState parameter retrieves a TOKEN_PRIVILEGES structure containing the the original state of the adjusted privileges. To restore the original state, pass the PreviousState pointer as the NewState parameter in a subsequent call to the AdjustTokenPrivileges function

 

最后

以上就是想人陪棒棒糖为你收集整理的用VC6.0实现定时关机功能OSVERSIONINFOLUIDOpenProcessTokenLookupPrivilegeValueAdjustTokenPrivileges的全部内容,希望文章能够帮你解决用VC6.0实现定时关机功能OSVERSIONINFOLUIDOpenProcessTokenLookupPrivilegeValueAdjustTokenPrivileges所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部