Search code examples
inno-setupcapicom

How to check if CAPICOM is installed?


My application uses CAPICOM. It may not be installed in some machines, so the installer needs to install it. I already added the msi installers to run on installing my application.

Is there any way to test in Inno Setup if CAPICOM is installed to avoid a redundant installation?


Solution

  • If you take a look at CAPICOM SDK distribution examples, specifically \Samples\vbs\CVersion.vbs, you'll see there a trial error feature test way to determine library version number. At the beginning of that script the author tries to initialize CAPICOM.Store object and opens the root certificate store (for later use):

    ' Open the machine Root store, since it is safe to assume it always has some certs in it.
    Set oStore = CreateObject("CAPICOM.Store")
    oStore.Open CAPICOM_LOCAL_MACHINE_STORE, "Root", CAPICOM_STORE_OPEN_READ_ONLY
    If Err.Number <> 0 Then
        GetCAPICOMVersion = CAPICOM_NOT_INSTALLED
        Exit Function
    End If
    

    Since there is no flat API function that would report CAPICOM installation state (or e.g. version), I'd take inspiration from the mentioned script except that I'd only try to initialize some of the CAPICOM COM objects (existing in the desired version), assuming if that fails, CAPICOM is not installed:

    [Code]
    function IsCapiComInstalled: Boolean;
    begin
      Result := True;
      try
        CreateOleObject('CAPICOM.Store'); { try to initialize CAPICOM.Store object }
      except
        Result := False; { if that fails, CAPICOM is not installed }
      end;
    end;
    

    Such helper function you can then use for the Check parameter in your selected script section item.