AnyCPU COM library (.tlb file) not working when called from external VB6 macro in x64

1.4k Views Asked by At

Okay my title might not be clear enough. I work with an application for which we can develop VB6 macros. This macro needs to work with a .NET dll that I also developped. When I launch the application in x86 mode and I try to run the macro, it's working fine, I can access the methods of the class and everything.

But when I do the same in x64 mode I get the error : "Automation server can't create object"

What's really strange is that I'm not using any x86 unnmanaged COM dll which would be a problem with an x64 architecture. When I do : "Dim myClass as new MyClass.App" it's working but when I call for example a myClass.Start() method, I get this error. And I tried just having a MsgBox('hello world') in my Start method, to be sure it had nothing to do with my code, still no luck. I'm linking my macro to my class with a TLB file.

I tried registering my DLL with RegAsm, but it's still not working in x64. Is there a way to generate both x86 and x64 versions of my TLB? Then I would add both references and if the x64 version throws me an error on Start method, I simply call the x86 method (not a perfect solution, but still..).

Any idea how to solve this problem?

1

There are 1 best solutions below

0
On BEST ANSWER

As Hans said, I needed to user the 64-bit version of Regasm.exe. Even though I had already tried launching RegAsm manually after my installation, it was not working. First I added that in the post-build event of my project :

"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe" "$(TargetPath)"

That did the trick for the Debug version. What's strange is that the AnyCPU version was working fine at first on my x64 application, in debug mode, it started crashing when I created my setup project (no idea why...). But this change doesn't solve my problem when installing the application, so I added this in the "Commit" of my custom action (based on this article : Regasm- 64bit or 32bit though windows installer in visual studio) :

   Public Overrides Sub Commit(savedState As System.Collections.IDictionary)
        MyBase.Commit(savedState)

        'Check if we're on a x64 OS
        If Environment.Is64BitOperatingSystem Then
            'Get the Windows dir on the current computer
            Dim winDir As String = New IO.DirectoryInfo(Environment.SystemDirectory).Parent.FullName

            'Define the path to regasm for x64 machines
            Dim regasmPath As String = IO.Path.Combine(winDir, "Microsoft.NET", "Framework64", System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion, "RegAsm.exe")

            If File.Exists(regasmPath) Then 'Si le chemin existe, on enregistre la DLL
                'Get the location of our DLL
                Dim componentPath As String = GetType(Installer).Assembly.Location

                'Executes regasm
                System.Diagnostics.Process.Start(regasmPath, "/codebase """ & componentPath & """")
            End If
        End If
    End Sub

What I changed from the original article is the path to RegAsm. Since my setup is x86, the RuntimeEnvironnment's directory was pointing to the "Microsoft.NET\Framework..." path not the "Framework64" which was defeating the purpose of my action (it was already registered as a 32 bit COM interop), so I try to get the x64 version of RegAsm and run it.