I'm using Visual Studio 2022 and I was running into this problem.
My installer's target platform was x64, and every project's build platform was also x64. I explicitly needed these properties to remain unchanged, I didn't want to set it to "Any CPU".
My solution was to go into the custom actions (right click the installer project, go to View > Custom Actions) and make sure that the Run64Bit property was set to True. After doing so, the installer no longer gave me that exception.