Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to call a C++ COM dll from C# without having to register the COM object. I've read everything I can including closely following
Registration-Free Activation of COM Components: A Walkthrough
but I can't get it to work. It's fine if the C++ COM component is registered, but I get a "Library not registered. (Exception from HRESULT: 0x8002801D" if it's not.
C++ IDL (SideBySide.idl):
import "oaidl.idl";
import "ocidl.idl";

[
	object,
	uuid(40cd21b0-f634-4c41-88d6-b18fe881b86d),
	dual,
	nonextensible,
	helpstring("ISideBySideClass Interface"),
	pointer_default(unique)
]
interface ISideBySideClass : IDispatch
{
	[id(1)] HRESULT Version([out, retval] BSTR *pVer);
	[id(2)] HRESULT Output();
};
[
	uuid(604e4622-3763-47d9-b3ca-1d3190dcc7d2),
	version(1.0),
	helpstring("SideBySide 1.0 Type Library")
]
library SideBySideLib
{
	importlib("stdole2.tlb");
	[
		uuid(0cd09207-7bfa-41d3-aba6-d565c0fb00d2),
		helpstring("SideBySideClass Class")
	]
	coclass SideBySideClass
	{
		[default] interface ISideBySideClass;
	};
};

import "shobjidl.idl";


C# calling code (in Form1.cs):
Type myType = Type.GetTypeFromProgID("SideBySide.SideBySideClass.1");
dynamic obj = Activator.CreateInstance(myType);
obj.Output();  <==FAILS


C# EXE manifest (SideBySideCSharpExE.exe.manifest"):
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0"
       xmlns="urn:schemas-microsoft-com:asm.v1">
  <assemblyIdentity name="SideBySideCSharpExE.exe" version="1.0.0.0" type="win32" />
  <file name="SideBySide.dll">
    <comClass clsid="{0cd09207-7bfa-41d3-aba6-d565c0fb00d2}"
       threadingModel="Apartment"
       tlbid="{604e4622-3763-47d9-b3ca-1d3190dcc7d2}"
       progid="SideBySide.SideBySideClass.1"
       description="SideBySideClass Class" />
  </file>
  <comInterfaceExternalProxyStub name="ISideBySideClass"
      iid="{40cd21b0-f634-4c41-88d6-b18fe881b86d}"
      proxyStubClsid32="{40cd21b0-f634-4c41-88d6-b18fe881b86d}"
      baseInterface="{0cd09207-7bfa-41d3-aba6-d565c0fb00d2}"
      tlbid="{0cd09207-7bfa-41d3-aba6-d565c0fb00d2}"/>
</assembly>

Using
Visual Studio: 16.10.3
C++ Windows SDK: 10.0
C++ Platform Toolset: v142
C++ Language Standard: ISO C++ 14 Standard
Currently using Win32 since that's what the examples seem to be, but will want to use x64 in production code
C#: .NET Framework 4.7.2
Platform target: x86 (will want to move to x64)

What I have tried:

I've run sxstrace.exe, but it shows no errors, so I think the manifest file syntax is OK.
To create the manifest file I've tried both adding a reference in the C# project to the C++ project and compiling to get it and I've run regsvr42 on the C++ COM object.
I've set the C# property "Manifest:" to "Create application without a manifest" so it uses the external manifest, not an embedded one.
Posted
Updated 13-Jan-22 12:50pm
Comments
[no name] 11-Aug-21 17:40pm    
https://docs.microsoft.com/en-us/dotnet/framework/interop/importing-a-type-library-as-an-assembly
blyon 11-Aug-21 19:42pm    
That pointed me in the right direction.
Thanks
[no name] 13-Aug-21 11:09am    
You're welcome!
KarstenK 17-Aug-21 9:00am    
plz write answers as solution to close the q&a.

1 solution

Changing the C# code in Form1.cs to:
C#
var sbsl = new SideBySideLib.SideBySideClass();
sbsl.Output();

fixed the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900