访问注册表中的所有 3 个配置单元

发布于 2024-10-08 12:10:30 字数 3851 浏览 0 评论 0原文

使用 delphi 创建 32 位应用程序 我无法访问所有配置单元,具体取决于应用程序是在 win32 还是 win64 Windows 计算机上运行。以下是默认访问的链接:http:// msdn.microsoft.com/en-us/library/aa390789(v=VS.85).aspx

我只想创建一个应用程序,而不是 32 和 32 的单独版本。 64. 我想使用 WMI 并从 32 位注册表配置单元、64 位注册表配置单元和 WOW6432Node 检索信息。有需要设置的标志,但我不知道如何从我的 delphi 应用程序中使用常规 WMI 查询调用发送标志。以下是有关 FLAG 的信息: http://msdn .microsoft.com/en-us/library/aa393067(v=VS.85).aspx

GLibWmi & DelphiWmiCodeCreator 示例:

function GetWMIObject(const objectName: String): IDispatch; //create the Wmi instance
var
  chEaten: Integer;
  BindCtx: IBindCtx;
  Moniker: IMoniker;
begin
  OleCheck(CreateBindCtx(0, bindCtx));
  OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
  OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
end;

procedure  GetWin32_StartupCommandInfo;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  objWMIService := GetWMIObject('winmgmts:\\localhost\root\CIMV2');
  colItems      := objWMIService.ExecQuery('SELECT * FROM Win32_StartupCommand','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  while oEnum.Next(1, colItem, iValue) = 0 do
  begin
    Writeln('');
  end;
end;

修订代码:

procedure  GetWin32_StartupCommandInfo(aIDispatch: IDispatch);
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  objWMIService := aIDispatch; //GetWMIObject('winmgmts:\\localhost\root\CIMV2');
  colItems      := objWMIService.ExecQuery('SELECT * FROM Win32_StartupCommand','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  while oEnum.Next(1, colItem, iValue) = 0 do
  begin
    with Form1.lst1 do begin

      items.Add(Format('Caption="%s"; Location="%s"',[colItem.Caption,colItem.Location]));// String
      {items.Add(Format('Command         %s',[colItem.Command]));// String
      items.Add(Format('Description     %s',[colItem.Description]));// String
      items.Add(Format('Location        %s',[colItem.Location]));// String
      items.Add(Format('Name            %s',[colItem.Name]));// String
      items.Add(Format('SettingID       %s',[colItem.SettingID]));// String
      items.Add(Format('User            %s',[colItem.User]));// String
      items.Add(Format('UserSID         %s',[colItem.UserSID]));// String
      }
      items.Add('');

    end;
  end;
end;

function MyConnectWMI(wmiHost:string; var Services: ISWbemServices):Boolean;
const
  STR_CIM2_ROOT ='root\CIMV2';
  STR_EMPTY = '';
var
  NVS: SWbemNamedValueSet;
  providerArchitecture : OleVariant;
  requiredArchitecture : OleVariant;
  Locator : ISWbemLocator; //CoSWbemLocator;
begin
  try
    providerArchitecture := 32; // or 64
    requiredArchitecture := true;

    NVS := CoSWbemNamedValueSet.Create( );
    NVS.Add('__ProviderArchitecture', providerArchitecture , 0);
    NVS.Add('__RequiredArchitecture', requiredArchitecture , 0);
    // Create the Location object
    Locator := CoSWbemLocator.Create();
    // Connect to the WMI service, with the root\cimv2 namespace
    Services := Locator.ConnectServer(wmiHost,
        STR_CIM2_ROOT, {user}STR_EMPTY, {password}STR_EMPTY,
        STR_EMPTY,STR_EMPTY, 0, NVS);

    Result := True;
  except
    Result := False;
  end;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  aServices: ISWbemServices;
begin
  if MyConnectWMI('localhost', aServices) then
    GetWin32_StartupCommandInfo(aServices);
end;

Creating a 32 bit application with delphi I do not have access to all hives depending whether the application is run on a win32 or win64 windows machine. Here is a link for default access: http://msdn.microsoft.com/en-us/library/aa390789(v=VS.85).aspx

I just want to create a single application, and not a separate version for 32 & 64. And I would like to use WMI and retrieve information from the 32 bit registry hive, the 64 bit registry hive and the WOW6432Node. There are FLAGS to set but I can't figure out how to send the flags with a regular WMI query call from my delphi application. Here is info about the FLAGs: http://msdn.microsoft.com/en-us/library/aa393067(v=VS.85).aspx

GLibWmi & DelphiWmiCodeCreator exmple:

function GetWMIObject(const objectName: String): IDispatch; //create the Wmi instance
var
  chEaten: Integer;
  BindCtx: IBindCtx;
  Moniker: IMoniker;
begin
  OleCheck(CreateBindCtx(0, bindCtx));
  OleCheck(MkParseDisplayName(BindCtx, StringToOleStr(objectName), chEaten, Moniker));
  OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
end;

procedure  GetWin32_StartupCommandInfo;
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  objWMIService := GetWMIObject('winmgmts:\\localhost\root\CIMV2');
  colItems      := objWMIService.ExecQuery('SELECT * FROM Win32_StartupCommand','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  while oEnum.Next(1, colItem, iValue) = 0 do
  begin
    Writeln('');
  end;
end;

REVISED CODE:

procedure  GetWin32_StartupCommandInfo(aIDispatch: IDispatch);
var
  objWMIService : OLEVariant;
  colItems      : OLEVariant;
  colItem       : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;
begin;
  objWMIService := aIDispatch; //GetWMIObject('winmgmts:\\localhost\root\CIMV2');
  colItems      := objWMIService.ExecQuery('SELECT * FROM Win32_StartupCommand','WQL',0);
  oEnum         := IUnknown(colItems._NewEnum) as IEnumVariant;
  while oEnum.Next(1, colItem, iValue) = 0 do
  begin
    with Form1.lst1 do begin

      items.Add(Format('Caption="%s"; Location="%s"',[colItem.Caption,colItem.Location]));// String
      {items.Add(Format('Command         %s',[colItem.Command]));// String
      items.Add(Format('Description     %s',[colItem.Description]));// String
      items.Add(Format('Location        %s',[colItem.Location]));// String
      items.Add(Format('Name            %s',[colItem.Name]));// String
      items.Add(Format('SettingID       %s',[colItem.SettingID]));// String
      items.Add(Format('User            %s',[colItem.User]));// String
      items.Add(Format('UserSID         %s',[colItem.UserSID]));// String
      }
      items.Add('');

    end;
  end;
end;

function MyConnectWMI(wmiHost:string; var Services: ISWbemServices):Boolean;
const
  STR_CIM2_ROOT ='root\CIMV2';
  STR_EMPTY = '';
var
  NVS: SWbemNamedValueSet;
  providerArchitecture : OleVariant;
  requiredArchitecture : OleVariant;
  Locator : ISWbemLocator; //CoSWbemLocator;
begin
  try
    providerArchitecture := 32; // or 64
    requiredArchitecture := true;

    NVS := CoSWbemNamedValueSet.Create( );
    NVS.Add('__ProviderArchitecture', providerArchitecture , 0);
    NVS.Add('__RequiredArchitecture', requiredArchitecture , 0);
    // Create the Location object
    Locator := CoSWbemLocator.Create();
    // Connect to the WMI service, with the root\cimv2 namespace
    Services := Locator.ConnectServer(wmiHost,
        STR_CIM2_ROOT, {user}STR_EMPTY, {password}STR_EMPTY,
        STR_EMPTY,STR_EMPTY, 0, NVS);

    Result := True;
  except
    Result := False;
  end;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  aServices: ISWbemServices;
begin
  if MyConnectWMI('localhost', aServices) then
    GetWin32_StartupCommandInfo(aServices);
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百思不得你姐 2024-10-15 12:10:31

您正在使用 WMI 名字对象,据我所知,您无法通过它指定所需的选项。您需要使用 SWbemLocator 对象,因为这样您就可以将 SWbemNamedValueSet 作为最终参数传递。

GLibWMI的UProcedures.pas文件中有一个ConnectWMI函数。它将 nil 传递给最终参数:

Services := Locator.ConnectServer(wmiHost,
        STR_CIM2_ROOT, {user}STR_EMPTY, {password}STR_EMPTY,
        STR_EMPTY,STR_EMPTY, 0, nil);

您需要创建一个替代方案,如下所示:

var
    NVS: SWbemNamedValueSet;
    providerArchitecture : OleVariant;
    requiredArchitecture : OleVariant;
/////

    providerArchitecture := 32; // or 64
    requiredArchitecture := true;

    NVS := CoSWbemNamedValueSet.Create();
    NVS.Add('__ProviderArchitecture', providerArchitecture , 0); 
    NVS.Add('__RequiredArchitecture', requiredArchitecture , 0);

    // Create the Location object
    Locator := CoSWbemLocator.Create();
    // Connect to the WMI service, with the root\cimv2 namespace
    Services := Locator.ConnectServer(wmiHost,
        STR_CIM2_ROOT, {user}STR_EMPTY, {password}STR_EMPTY,
        STR_EMPTY,STR_EMPTY, 0, NVS);

这将为您提供一个 ISWbemServices 接口,然后您可以在该接口上执行 ExecQuery。

通过 StdRegProv 访问注册表 -


procedure  Get_RegistryValue(aIDispatch: IDispatch);
var
  objWMIService : OLEVariant;

  strKeyPath : OLEVariant;
  strValue : OLEVariant;
  strOut : OLEVariant;

  objStdRegProv : OLEVariant;
begin;
  objWMIService := aIDispatch; 

  objStdRegProv := objWMIService.Get('StdRegProv');

  strKeyPath := 'Software\Microsoft\Wbem\CIMOM';
  strValue := 'Logging';

  objStdRegProv.GetStringValue(HKEY_LOCAL_MACHINE, strKeyPath, strValue, strOut);

    with Form1.lst1 do begin
      items.Add(strOut);
    end;
  end;
end;


// connect to root\default instead of cimv2
function MyConnectWMI(wmiHost:string; var Services: ISWbemServices):Boolean;
const
  STR_DEFAULT_ROOT = 'root\default'
  STR_EMPTY = '';
var
  NVS: SWbemNamedValueSet;
  providerArchitecture : OleVariant;
  requiredArchitecture : OleVariant;
  Locator : ISWbemLocator; //CoSWbemLocator;
begin
  try
    providerArchitecture := 32; // or 64
    requiredArchitecture := true;

    NVS := CoSWbemNamedValueSet.Create( );
    NVS.Add('__ProviderArchitecture', providerArchitecture , 0);
    NVS.Add('__RequiredArchitecture', requiredArchitecture , 0);
    // Create the Location object
    Locator := CoSWbemLocator.Create();
    // Connect to the WMI service, with the root\cimv2 namespace
    Services := Locator.ConnectServer(wmiHost,
        STR_DEFAULT_ROOT, {user}STR_EMPTY, {password}STR_EMPTY,
        STR_EMPTY,STR_EMPTY, 0, NVS);

    Result := True;
  except
    Result := False;
  end;
end;

You're using a WMI Moniker and as far as I can see you can't specify the options you need via it. You need to use the SWbemLocator object instead as that let's you pass the SWbemNamedValueSet as the final parameter.

There is a ConnectWMI function in the UProcedures.pas file of GLibWMI. It passes a nil to the final parm:

Services := Locator.ConnectServer(wmiHost,
        STR_CIM2_ROOT, {user}STR_EMPTY, {password}STR_EMPTY,
        STR_EMPTY,STR_EMPTY, 0, nil);

You need to create an alternative, something like this:

var
    NVS: SWbemNamedValueSet;
    providerArchitecture : OleVariant;
    requiredArchitecture : OleVariant;
/////

    providerArchitecture := 32; // or 64
    requiredArchitecture := true;

    NVS := CoSWbemNamedValueSet.Create();
    NVS.Add('__ProviderArchitecture', providerArchitecture , 0); 
    NVS.Add('__RequiredArchitecture', requiredArchitecture , 0);

    // Create the Location object
    Locator := CoSWbemLocator.Create();
    // Connect to the WMI service, with the root\cimv2 namespace
    Services := Locator.ConnectServer(wmiHost,
        STR_CIM2_ROOT, {user}STR_EMPTY, {password}STR_EMPTY,
        STR_EMPTY,STR_EMPTY, 0, NVS);

That will get you an ISWbemServices interface on which you can then execute ExecQuery.

Accessing the registry via StdRegProv -


procedure  Get_RegistryValue(aIDispatch: IDispatch);
var
  objWMIService : OLEVariant;

  strKeyPath : OLEVariant;
  strValue : OLEVariant;
  strOut : OLEVariant;

  objStdRegProv : OLEVariant;
begin;
  objWMIService := aIDispatch; 

  objStdRegProv := objWMIService.Get('StdRegProv');

  strKeyPath := 'Software\Microsoft\Wbem\CIMOM';
  strValue := 'Logging';

  objStdRegProv.GetStringValue(HKEY_LOCAL_MACHINE, strKeyPath, strValue, strOut);

    with Form1.lst1 do begin
      items.Add(strOut);
    end;
  end;
end;


// connect to root\default instead of cimv2
function MyConnectWMI(wmiHost:string; var Services: ISWbemServices):Boolean;
const
  STR_DEFAULT_ROOT = 'root\default'
  STR_EMPTY = '';
var
  NVS: SWbemNamedValueSet;
  providerArchitecture : OleVariant;
  requiredArchitecture : OleVariant;
  Locator : ISWbemLocator; //CoSWbemLocator;
begin
  try
    providerArchitecture := 32; // or 64
    requiredArchitecture := true;

    NVS := CoSWbemNamedValueSet.Create( );
    NVS.Add('__ProviderArchitecture', providerArchitecture , 0);
    NVS.Add('__RequiredArchitecture', requiredArchitecture , 0);
    // Create the Location object
    Locator := CoSWbemLocator.Create();
    // Connect to the WMI service, with the root\cimv2 namespace
    Services := Locator.ConnectServer(wmiHost,
        STR_DEFAULT_ROOT, {user}STR_EMPTY, {password}STR_EMPTY,
        STR_EMPTY,STR_EMPTY, 0, NVS);

    Result := True;
  except
    Result := False;
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文