gpt4 book ai didi

c# - Windows 7 欢迎屏幕(将 C# 翻译成 Pascal)

转载 作者:行者123 更新时间:2023-11-30 17:54:54 25 4
gpt4 key购买 nike

Jon 在以下线程中提供的代码似乎准确地说明了我想要做什么

Running a process at the Windows 7 Welcome Screen

不幸的是,它是用 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) 错误:arg 的类型不兼容。 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;

我可以看到 LUID_AND_ATTRIBUTES 结构类型在 Windows API 中,但它似乎无法被 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 的调用实际上在第三个参数上失败了。看声明,是LPSECURITY_ATTRIBUTES也就是^TSecurityAttributes。现在,tokenAttributesTSecurityAttributes 类型,所以您需要传递它的地址:

If Not DuplicateTokenEx(..., @tokenAttributes, ...) then

在对 AdjustTokenPrivileges 的调用中也类似。 C#代码是:

AdjustTokenPrivileges(newToken, false, ref tokPrivs, 0, IntPtr.Zero, IntPtr.Zero)

翻译成 Pascal 将是:

AdjustTokenPrivileges(NewToken, False, @TOKPrivs, 0, nil, nil)

我不知道这段代码是否能解决您的问题。我认为这超出了这个问题的范围 - 至少这是我的借口并且我会坚持下去!

关于c# - Windows 7 欢迎屏幕(将 C# 翻译成 Pascal),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15590308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com