3

Lazarus IDE が Delphi を使用してプログラムでシステムにインストールされているかどうかを検出する適切な方法は何ですか?

たとえば、Delphi 7 がインストールされているかどうかを検出するには、このキーを確認できますHKLM\Software\Borland\Delphi\7.0

Windows レジストリで Lazarus の同様のキーを検索しましたが、何も見つかりませんでした。

4

3 に答える 3

7

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>

したがって、Lazarus IDE が Windows システムにインストールされているかどうかを判断するために必要な手順は次のとおりです。

  1. 関数を<user name>\Local Settings\Application Data\lazarus使用して値の場所を特定します。SHGetSpecialFolderLocationCSIDL_LOCAL_APPDATA

  2. ファイルを解析して、ルートの下にあるキーenvironmentoptions.xmlを見つけます。LazarusDirectoryEnvironmentOptions

  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.
于 2011-03-21T20:53:14.527 に答える
2

Program Files と C:\Users\your_name\AppData\Local\lazarus にある場合は? また、SOのバージョンは何ですか?

LE: Lazarus はデータをレジストリに保持していないようですhttp://www.lazarus.freepascal.org/index.php?topic=9342.0

于 2011-03-21T15:31:26.207 に答える
2

Afaik Lazarus のデフォルトでは、プログラム ファイルにインストールされません。これは、過去に FPC/Lazarus が使用する GNU ツールの一部がファイル名のスペースを処理できなかったためです (特にリソース コンパイラ)。

プロファイルの設定ディレクトリは、デフォルト ディレクトリのみであることに注意してください。たとえば、バッチファイルを使用して (-pcp を使用して) 独自の設定ディレクトリを渡すことができます。これは、いくつかの「スティック」バージョンが行うことです。

さらに、複数の lazarus インストール (複数のバージョン、32 ビットおよび 64 ビット、クロスコンパイラなど) が存在する可能性がありますが、その場合、appdata ディレクトリを使用できるのは 1 つだけです。

私見の最善の解決策は、ユーザーが構成できるようにすることですが、c:\lazarus および/または appdata ディレクトリ内の XML ファイルをチェックして、設定をシードする可能性のある場所を見つけます。

于 2011-03-21T20:52:41.710 に答える