这是我的代码:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
DefaultGroupName=My Program
UninstallDisplayIcon={app}\MyProg.exe
OutputDir=userdocs:Inno Setup Examples Output
[Code]
const
CLSID_SetupConfiguration = '{177F0C4A-1CD3-4DE7-A32C-71DBBB9FA36D}';
type
ISetupPackageReference = interface(IUnknown)
'{DA8D8A16-B2B6-4487-A2F1-594CCCCD6BF5}'
function GetId(out result: WideString): HResult;
end;
packageReference = array of ISetupPackageReference;
ISetupInstance = interface(IUnknown)
'{B41463C3-8866-43B5-BC33-2B0676F7F42E}'
function GetInstanceId(out id: WideString): HResult;
procedure Dummy2;
function GetInstallationName(out installationName: WideString): HResult;
function GetInstallationPath(out installationName: WideString): HResult;
function GetInstallationVersion(out installationVersion: WideString): HResult;
function GetDisplayName(lcid: Integer; out displayName: WideString): HResult;
function GetDescription(lcid: Integer; out description: WideString): HResult;
function ResolvePath(pwszRelativePath: WideString; out result: WideString): HResult;
end;
ISetupInstance2 = interface(IUnknown)
'{89143C9A-05AF-49B0-B717-72E218A2185C}'
function GetInstanceId(out id: WideString): HResult;
procedure Dummy2;
function GetInstallationName(out installationName: WideString): HResult;
function GetInstallationPath(out installationName: WideString): HResult;
function GetInstallationVersion(out installationVersion: WideString): HResult;
function GetDisplayName(lcid: Integer; out displayName: WideString): HResult;
function GetDescription(lcid: Integer; out description: WideString): HResult;
function ResolvePath(pwszRelativePath: WideString; out result: WideString): HResult;
procedure Dummy3;
function GetPackages(out pack: packageReference): HResult;
end;
arrISetupInstance2 = array[1..1] of ISetupInstance2;
IEnumSetupInstances = interface(IUnknown)
'{6380BCFF-41D3-4B2E-8B2E-BF8A6810C848}'
function Next(celt: Integer; var rgelt: arrISetupInstance2; out pceltFetched: Integer): HResult;
function Skip(celt: Integer): HResult;
function Reset: HResult;
function Clone(out pEnumSetupInstances: IEnumSetupInstances): HResult;
end;
ISetupConfiguration = interface(IUnknown)
'{42843719-DB4C-46C2-8E7C-64F1816EFD5B}'
function EnumInstances(out pEnumSetupInstances: IEnumSetupInstances): HResult;
end;
ISetupConfiguration2 = interface(IUnknown)
'{26AAB78C-4A60-49D6-AF3B-3C35BC93365D}'
function EnumInstances(out pEnumSetupInstances: IEnumSetupInstances): HResult;
function EnumAllInstances(out pEnumSetupInstances: IEnumSetupInstances): HResult;
end;
procedure TestProcedure;
var
obj: IUnknown;
setupConfiguration: ISetupConfiguration2;
setupInstances: IEnumSetupInstances;
instances: arrISetupInstance2;
instance: ISetupInstance2;
fetched: Integer;
instanceVersion: WideString;
MyPackages: packageReference;
lengthOfArray : longint;
OneInstance: ISetupPackageReference;
MyString: WideString;
begin
obj := CreateComObject(StringToGUID(CLSID_SetupConfiguration));
setupConfiguration := ISetupConfiguration2(obj);
setupConfiguration.EnumInstances(setupInstances);
fetched := 0;
setupInstances.Next(1, instances, fetched);
instance := instances[1];
instance.GetInstallationVersion(instanceVersion);
instance.GetPackages(MyPackages);
lengthOfArray := Length(MyPackages); // Why do I get negative value(-177484340)?
OneInstance := MyPackages[5]; // If try to get access to first five elements of packageReference
// you get either null pointer exception or access violation.
OneInstance.GetId(MyString);
end;
function InitializeSetup(): Boolean;
begin
TestProcedure;
end;
调用ISetupInstance2中定义的GetPackages()方法后,我得到了packageReference数组。但是,要枚举它,我需要知道它的长度。这就是问题所在:“ GetArrayLength”和“ Length”之类的功能对我不起作用-它们神秘地返回负值。如果有人可以给我解决这个问题的线索,我将不胜感激。
此外,我注意到了奇怪的行为-如果尝试访问packageReference的前五个元素,则会收到空指针异常或访问冲突。如果我使用高于5的索引,一切都会正常。