gpt4 book ai didi

delphi - 从 Delphi 访问 BcdStore

转载 作者:行者123 更新时间:2023-12-03 15:43:52 25 4
gpt4 key购买 nike

我正在尝试将此代码片段转换为 Delphi,但我陷入了for every objWBL in colObjects

if not objBcdStore.EnumerateObjects( &h10200003, colObjects ) then
WScript.Echo "ERROR objBcdStore.EnumerateObjects( &h10200003 ) failed."
WScript.Quit(1)
end if

for each objWBL in colObjects
WScript.Echo ""
WScript.Echo "Windows Boot Loader"
WScript.Echo "-------------------"

WScript.Echo "identifier " & GetBcdId( objWBL.Id )

If objWBL.Id = current then

if not objWBL.GetElement(BcdOSLoaderInteger_NumberOfProcessors, objElement ) then
WScript.Echo "ERROR WBL GetElement for " & Hex(BcdOSLoaderInteger_NumberOfProcessors) & " failed."
WScript.Quit(1)
end if
WScript.Echo "numproc " & objElement.Integer

if not objWBL.GetElement(BcdOSLoaderBoolean_UseBootProcessorOnly, objElement ) then
WScript.Echo "ERROR WBL GetElement for " & Hex(BcdOSLoaderBoolean_UseBootProcessorOnly) & " failed."
WScript.Quit(1)
end if
WScript.Echo "onecpu " & objElement.Boolean

end if
next

我的部分问答翻译(注意,必须由管理员运行):

uses
OleAuto,
ActiveX;

function GetObject(const objectName: String): IDispatch;
var
bindCtx: IBindCtx;
moniker: IMoniker;
chEaten: Integer;
begin
OleCheck(CreateBindCtx(0, bindCtx));
OleCheck(MkParseDisplayName(bindCtx, StringToOleStr(objectName), chEaten, moniker));
OleCheck(moniker.BindToObject(bindCtx, nil, IDispatch, Result));
end;

procedure TForm44.btnClick(Sender: TObject);
var
colObjects : OleVariant;
objBcdStore : OleVariant;
objWMIService: OleVariant;
begin
objWMIService := GetObject('winmgmts:{impersonationlevel=Impersonate,(Backup,Restore)}!root/wmi:BcdStore');
if not objWMIService.OpenStore('', objBcdStore) then
Caption := 'error'
else begin
objBcdStore.EnumerateObjects($10200003, colObjects);
//???
end;
end;

EnumerateObjects定义为

boolean EnumerateObjects(
[in] uint32 Type,
[out] BcdObject Objects[]
);

我不知道如何在 Delphi 中遍历 BcdObject 数组。

最佳答案

您必须使用VarArrayLowBoundVarArrayHighBound函数来获取 EnumerateObjects 函数返回的变体数组的边界,然后您可以使用 for 循环来迭代数组的元素。

检查此示例

Uses
ComObj,
ActiveX;

function GetObject(const objectName: String): IDispatch;
var
bindCtx: IBindCtx;
moniker: IMoniker;
chEaten: Integer;
begin
OleCheck(CreateBindCtx(0, bindCtx));
OleCheck(MkParseDisplayName(bindCtx, StringToOleStr(objectName), chEaten, moniker));
OleCheck(moniker.BindToObject(bindCtx, nil, IDispatch, Result));
end;


procedure TForm44.Button1Click(Sender: TObject);
var
colObjects : OleVariant;
objBcdStore : OleVariant;
objWMIService: OleVariant;

i : Integer;
objWBL : OleVariant;
begin
objWMIService := GetObject('winmgmts:{impersonationlevel=Impersonate,(Backup,Restore)}!root/wmi:BcdStore');
if not objWMIService.OpenStore('', objBcdStore) then
Caption := 'error'
else
begin
objBcdStore.EnumerateObjects($10200003, colObjects);
if not VarIsNull(colObjects) and VarIsArray(colObjects) then
for i := VarArrayLowBound(colObjects, 1) to VarArrayHighBound(colObjects, 1) do
begin
objWBL:=colObjects[i];
//do your stuff here


end;
end;
end;

关于delphi - 从 Delphi 访问 BcdStore,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7517965/

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