I need to create a registration free COM object in .Net Framework using C#.
I've followed the MSDN walkthrough. I've to work on it because, or it is not enough clear to me, or it is not correct, however this is an old post and I use Visual Studio 2015 on Windows 10, so maybe something is changed.
Here in the following the steps that I've made to make it work:
Compile COM C# dll
SideBySide.dll
(Target Framework 2.0), of course I've not registered it by regasm.I don't use the approach described in the tutorial, it seems not work for me. I create
SideBySide.Manifest
bymt.exe
, here's the command:mt -outputresource:"<path SidebySide.dll>" -manifest "<SideBySide.manifest>"
I've manually modified the generated manifest to remove all not useful tags, and add the mandatory ones. Here is the modified manifest:
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity name="SideBySide" version="1.0.0.0" type="win32" /> <clrClass clsid="{4B72FC46-C543-4101-80DB-7777848D1357}" progid="SideBySide.SideBySideClass" threadingModel="Both" name="SideBySide.SideBySideClass" runtimeVersion="v2.0.50727"> </clrClass> <file name="SideBySide.dll"> </file> </assembly>
I've added the manifest to
SideBySide.dll
with this command:mt -outputresource:"<Path SidebySide.dll>" -manifest "SideBySide.manifest"
I've exported the TLB from
SideBySide.dll using
tlbexp`.I've set No in the configuration
Properties -> Manifest tool -> Embed Manifest
of the C++ client.I've compiled
client.exe
, and then I've applied changes to theclient.exe.manifest
file. Here is the modified manifest:<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <assemblyIdentity type = "win32" name = "client" version = "1.0.0.0" /> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="SideBySide" version="1.0.0.0" /> </dependentAssembly> </dependency> </assembly>
Everything is working well, and it seems that I can consume the .Net Framework COM interface from the native C++ application.
However, there is an issue when I try to compile SideBySide.dll
with .Net Framework 4.0 or newer, when I call CreateInstance
:
ISideBySideClassPtr ptr;
HRESULT hr = ptr.CreateInstance(__uuidof(SideBySideClass));
This error occurs:
0x8013101b : This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
Of course, I've tried to perform all the steps listed above, I try also to specify the runtime version in the manifest of the DLL, but it was not useful.
I also read this post. The problem is the same, but the solution, in my opinion, is not suitable for me, because I need to call a C# COM object from a native client.
Is there some workaround to apply, or do I need another approach to my problem?