2

I have a code that modifies a cached (not the one being installed) msi installer database which works fine when executed within a standalone exe. But when it is run from within an msi Custom Action I get an access violation that is very strange.

const auto& msiProductCode = ::GetProductCode(::msiUpgradeCode);
const auto& msiPath = ::GetCachedMSIPath(msiProductCode);

PMSIHANDLE dbHandle{};
const auto res = MsiOpenDatabaseW(msiPath.data(), MSIDBOPEN_TRANSACT, &dbHandle); // access violation here within msi castom aciton

The exception message:

Exception thrown at 0x76246E10 (rpcrt4.dll) in msiexec.exe: 0xC0000005: Access violation reading location 0x00000001.

The call stack:
enter image description here

What I can't understand is how it even ends up in the rpcrt4.dll, since a standalone executable does not have that dependency:

Dump of file .\disable_custom_action.exe

File Type: EXECUTABLE IMAGE

  Image has the following dependencies:

    msi.dll
    MSVCP140D.dll
    VCRUNTIME140D.dll
    VCRUNTIME140_1D.dll
    ucrtbased.dll
    KERNEL32.dll

The dll for the msi custom action, however, has this dependency due to the use of RPC in other Custom Action functions. In-Script execution for this Custom Action is configured to Immediate Execution.

1 What might cause the access violation within when running withinthe msi Custom Action?
2 Can MsiOpenDatabaseW somehow depend on rpcrt4.dll?
3 Can it be caused by a possible inconsistence in C/Cpp Runtime linkage (different CRT versions)?


My initial problem is described here. I though that I figured it out, before the answer revealed the part where I can't call MsiOpenDatabaseW during an active installation.

Sergey Kolesnik
  • 3,009
  • 1
  • 8
  • 28

1 Answers1

1

You can't use MsiOpenDatabase from a custom action:

Because MsiOpenDatabase initiates database access, it cannot be used with a running installation.

Bob Arnson
  • 21,377
  • 2
  • 40
  • 47
  • https://stackoverflow.com/a/70440925/9363996 this answer suggested an approcah to ammend a Custom Action (though didn't mention if I could run it during the installation). My initial problem is described in the question down the link: how can I amend a Custom Action condition of a cached msi *during* an upgrade installation? – Sergey Kolesnik Jan 11 '22 at 00:52
  • 1
    what if I had it called within a subprocess started by a Custom Action? – Sergey Kolesnik Jan 11 '22 at 01:06
  • I think it has to be clarified whether you can't use `MsiOpenDatabase` "with a running installation" at all or just can't open a database of *the msi package being installed*. Because my executable that modifies the cached msi database works fine during an active installation (windows installer running in parallel + exe modifies the db). – Sergey Kolesnik Jan 11 '22 at 10:14
  • I agree the documentation is vague but I wouldn't count on being able to open *any* database from a custom action. – Bob Arnson Jan 11 '22 at 14:59
  • Running an executable in a Custom Action that modifies the cached database *worked* in my case. It *does not* work when running a dll function that opens a database. – Sergey Kolesnik Jan 12 '22 at 09:20