Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello fellow developers.

I have problem with reg free object creation in VBA.
I use popular method like below:

VB
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:

C#
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:
C#
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
XML
<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:
XML
<?xml version="1.0" encoding="utf-8" standalone="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:
VB
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?
Posted
Updated 27-Apr-22 11:04am
v2
Comments
Dave Kreskowiak 27-Apr-22 10:34am    
Did you really leave "ProgID" in your code and expect that to work?
Member 15613763 27-Apr-22 16:49pm    
Nope. It is just an example. In fact I have sth like oCtx.CreateObject("COMServer.Server"). Assumed it was obvious. I have edited my question to be more precise. Still looking for solution.

1 solution

Seems to me you ought to get an error in the penultimate line. According to the documentation, Microsoft.Windows.ActCtx.Manifest is Read-Only. It fails when I try it.
 
Share this answer
 
Comments
Member 15613763 27-Apr-22 17:26pm    
I really do not know why it is marked as Read-Only in documentation but presented approach works.
Line: oCtx.Manifest = sFile does not throw any error. It does only when manifest is incorrect itself.

Please see real working example from WizardSoft I have checked:
https://wizardsoft.nl/products/wstools

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