改写一个C++程序碰到的问题(50分)

  • 主题发起人 主题发起人 AM_WJ
  • 开始时间 开始时间
A

AM_WJ

Unregistered / Unconfirmed
GUEST, unregistred user!
这是我在VC知识库里找到的一段C++关机程序,我用DP改写<br>TOKEN_PRIVILEGES tkp;<br>HANDLE hToken;<br>&nbsp; &nbsp;if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &amp;hToken)) <br>&nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; MessageBox("OpenProcessToken failed!");<br>&nbsp; &nbsp;}<br>&nbsp; &nbsp;LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&amp;tkp.Privileges[0].Luid); //获得本地机唯一的标识<br>&nbsp; &nbsp;tkp.PrivilegeCount = 1; &nbsp;<br>&nbsp; &nbsp;tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; <br>&nbsp; &nbsp;AdjustTokenPrivileges(hToken, FALSE, &amp;tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0); //调整获得的权限<br>&nbsp; &nbsp;if (GetLastError() != ERROR_SUCCESS) <br>&nbsp; {<br>&nbsp; &nbsp; MessageBox("AdjustTokenPrivileges enable failed!");<br>&nbsp; }<br>&nbsp; fResult =InitiateSystemShutdown( <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NULL, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 要关的计算机用户名,可在局域网网中关掉对方的机器,NULL表示关本机<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"由于系统不稳定,WINDOWS将在上面的时间内关机,请做好保存工作!", &nbsp;// 显示的消息<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;10, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 关机所需的时间<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TRUE, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TRUE); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //设为TRUE为重起,设为FALSE为关机<br>&nbsp; if(!fResult) <br>&nbsp; { <br>&nbsp; &nbsp; &nbsp; MessageBox("InitiateSystemShutdown failed."); <br>&nbsp; } <br>&nbsp; tkp.Privileges[0].Attributes = 0; <br>&nbsp; AdjustTokenPrivileges(hToken, FALSE, &amp;tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0); <br>&nbsp; if (GetLastError() != ERROR_SUCCESS) <br>&nbsp;{<br>&nbsp; &nbsp; &nbsp;MessageBox("AdjustTokenPrivileges disable failed."); <br>&nbsp;}<br>&nbsp; &nbsp; &nbsp;ExitWindowsEx(EWX_SHUTDOWN,0); &nbsp; &nbsp; //开始关机<br><br>改成dp版时<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; tkp: _TOKEN_PRIVILEGES;<br>&nbsp; hToken: Cardinal;<br>begin<br>&nbsp; if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken) then //创建一个进程<br>&nbsp; &nbsp; MessageDlg('OpenProcessToken failed!',mtError,[mbOK],0);<br>&nbsp; LookupPrivilegeValue(nil,'SE_SECURITY_NAME',tkp.Privileges[0].Luid); //获得本地机唯一的标识<br>&nbsp; tkp.PrivilegeCount:= 1;<br> tkp.Privileges[0].Attributes:= SE_PRIVILEGE_ENABLED;<br>&nbsp; AdjustTokenPrivileges(hToken, false, tkp, 0, (_TOKEN_PRIVILEGES)nil, 0);<br>end;<br>AdjustTokenPrivileges这个函数的PreviousState这个参数我(_TOKEN_PRIVILEGES)nil写不行应该怎么写呢?看API帮助说这里是<br>为null的,我直接写nil也不行.<br>那位大哥能把这段代码完整的翻成DP的吗?<br>
 
AdjustTokenPrivileges(hToken, false, tkp, 0, nil, 0)<br>最后一个参数传错了,必须传递一个变量,另外该函数<br>有两种声明,<br><br>function AdjustTokenPrivileges(TokenHandle: THandle; DisableAllPrivileges: BOOL;<br>&nbsp; const NewState: TTokenPrivileges; BufferLength: DWORD;<br>&nbsp; var PreviousState: TTokenPrivileges; var ReturnLength: DWORD): BOOL; stdcall; overload;<br>{$EXTERNALSYM AdjustTokenPrivileges}<br>function AdjustTokenPrivileges(TokenHandle: THandle; DisableAllPrivileges: BOOL;<br>&nbsp; const NewState: TTokenPrivileges; BufferLength: DWORD;<br>&nbsp; PreviousState: PTokenPrivileges; var ReturnLength: DWORD): BOOL; stdcall; overload;<br>
 
我改写了是可以通过了但是调整权限却失败了,我现在改写的如下:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; tkp: _TOKEN_PRIVILEGES;<br>&nbsp; hToken: Cardinal;<br>&nbsp; temp: Cardinal;<br>&nbsp; fResult: LongBool;<br>begin<br>&nbsp; if not OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,hToken) then //创建一个进程<br>&nbsp; &nbsp; MessageDlg('OpenProcessToken failed!',mtError,[mbOK],0);<br>&nbsp; LookupPrivilegeValue(nil,'SE_SECURITY_NAME',tkp.Privileges[0].Luid); //获得本地机唯一的标识<br>&nbsp; tkp.PrivilegeCount:= 1;<br> tkp.Privileges[0].Attributes:= SE_PRIVILEGE_ENABLED;<br>&nbsp; AdjustTokenPrivileges(hToken, false, tkp, 0, nil, temp);<br>&nbsp; if GetLastError() &lt;&gt; ERROR_SUCCESS then<br>&nbsp; &nbsp; MessageDlg('AdjustTokenPrivileges enable failed',mtError,[mbOK],0);<br>&nbsp; fResult:= InitiateSystemShutdown(nil,'关机',10,true,true);<br>&nbsp; if not fResult then<br>&nbsp; &nbsp; MessageDlg('InitiateSystemShutdown failed!',mtError,[mbOK],0);<br>&nbsp; tkp.Privileges[0].Attributes:= 0;<br>&nbsp; AdjustTokenPrivileges(hToken,false,tkp,0,nil,temp);<br>&nbsp; if GetLastError() &lt;&gt; ERROR_SUCCESS then<br>&nbsp; &nbsp; MessageDlg('AdjustTokenPrivileges disable failed!',mtError,[mbOK],0);<br>&nbsp; ExitWindowsEx(EWX_SHUTDOWN,0);<br>end;<br>那位能指出错在哪里吗?<br>运行时出现提示MessageDlg('AdjustTokenPrivileges enable failed',mtError,[mbOK],0);<br>说明函数AdjustTokenPrivileges没有成功,没有获得权限.<br>
 
给你一段 SetPrivilege 的源代码<br><br>function TMain.SetPrivilege(sPrivilegeName: string;<br>&nbsp; bEnabled: boolean): boolean;<br>var<br>&nbsp; TPPrev,TP : TTokenPrivileges;<br>&nbsp; Token : THandle;<br>&nbsp; dwRetLen : DWord;<br>begin<br>&nbsp; &nbsp; Result := False;<br>&nbsp; &nbsp; OpenProcessToken(GetCurrentProcess,<br>&nbsp; &nbsp; TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);<br>&nbsp; &nbsp; TP.PrivilegeCount := 1;<br>&nbsp; &nbsp; if( LookupPrivilegeValue(<br>&nbsp; &nbsp; Nil, PChar( sPrivilegeName ), TP.Privileges[ 0 ].LUID ))then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if( bEnabled )then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TP.Privileges[ 0 ].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TP.Privileges[ 0 ].Attributes := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; dwRetLen := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := AdjustTokenPrivileges(Token, False,<br>&nbsp; &nbsp; &nbsp; &nbsp; TP, SizeOf( TPPrev ), TPPrev, dwRetLen);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; CloseHandle( Token );<br>end;<br>
 
不行呀,我引用楼上的这个函数<br>SetPrivilege('SE_SECURITY_NAME',true) 返回是false不能得到权限<br>我自己写的那个在这里就已经出问题了<br>LookupPrivilegeValue(nil,PChar('SE_SECURITY_NAME'),tkp.Privileges[0].Luid);<br>得到当前局域网的SE_SECURITY_NAME权限的Luid返回为False 后面的代码自然就不能实现了
 
这样调用,老兄,你的参数有问题啊, 根本就没有你写的那个权限<br>应该是 SeSecurityPrivilege<br><br><br>给你搜索了一下相关的东西:<br>附在后面<br><br>begin<br>&nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; if( SetPrivilege( 'SeShutdownPrivilege', True ) )then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if(not ExitWindowsEx( iFlags, 0)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := False;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; SetPrivilege( 'SeShutdownPrivilege', False );<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; SendResult(Socket,false,'SetPrivilege failed!');<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := False;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if Result then SendResult(Socket,true,'ShutDown Execute!');<br>end;<br><br><br>SeCreateTokenPrivilege<br>1 wbemPrivilegeCreateToken Required to create a primary token. <br>SeAssignPrimaryTokenPrivilege<br>2 wbemPrivilegePrimaryToken Required to assign the primary token of a process. <br>SeLockMemoryPrivilege<br>3 wbemPrivilegeLockMemory Required to lock physical pages in memory. <br>SeIncreaseQuotaPrivilege<br>4 wbemPrivilegeIncreaseQuota Required to increase the quota assigned to a process. <br>SeMachineAccountPrivilege<br>5 wbemPrivilegeMachineAccount Required to create a computer account. <br>SeTcbPrivilege<br>6 wbemPrivilegeTcb Identifies its holder as part of the trusted computer base. Some trusted, protected subsystems are granted this privilege. <br>SeSecurityPrivilege<br>7 wbemPrivilegeSecurity Required to perform a number of security-related functions, such as controlling and viewing audit messages. This privilege identifies its holder as a security operator. <br>SeTakeOwnershipPrivilege<br>8 wbemPrivilegeTakeOwnership Required to take ownership of an object without being granted discretionary access. This privilege allows the owner value to be set only to those values that the holder may legitimately assign as the owner of an object. <br>SeLoadDriverPrivilege<br>9 wbemPrivilegeLoadDriver Required to load or unload a device driver. <br>SeSystemProfilePrivilege<br>10 wbemPrivilegeSystemProfile Required to gather profiling information for the entire system. <br>SeSystemtimePrivilege<br>11 wbemPrivilegeSystemtime Required to modify the system time. <br>SeProfileSingleProcessPrivilege<br>12 wbemPrivilegeProfileSingleProcess Required to gather profiling information for one process. <br>SeIncreaseBasePriorityPrivilege<br>13 wbemPrivilegeIncreaseBasePriority Required to increase the base priority of a process. <br>SeCreatePagefilePrivilege<br>14 wbemPrivilegeCreatePagefile Required to create a paging file. <br>SeCreatePermanentPrivilege<br>15 wbemPrivilegeCreatePermanent Required to create a permanent object. <br>SeBackupPrivilege<br>16 wbemPrivilegeBackup Required to perform backup operations. <br>SeRestorePrivilege<br>17 wbemPrivilegeRestore Required to perform restore operations. This privilege enables you to set any valid user or group security identifier (SID) as the owner of an object. <br>SeShutdownPrivilege<br>18 wbemPrivilegeShutdown Required to shut down a local system. <br>SeDebugPrivilege<br>19 wbemPrivilegeDebug Required to debug a process. <br>SeAuditPrivilege<br>20 wbemPrivilegeAudit Required to generate audit-log entries. <br>SeSystemEnvironmentPrivilege<br>21 wbemPrivilegeSystemEnvironment Required to modify the nonvolatile RAM of systems that use this type of memory to store configuration information. <br>SeChangeNotifyPrivilege<br>22 wbemPrivilegeChangeNotify Required to receive notifications of changes to files or directories. This privilege also causes the system to skip all traversal access checks. It is enabled by default for all users. <br>SeRemoteShutdownPrivilege<br>23 wbemPrivilegeRemoteShutdown Required to shut down a system using a network request. <br>SeUndockPrivilege<br>24 wbemPrivilegeUndock Required to remove a computer from a docking station. <br>SeSyncAgentPrivilege<br>25 wbemPrivilegeSyncAgent Required to synchronize directory service data. <br>SeEnableDelegationPrivilege<br>26 wbemPrivilegeEnableDelegation Required to enable computer and user accounts to be trusted for delegation. <br><br>
 
再问一下,我查了一下SDK帮助有关Privileges的定义<br>The following privileges are defined by Windows NT<br>SE_SECURITY_NAME <br>Required to perform a number of security-related functions,<br>such as controlling and viewing audit messages.<br>This privilege identifies its holder as a security operator.<br>是有这个权限的呀,那张权限表和你给的怎么大不一样呀?
 
看明白了是一样的只是写法不同,VC里面写SE_REMOTE_SHUTDOWN_NAME = 这里的<br>SeRemotePrivilege
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
604
import
I
后退
顶部