I have been working through Enumerating Windows Portable Devices in C#, which among other things says to edit a Runtime Callable Wrapper (RCW) generated for the PortableDeviceApi COM object, since the default COM type library import process gets the marshalling wrong for the GetDevices
method. I have accordingly followed How to: Edit Interop Assemblies to edit the RCW, and even managed to set up a VS project to check it into source and generate the edited RCW during build by following the advice given here - Are there any examples of compiling CIL code from within a Visual Studio project.
However, whether I build the custom RCW manually or from a VS project, I am having trouble getting VS to reference the RCW. Here is what "works": if I follow the normal procedure to add a COM reference (right-click project > Add > COM Reference...), and select PortableDeviceApi, a COMReference
element gets added to the .csproj:
<COMReference Include="PortableDeviceApiLib">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>1f001332-1a57-4934-be31-affc99f4ee0a</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
And the auto-generated Interop.PortableDeviceApiLib.dll RCW is added under the project's obj folder. I can manually replace this DLL with the one I generate from the edited MSIL mentioned above, and this seems to work well enough - I get intellisense for the COM object, with the edited marshalling changes. The disadvatage with this approach is that I have to copy the edited DLL over the one generated from adding the COM reference, which seems flaky (and not very friendly to an automated build pipeline).
Another thing I have tried is just directly referencing the output DLL from the MSIL project. This does indeed build successfully, but for whatever reason neither intellisense nor the Object Browser pick up on the metadata, so it's not a great developer experience.
Finally, if I try changing the custom RCW's version information to v1.1 (the stock RCW is 1.0) and update the above COMReference.VersionMinor
element accordingly, MSBuild complains that
Warning MSB3284 Cannot get the file path for type library "1f001332-1a57-4934-be31-affc99f4ee0a" version 1.1. Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))
The underlying COM component itself is registered. So far, however, I have not been able to find any information on how to register the RCW.
I suppose next I might try going with a hybrid, using the MSIL project to generate the custom DLL and using the standard Add COM Reference process to give me intellisense, and putting a custom build step in somewhere to copy the MSIL project's custom RCW where the COMReference
element will be expecting it. But before I commit to that solution -
Is there anyone who has had any success getting Visual Studio to cleanly recognize a reference to a custom Runtime Callable Wrapper DLL? Ideally I would like to have something like the updated RCW is available out of the Add COM Reference dialog, but that is not an absolute requirement.