0

I've struggle several hours on that and I can't find what I'm doing wrong.

I created a new C# dll project, here is the content of the only class it contain:

using System;
using System.Runtime.InteropServices;

namespace PolygonSl {

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class Config {

        [ComVisible(true)]
        public string GetCompany() {
            return "POL";
        }
    }
}

I basically remove everything from it trying to make it work, the only reference is System.

I checked the Make assembly COM-Visible flag on the Assembly Information and my project is signed (seams required for codebase).

It compiling fine, after that, I called RegAsm.exe, giving it my dll, I added /codebase and /tlb, the command is successful.

When I go to my VBA project, I can add my new tlb file to the references, working fine. After, I can use it in my code, the autocomplete is working and I can compile with no errors.

Then, when I execute, I got this:

Run-time error '430':
Class does not support Automation or does not support expected interface

Here is my code sample in the VBA:

Private Sub Button1_Click()
    'With CreateObject("PolygonSl.Config")
    With New PolygonSl.Config
        MessBox .GetCompany, MB_OK, "Test"
    End With
End Sub

I tried late binding and my code is running fine with it but I'd like to be able to use the autocomplete.

Anyone have a suggestion on what I could try to make it work?

Edit (Adding some details on my environment)

  • I work on VS2008 for projects related to Dynamics SL (one of the Microsoft ERPs)
  • I'm on Windows Server 2008 R8 Standard, running from VMWare
  • Compiling on Framework 3.5, Release, x86, Dynamics SL client is 32 bits
  • I tried my dll on Dynamics but also on Excel to be sure that the problem was not Dynamics ;)
Alex Dupuis
  • 366
  • 3
  • 14
  • The most basic reason is that you changed the DLL but forgot to remove and add back the changed .tlb in your VBA project. Or just forgot to run Regasm again, one good reason to let VS do this automatically. – Hans Passant Apr 10 '19 at 17:21
  • @HansPassant, I have a cmd file that I created that regenerate the tbl file each time. I'm pretty sure that I'm running regasm unregister and register each time, since it never worked, I watch every step carefully. I just tried to check the Register for COM interop (after unregistered the other one) and I have the same result, only the late binding is working. – Alex Dupuis Apr 10 '19 at 17:31
  • @HansPassant, In the Process Monitor, I can see the writing of the class and the interface by the RegAsm (my actual code is the same as Freeflow). But when I look at my program, I see that it read the good class but it just don't read the interface. After that, there is a lot of stuff that is not found but nothing with clsid. – Alex Dupuis Apr 10 '19 at 18:49

1 Answers1

1

I think you need to define an interface to be able to see getcompany.

using System;
using System.Runtime.InteropServices;

namespace PolygonSl
{
    [Guid("6DC1808F-81BA-4DE0-9F7C-42EA11621B7E")]
    [System.Runtime.InteropServices.ComVisible(true)]
    [System.Runtime.InteropServices.InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IConfig
    {
        string GetCompany();
    }

    [Guid("434C844C-9FA2-4EC6-AB75-45D3013D75BE")]
    [System.Runtime.InteropServices.ComVisible(true)]
    [System.Runtime.InteropServices.ClassInterface(ClassInterfaceType.None)]
    public class Config : IConfig
    {
        public string GetCompany()
        {
            return "POL";
        }
    }
}

You can generate the interface automatically by placing the cursor in the class definition and using Edit.Refactor.ExtractInterface.

I'd have to admit that I'm at the absolute edge of my abilities here and the above is put together based on examples I've seen elsewhere.

Edit

The following test code works fine on my PC

Option Explicit
Sub polygontest()

    Dim my_polygon As SOPolygon.Config
    Set my_polygon = New SOPolygon.Config
    Debug.Print my_polygon.GetCompany

End Sub

Where SOPolygon is the project name.

freeflow
  • 4,129
  • 3
  • 10
  • 18
  • I have the exacte same result. When using the reference, I got the error but the late binding is working same as before. – Alex Dupuis Apr 10 '19 at 15:19
  • @AlexDupuis: Could it be as simple as the fact that you are using MessBox rather than MsgBox? I added VBA code for the test that correctly returns POL on my machine. Please make sure you have Option Explicit as the first line in each of your module/classes. – freeflow Apr 10 '19 at 16:00
  • Yes, I had the Option Explicit and the MessBox is something from the application that I use (Dynamics SL). To be sure, I copied both of you sources and paste it on my side, I even used Excel and I have the same result. Maybe it's something in my setup. I use VS2008, FW 3.5, compiling in Release x86. – Alex Dupuis Apr 10 '19 at 16:56
  • I'm using VS2019Community, FW 4.5. I suspect this might be the issue. – freeflow Apr 11 '19 at 09:18
  • I will try to upgrade on a snapshot of this server to be sure that I'm not braking anything ;) – Alex Dupuis Apr 11 '19 at 12:55
  • You might find this link useful. https://bettersolutions.com/csharp/excel-interop/vba-calling-csharp-com-interop.htm With VS2017 and 2019 there is no need to do the regasm seperately if all the correct boxes are ticked in the project properties dialog boxes. – freeflow Apr 11 '19 at 13:58
  • I have access to VS2017 on an other system, I created the exact same project and copied the DLL to the VM, used RegAsm and it worked perfectly. Maybe something is wrong with 2008. I will check to install it on my VM. – Alex Dupuis Apr 11 '19 at 15:13