2012-02-26 169 views
1

我有一个小的Inno脚本来检查注册表您当前.NET安装,并返回一个布尔...检查XML的版本使用Inno Setup的安装程序

[Code] 
function IsDotNetDetected(version: string; service: cardinal): Boolean; 
// Indicates whether the specified version and service pack of the .NET Framework is installed. 
// 
// version -- Specify one of these strings for the required .NET Framework version: 
// 'v1.1.4322'  .NET Framework 1.1 
// 'v2.0.50727' .NET Framework 2.0 
// 'v3.0'   .NET Framework 3.0 
// 'v3.5'   .NET Framework 3.5 
// 'v4\Client'  .NET Framework 4.0 Client Profile 
// 'v4\Full'  .NET Framework 4.0 Full Installation 
// 
// service -- Specify any non-negative integer for the required service pack level: 
// 0    No service packs required 
// 1, 2, etc.  Service pack 1, 2, etc. required 
var 
    key: string; 
    install, serviceCount: cardinal; 
    success: boolean; 
begin 
    key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + version; 
    // .NET 3.0 uses value InstallSuccess in subkey Setup 
    if Pos('v3.0', version) = 1 then begin 
     success := RegQueryDWordValue(HKLM, key + '\Setup', 'InstallSuccess', install); 
    end else begin 
     success := RegQueryDWordValue(HKLM, key, 'Install', install); 
    end; 
    // .NET 4.0 uses value Servicing instead of SP 
    if Pos('v4', version) = 1 then begin 
     success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount); 
    end else begin 
     success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount); 
    end; 
    result := success and (install = 1) and (serviceCount >= service); 
end; 

function CheckDotNet(): Boolean; 
begin 
    if not IsDotNetDetected('v4\Full', 0) then begin 
     //MsgBox('{#gsAppName} requires Microsoft .NET Framework 4.0 Full.'#13#13 
     // 'Please use Windows Update to install this version,'#13 
     // 'and then re-run the {#gsAppName} setup program.', mbInformation, MB_OK); 
     result := false; 
    end else 
     result := true; 
end; 

我想看看是否有可能做同样的事情,但在一个XML文件。我有位于'C:\ test_folder \ test.xml'的以下XML文件。

<Registry> 
    <HKEY_LOCAL_MACHINE> 
     <SOFTWARE> 
     <KOFAX> 
      <CONDOR Value="0" Type="integer"> 
       <VERSION Value="V4.10.039"/> 

任何人都知道如何检查该版本并检查它是否高于4.0?使用.NET函数我简单地调用CheckDotNet(),它然后依次调用IsDotNetDetected(“V4 \满”,0)我想要做同样的事情与此XML文件。我想要的功能检查,如果我的版本“V4.10.039”是大于“4.0”,通过调用像IsMySoftwareDetected(“4.0”)。

+1

的[CodeAutomation.iss示例](https://开头github.com/jrsoftware/issrc/blob/master/Examples/CodeAutomation.iss)演示了使用创新科技的MSXML库。 – Deanna 2012-02-27 00:01:14

回答

1

我知道我很晚:-)只是想完成你的问题,因为在上面的评论链接无法完全涵盖你问什么InnoSetup代码示例。

脚本文件:

[Setup] 
AppName=My Program 
AppVersion=1.5 
DefaultDirName={pf}\My Program 

[Files] 
Source: "ProductVersion.xml"; Flags: dontcopy; 

[Code] 
// this function opens the XML file and returns the value of XML node 
// attribute specified by the given XPath expression 
function GetAttrValueFromXML(const AFileName, APath: string): string; 
var 
    XMLNode: Variant; 
    XMLDocument: Variant; 
begin 
    Result := ''; 
    XMLDocument := CreateOleObject('Msxml2.DOMDocument.6.0'); 
    try 
    XMLDocument.async := False; 
    XMLDocument.load(AFileName); 
    if (XMLDocument.parseError.errorCode <> 0) then 
     MsgBox('The XML file could not be parsed. ' + 
     XMLDocument.parseError.reason, mbError, MB_OK) 
    else 
    begin 
     XMLDocument.setProperty('SelectionLanguage', 'XPath'); 
     XMLNode := XMLDocument.selectSingleNode(APath); 
     Result := XMLNode.NodeValue; 
    end; 
    except 
    MsgBox('An error occured!', mbError, MB_OK); 
    end; 
end; 

// function that strips out all non-numeric values and returns what 
// remains as an integer, so you should keep the version string format 
function GetVersionValue(const Value: string): Integer; 
var 
    S: string; 
    I: Integer; 
begin 
    S := Value; 
    for I := Length(Value) downto 1 do 
    if not ((Ord(S[I]) >= Ord('0')) and (Ord(S[I]) <= Ord('9'))) then 
     Delete(S, I, 1); 
    Result := StrToInt(S); 
end; 

procedure InitializeWizard; 
var 
    S: string; 
    I: Integer; 
begin 
    // extract the XML file containing the version information temporarly 
    ExtractTemporaryFile('ProductVersion.xml'); 
    // read the attribute value specified by the XPath expression 
    S := GetAttrValueFromXML(ExpandConstant('{tmp}\ProductVersion.xml'), 
    '//Registry/HKEY_LOCAL_MACHINE/SOFTWARE/KOFAX/CONDOR/VERSION/@Value'); 
    // the numeric part of the version string must be in a format X.XX.XXX 
    // because the GetVersionValue function removes all non-numeric values 
    // from that string and returns it as an integer value, and to compare 
    // it with an integer constant they must match in the number of digits 
    // following comparision means when the version is below 4.10.039 then 
    if GetVersionValue(S) < 410039 then 
    MsgBox('Version is below 4.10.039.', mbInformation, MB_OK) 
    else 
    MsgBox('Version equals or greater than 4.10.039.', mbInformation, MB_OK); 
end; 

XML文件(ProductVersion.xml):

<?xml version="1.0" encoding="utf-8"?> 
<Registry> 
    <HKEY_LOCAL_MACHINE> 
    <SOFTWARE> 
     <KOFAX> 
     <CONDOR Value="0" Type="integer"> 
      <VERSION Value="V4.10.038"/> 
     </CONDOR> 
     </KOFAX> 
    </SOFTWARE> 
    </HKEY_LOCAL_MACHINE> 
</Registry> 
相关问题