3

我使用这个函数来获取 IE 版本。但在某些情况下,它找不到这个键并返回 0。还有另一种不依赖注册表的方法吗?

function GetIEVersion: string;
var
   Reg: TRegistry;
   S: string;
   i : integer;
begin
   Reg := TRegistry.Create;
   with Reg do
      begin
         RootKey := HKEY_LOCAL_MACHINE;
         OpenKey('Software\Microsoft\Internet Explorer', False);
         if ValueExists('Version') then
          begin
            S := ReadString('Version');
          end
         else
         begin
            S := '0';
         CloseKey;
        end;
         Free;
      end;
    result := s;
end;
4

3 回答 3

7

TRegistry.OpenKey()使用该TRegistry.Access属性知道打开密钥时要请求什么权限。默认情况下,TRegistry.Access设置为KEY_ALL_ACCESS,仅限管理员使用。改为使用TRegistry.OpenKeyReadOnly()。此外,为了更好地衡量,您可以摆脱ValueExists()检查,因为ReadString()如果该值不存在,则返回一个空白字符串。

尝试这个:

function GetIEVersion: string; 
var 
  Reg: TRegistry; 
begin 
  Result := '';
  Reg := TRegistry.Create; 
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE; 
    if Reg.OpenKeyReadOnly('Software\Microsoft\Internet Explorer') then
    try
      Result := Reg.ReadString('Version'); 
    finally
      Reg.CloseKey; 
    end; 
  finally
    Reg.Free; 
  end;
  if Result = '' then
    Result := '0'; 
end; 
于 2012-10-04T00:39:26.520 回答
5

您可以使用获取文件版本信息的方法。首先我们会得到iexplore.exe路径。

function GetIEPath: string;
var
  Reg: TRegistry;
  Len: Integer;
begin
  Result := '';
  Reg := TRegistry.Create;

  with Reg do
  begin
    try
      RootKey := HKEY_CLASSES_ROOT;
      OpenKeyReadOnly('CLSID\{0002DF01-0000-0000-C000-000000000046}\LocalServer32');
      try
        Result := ReadString('');
      finally
        CloseKey;
        Len := Length(Result);

        if Len >= 2 then
        begin
          if(Result[Len] = '"') then
            Delete(Result, Len, 1);

          if(Result[1] = '"') then
            Delete(Result, 1, 1);
        end
        else
        begin
          Result := '';
        end;
      end;
    finally
      Free;
    end;
  end;
end;

{0002DF01-0000-0000-C000-000000000046}是 Internet Explorer 的 CLSID。的默认键值为LocalServer32路径iexplore.exe

然后,我们使用一些 API 调用,如 Simon Grossenbacher 在此链接上演示的那样,来获取文件版本信息。修改他的函数,我们得到这个:

function GetIEVersion: string;
var
  IEPath: string;
  VerInfoSize: DWORD;
  VerInfo: Pointer;
  VerValueSize: DWORD;
  VerValue: PVSFixedFileInfo;
  Dummy: DWORD;
begin
  Result := '0';

  IEPath := GetIEPath;
  if IEPath = '' then
    Exit;

  VerInfoSize := GetFileVersionInfoSize(PChar(IEPath), Dummy);
  if VerInfoSize = 0 then
    Exit;

  GetMem(VerInfo, VerInfoSize);
  GetFileVersionInfo(PChar(IEPath), 0, VerInfoSize, VerInfo);
  VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);

  with VerValue^ do
  begin
    Result := IntToStr(dwFileVersionMS shr 16);
    Result := Result + '.' + IntToStr(dwFileVersionMS and $FFFF);
    Result := Result + '.' + IntToStr(dwFileVersionLS shr 16);
    Result := Result + '.' + IntToStr(dwFileVersionLS and $FFFF);
  end;
  FreeMem(VerInfo, VerInfoSize);
end;

然后,您只需使用,例如,这个:

ShowMessage(GetIEVersion);
于 2012-10-04T01:02:05.730 回答
0
function TFuncoesSO.GetIEVersion(AOnlyMajorVersion: Boolean = False): string;
var
   lVersao: string;
   lReg: TRegistry;
begin
   Result := '';

   lReg := TRegistry.Create;

   try

      lReg.RootKey := HKEY_LOCAL_MACHINE;

      if lReg.OpenKeyReadOnly('Software\Microsoft\Internet Explorer') then
      begin
         lVersao := '';

         if lReg.ValueExists('svcVersion') then
         begin
            lVersao := lReg.ReadString('svcVersion');
         end
         else if lReg.ValueExists('Version') then
         begin
            lVersao := lReg.ReadString('Version');
         end
         else if lReg.ValueExists('IVer') then
         begin
            lVersao := lReg.ReadString('IVer');
         end;

         if (lVersao <> '') then
         begin
            if (AOnlyMajorVersion) then
            begin
               if (WordCount(lVersao, ['.']) > 0) then
               begin
                  lVersao := ExtractWord(1, lVersao, ['.']);
               end;
            end;
         end;

         Result := lVersao;

         lReg.CloseKey;
      end;

   finally
      lReg.Free;
   end;
end;
于 2019-09-11T14:03:26.470 回答