Jon在下面的帖子中提供的代码似乎恰好说明了我想做的事情。
不幸的是,它是用C#编写的,这是我完全不懂的语言。我正在尝试将代码翻译成Pascal(在Windows 7上使用最新版本的Lazarus)。通过阅读代码之间的空白,我认为我可能已经理解了很多 - 但是,只有当它不能完成任务时,我才会知道是否出错。目前,在以下位置它无法编译。
if (!DuplicateTokenEx(userToken, 0x10000000, ref tokenAttributes,
SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, TOKEN_TYPE.TokenImpersonation,
out newToken)) {
log("ERROR: DuplicateTokenEx returned false - "
我的Pascal版本:
If Not DuplicateTokenEx(UserToken, MAXIMUM_ALLOWED, tokenAttributes,
SecurityImpersonation, TokenPrimary, newToken) then
Writeln(DLog, 'Failed to duplicate security token');
在六个参数中的第五个参数上,Lazarus会抛出一个错误。
dmain.pas(189,110) 错误:第5个参数类型不兼容:得到“TOKEN_TYPE”,期望“_TOKEN_TYPE”-这表明我没有理解参数的作用。(将参数5更改为TokenImpersonation会导致相同的错误。)
往下看,我变得更加迷失:
tokPrivs.Privileges = new LUID_AND_ATTRIBUTES[1];
tokPrivs.Privileges[0].Luid = seDebugNameValue;
tokPrivs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
我看到 Windows API 中有结构类型 LUID_AND_ATTRIBUTES,但似乎 Lazarus 不识别它。
简而言之,我在摸索中。我尝试过搜索“C# for Pascal programmers”,但没有找到任何有用的信息。学习 C# 不是一件轻松的事情,因此我会非常感激有关它与 Object Pascal 之间差异以及如何翻译此代码的任何提示。
编辑:根据要求未完成的代码。
function RunOurProcess(ProgramName: String): Boolean;
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
NewToken, Token, UserToken: THandle;
WPID: DWord;
ThreadAttributes, TokenAttributes: TSecurityAttributes;
TOKPrivs: TTokenPrivileges;
begin
FillChar(StartInfo, SizeOf(TStartupInfo), #0);
FillChar(ProcInfo, SizeOf(TProcessInformation), #0);
StartInfo.cb:= SizeOf(TStartupInfo);
{ Insert handle of current desktop - without this, GUI app is not visible!
To appear before logon, lpDesktop value should be 'winsta0\WinLogon' }
StartInfo.lpDesktop:= PChar('winsta0\WinLogon');
// Save the process ID of the WinLogon process
WPID:= FindInProcesses('Winlogon.exe');
// Get the handle of this
Token:= OpenProcess(TOKEN_QUERY or TOKEN_IMPERSONATE or TOKEN_DUPLICATE, False, WPID);
// Open a process token using the handle above
If OpenProcessToken(Token, TOKEN_QUERY or TOKEN_IMPERSONATE or TOKEN_DUPLICATE, UserToken) then
Writeln(DLog, 'Opened process token for WinLogon')
else
Writeln(DLog, 'Failed to open process token for WinLogon');
// Create a new token
NewToken:= 0;
tokenAttributes.nLength:= SizeOf(tokenAttributes);
threadAttributes.nLength:= SizeOf(threadAttributes);
If Not DuplicateTokenEx(UserToken, MAXIMUM_ALLOWED, tokenAttributes, SecurityImpersonation, TokenImpersonation, newToken) then
Writeln(DLog, 'Failed to duplicate security token');
// Elevate the privileges of the token
AdjustTokenPrivileges(NewToken, False, {NewState, BufferLength, PreviousState, ReturnLength});
// LogOnUser
// If successful, CreateProcessAsUser
// In progress - code below needs to go before 'CreateProcessAsUser'
StartInfo.cb:= SizeOf(TStartupInfo);
// Insert handle of current desktop - without this, GUI app is not visible!
StartInfo.lpDesktop:= PChar('winsta0\WinLogon');
end; // RunOurProcess
我注意到,如果我尝试查找“DuplicateTokenEx”的声明,我会收到以下错误:
C:\lazarus\fpc\2.6.1\source\packages\winunits-jedi\src\jwawindows.pas(366,5) 错误: 找不到包含文件“JwaLmErr.pp”
DuplicateTokenEx
?请提供一个完整的程序。 - David Heffernan