Hello fellow developers.
I have problem with reg free object creation in VBA.
I use popular method like below:
Dim oCtx As Object
Dim sFile As String
Dim retVal As Object
sFile = "C:\DllLibrary.X.manifest"
Set oCtx = CreateObject("Microsoft.Windows.ActCtx")
oCtx.Manifest = sFile
Set retVal = oCtx.CreateObject("DllLibrary.Server")
Last line is giving me error:
Run-time error '-2147467262 (80004002)':
Specified cast is not valid.
What I have tried:
I have created COM Assembly following every step from article:
Exposing .NET Core components to COM | Microsoft Docs
I have even used example project from linked github:
samples/core/extensions/COMServerDemo
In both case I see error mentioned above.
It seems that activation context is being created successfully. So generated manifest is being accepted, so why .CreateObject fails?
Marshalling (
Type marshalling - .NET | Microsoft Docs)
of returned types doesn't help either.
To sum up. Visual Studio project contains three elements:
IServer interface:
using System.Runtime.InteropServices;
namespace DllLibrary {
[Guid("DD53E788-46CF-447D-B3FE-74621E39D28F")]
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIInspectable)]
public interface IServer
{
public string greetings();
}
}
Class Server:
using System.Runtime.InteropServices;
namespace DllLibrary
{
[Guid("F9DA10EE-5B2C-40CD-9E85-59C024AD9718")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class Server : IServer
{
public string greetings()
{
return "hello from library";
}
}
}
DllLibrary.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<EnableComHosting>true</EnableComHosting>
<EnableRegFreeCom>true</EnableRegFreeCom>
<Platforms>AnyCPU;x64;x86</Platforms>
</PropertyGroup>
</Project>
Compilation gives me following result:
DllLibrary.comhost.dll
DllLibrary.deps.json
DllLibrary.dll
DllLibrary.pdb
DllLibrary.runtimeconfig.dev.json
DllLibrary.runtimeconfig.json
DllLibrary.X.manifest
Generated manifest looks like:
="1.0"="utf-8"="yes"
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity type="win32" name="DllLibrary.X" version="1.0.0.0" />
<file name="DllLibrary.comhost.dll">
<comClass clsid="{f9da10ee-5b2c-40cd-9e85-59c024ad9718}" threadingModel="Both" progid="DllLibrary.Server" />
</file>
</assembly>
What is important normal registration with regsvr32:
regsvr32 DllLibrary.comhost.dll
works fine and I can create object in VBA:
Dim lib As Object
Set lib = CreateObject("DllLibrary.Server")
Debug.Print lib.greetings()
Set lib = Nothing
And:
hello from library
is being printed in immediate window:
So what can be wrong with "manifest" approach'?
Any ideas?