MsiOpenDatabaseW throws access violation when executed within an msi custom action

  access-violation, c++, debugging, windows-installer

I have a code that modifies a cached 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.

What might cause the access violation within when running withinthe msi Custom Action?
Can it be caused by a possible inconsistence in C/Cpp Runtime linkage (different CRT versions)

Source: Windows Questions C++

LEAVE A COMMENT