检测安装的 Lazarus IDE

3

如何使用Delphi编程以编程方式检测系统中是否安装了Lazarus IDE的正确方法?

例如,要检测是否安装了Delphi 7,可以检查此键HKLM\Software\Borland\Delphi\7.0

我在Windows注册表中搜索类似于Lazarus的键,但没有找到任何内容。

3个回答

8
默认情况下,Lazarus将一个名为environmentoptions.xml的文件存储在<user name>\Local Settings\Application Data\lazarus文件夹中(在某些情况下,此文件可能位于其他文件夹中)。该文件包含了获取Lazarus IDE位置以及IDE使用的FPC(Free Pascal编译器)所需的所有信息。 environmentoptions.xml文件的内容如下:
<?xml version="1.0"?>
<CONFIG>
  <EnvironmentOptions>
    <Version Value="106"/>
    <LazarusDirectory Value="C:\lazarus\">
      <History Count="1">
        <Item1 Value="C:\lazarus\"/>
      </History>
    </LazarusDirectory>
    <CompilerFilename Value="C:\lazarus\fpc\2.2.4\bin\i386-win32\fpc.exe">
      <History Count="3">
        <Item1 Value="C:\fpc\2.2.4\bin\i386-win32\fpc.exe"/>
        <Item2 Value="C:\lazarus\fpc\2.2.4\bin\i386-win32\fpc.exe"/>
        <Item3 Value="C:\lazarus\fpc\2.4.2\bin\i386-win32\fpc.exe"/>
      </History>
    </CompilerFilename>
    <FPCSourceDirectory Value="c:\lazarus\fpc\2.2.4\source\">
      <History Count="1">
        <Item1 Value="c:\lazarus\fpc\2.2.4\source\"/>
      </History>
    </FPCSourceDirectory>
    <MakeFilename Value="C:\lazarus\fpc\2.2.4\bin\i386-win32\make.exe">
      <History Count="2">
        <Item1 Value="C:\fpc\2.2.4\bin\i386-win32\make.exe"/>
        <Item2 Value="C:\lazarus\fpc\2.2.4\bin\i386-win32\make.exe"/>
      </History>
    </MakeFilename>
    <TestBuildDirectory Value="C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\">
      <History Count="3">
        <Item1 Value="C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\"/>
        <Item2 Value="C:\temp\"/>
        <Item3 Value="C:\windows\temp\"/>
      </History>
    </TestBuildDirectory>
    <BackupProjectFiles AdditionalExtension="bak" MaxCounter="9"/>
    <BackupOtherFiles AdditionalExtension="bak" MaxCounter="9"/>
    <Debugger Class="TGDBMIDebugger" EventLogLineLimit="100"/>
    <DebuggerFilename Value="c:\lazarus\mingw\bin\gdb.exe">
      <History Count="4">
        <Item1 Value="c:\lazarus\mingw\bin\gdb.exe"/>
        <Item2 Value="/usr/bin/gdb"/>
        <Item3 Value="/usr/local/bin/gdb"/>
        <Item4 Value="/opt/fpc/gdb"/>
      </History>
    </DebuggerFilename>
    <Recent>
      <OpenFiles Max="10" Count="10">
      </OpenFiles>
      <ProjectFiles Max="5" Count="5">
      </ProjectFiles>
      <PackageFiles Max="10" Count="1">
        <Item1 Value="C:\Librerias\Indy10\Lib\indylaz.lpk"/>
      </PackageFiles>
    </Recent>
    <ExternalTools Count="0"/>
    <CharcaseFileAction Value="Ask"/>
    <CompilerMessagesFilename Value=""/>
  </EnvironmentOptions>
  <ObjectInspectorOptions ShowHints="False" InfoBoxHeight="50">
    <Version Value="3"/>
    <ComponentTree>
      <Height Value="97"/>
    </ComponentTree>
  </ObjectInspectorOptions>
</CONFIG>

确定Windows系统中Lazarus IDE是否已安装的步骤如下:
  1. 使用SHGetSpecialFolderLocation函数和CSIDL_LOCAL_APPDATA值确定<用户名>\Local Settings\Application Data\lazarus的位置。

  2. 解析文件environmentoptions.xml,找到EnvironmentOptions根目录下的LazarusDirectory键。

  3. 现在,有了Lazarus IDE的位置,您可以检查该文件夹中lazarus.exe文件是否存在。

请查看此示例应用程序,它总结了本答案中的所有步骤。

{$APPTYPE CONSOLE}

uses
  ShlObj,
  ComObj,
  ActiveX,
  Classes,
  Windows,
  Variants,
  SysUtils;

function GetLocalAppDataFolder : string;
const
  CSIDL_LOCAL_APPDATA        = $001C;
var
  ppMalloc   : IMalloc;
  ppidl      : PItemIdList;
begin
  ppidl := nil;
  try
    if SHGetMalloc(ppMalloc) = S_OK then
    begin
      SHGetSpecialFolderLocation(0, CSIDL_LOCAL_APPDATA, ppidl);
      SetLength(Result, MAX_PATH);
      if not SHGetPathFromIDList(ppidl, PChar(Result)) then
        RaiseLastOSError;
      SetLength(Result, lStrLen(PChar(Result)));
    end;
  finally
   if ppidl <> nil then
         ppMalloc.free(ppidl);
  end;
end;


function GetLazarusLocalFolder : string;
begin
 Result:=Format('%slazarus',[IncludeTrailingPathDelimiter(GetLocalAppDataFolder)]);
 if not DirectoryExists(Result) then
 Result:='';
end;


function FileToString(const FileName: TFileName): AnsiString;
var
   Stream : TFileStream;
begin
  Stream:=TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  try
      try
        SetLength(Result, Stream.Size);
        Stream.Read(Pointer(Result)^, Stream.Size);
      except
        Result:='';
      end;
  finally
     Stream.Free;
  end;
end;

function GetLazarusFolder : string;
var
   LocalFolder : TFileName;
   FileName    : TFileName;
   XmlDoc      : OleVariant;
   Node        : OleVariant;
begin
  Result:='';
  LocalFolder:=GetLazarusLocalFolder;
  if LocalFolder<>'' then
  begin
   FileName:=IncludeTrailingPathDelimiter(LocalFolder)+'environmentoptions.xml';
   if FileExists(FileName) then
   begin
     XmlDoc       := CreateOleObject('Msxml2.DOMDocument.6.0');
     try
       XmlDoc.Async := False;
       XmlDoc.LoadXML(FileToString(FileName));
       XmlDoc.SetProperty('SelectionLanguage','XPath');

        if (XmlDoc.parseError.errorCode <> 0) then
         raise Exception.CreateFmt('Error in Xml Data %s',[XmlDoc.parseError]);

       Node  :=XmlDoc.selectSingleNode('//CONFIG/EnvironmentOptions/LazarusDirectory/@Value');
       if not VarIsClear(Node) then
       Result:=Node.text;
     finally
       XmlDoc:=Unassigned;
     end;
   end;
  end;
end;


function IsLazarusInstalled : Boolean;
begin
  Result:=FileExists(IncludeTrailingPathDelimiter(GetLazarusFolder)+'lazarus.exe');
end;

begin
 try
    CoInitialize(nil);
    try
      Writeln('Lazarus config Folder  '+GetLazarusLocalFolder);
      Writeln('Lazarus Install folder '+GetLazarusFolder);
      Writeln('Is Lazarus Installed   '+BoolToStr(IsLazarusInstalled,True));
      Readln;
    finally
      CoUninitialize;
    end;
 except
    on E:Exception do
    begin
        Writeln(E.Classname, ':', E.Message);
        Readln;
    end;
  end;
end.

2
如果它驻留在“Program Files”文件夹中,以及您的C:\ Users \ your_name \ AppData \ Local \ lazarus文件夹中?另外,您使用的SO版本是什么?
更新:看起来Lazarus不会在注册表中存储其数据。http://www.lazarus.freepascal.org/index.php?topic=9342.0

4
非常有道理,因为Lazarus也可以在没有注册表的其他平台上运行。 :) - Ken White

2
据我所知,Lazarus默认不会安装到Program files目录下。这是因为在过去,FPC/Lazarus使用的一些GNU工具无法处理文件名中的空格(尤其是资源编译器)。
请注意,配置文件夹位于用户配置文件夹中只是默认设置。可以通过传递自己的设置目录(使用-pcp),例如批处理文件,在多个“粘性”版本中进行操作。
此外,可能会有多个Lazarus安装(多个版本、32位和64位、交叉编译器等),但只能使用一个appdata目录。
我认为最好的解决方案是使其可由用户配置,并检查c:\lazarus和/或appdata目录中的XML文件以找到可能的位置来填充设置。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接