How to make a VB.NET DLL callable in VB6? - vb.net

I have made a DLL in VS2017 using VB.NET:
Imports System.Runtime.InteropServices
<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)>
Public Class ComClass1
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "c67bcd70-54d0-4498-97be-a5f954790dec"
Public Const InterfaceId As String = "7ef1a8ce-bcc1-464e-8dc3-fc164bdb7ea3"
Public Const EventsId As String = "9939eabd-1102-4e34-9735-54664e3536bd"
#End Region
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
End Sub
Public Xposition As Int32
Public GoX As Int32
Public Function MoveX() As Int32
Dim target As Int32
target = Convert.ToInt32(Rnd() * 1000)
Xposition = target
Return Xposition
End Function
Public Function ReadX() As Int32
Dim target As Int32
target = Convert.ToInt32(Rnd() * 1000 - GoX)
Xposition = target
Return Xposition
End Function
End Class
I compiled the DLL as Administrator on the development Widows 10 PC.
I copied the DLL file to the Windows XP target machine running VB6 and get Run-time error 453.
The VB6 code:
Private Declare Function GSCloseServer Lib "GSWDLL32.DLL" () As Long
Private Declare Function MoveX Lib "C:\Temp\VB_Applications\My_DLL_Test\MyNewDLL.dll" () As Integer
Private Sub btnMove_Click()
Call test
End Sub
Private Sub btnStop_Click()
Dim Running As Integer
' Unload MyGraph
Set Form1 = Nothing
Running = GSCloseServer()
If Running = 0 Then
MsgBox "Close Server OK: " & Running, vbOKOnly
Else
MsgBox "Close Server Error " & Running, vbOKOnly
End If
Unload Me
End Sub
Private Sub test()
'txtbxReadBack.Text = ComClass1.Xposition
txtbxReadBack.Text = MoveX
End Sub
I am running VS2017 as administrator, so the DLL appears to be registered properly on the development PC.

These are the steps. I'll create a very simple "COMVisibleHelloWorld" class library with one class
Imports System.Runtime.InteropServices
'Create a manual interface so we can use a fixed and known guid for the COM interface.
<Guid("3BBA8F8E-6B35-48E0-91D4-124DC6FCE7B9"), InterfaceType(ComInterfaceType.InterfaceIsDual)>
Public Interface IHelloWorld
<DispId(1)> Function SayHello() As String
End Interface
<Guid("BCDCDA5C-DCAF-4C6C-86BA-03908DBEFF4B"), ClassInterface(ClassInterfaceType.None)>
Public Class HelloWorld
Implements IHelloWorld
Public Function SayHello() As String Implements IHelloWorld.SayHello
Return "Hello World"
End Function
End Class
In Visual Studio select "Make Assembly COM-Visible"
In Visual Studio create a strong key
Build the project. This will then create a tlb file.
You can now reference this is VB6
To use in VB6 just create an instance, and call the methods that have a COM interface
Private Sub Command1_Click()
Dim hello As New COMVisibleHelloWorld.HelloWorld
MsgBox (hello.SayHello)
End Sub
Result:

Related

Why can't connect to Postgresql in Win 10 64bit when build release?

The code of TDBACore.vb:
Imports System.ComponentModel
Imports System.Data
<EditorBrowsable(EditorBrowsableState.Never)> _
Public MustInherit Class TDBACore
Private Shared FRefCount As Integer
Friend Shared FIsBeginTran As Boolean = False
Friend Shared FConnection As IDbConnection
Friend Shared FTransaction As IDbTransaction
Private disposedValue As Boolean
Friend Shared iRecheckTimeout As Integer
Friend Shared iConnectionTimeOut As Integer
Friend MustOverride Function CreateConnection() As IDbConnection
Public Sub New()
Me.disposedValue = False
If TDBACore.FRefCount = 0 Then
TDBACore.FConnection = Me.CreateConnection()
End If
TDBACore.FRefCount += 1
End Sub
Friend Shared Sub OpenConnection(ByVal ConnectionString As String)
If TDBACore.FConnection.State = ConnectionState.Closed Then
TDBACore.FConnection.ConnectionString = ConnectionString
TDBACore.FConnection.Open()
End If
End Sub
End Class
The code of TDBOpener.vb:
Public NotInheritable Class TDBOpener
Inherits TDBACore
Public Shared ReadOnly _TDBOpener As New TDBOpener()
Friend Overrides Function CreateConnection() As IDbConnection
Return New NpgsqlConnection()
End Function
Public Shared Sub SetTimeoutValue(ByVal param_iRecheckTimeout As Integer, ByVal param_iConnectionTimeOut As Integer)
TDBACore.iConnectionTimeOut = param_iConnectionTimeOut
TDBACore.iRecheckTimeout = param_iRecheckTimeout
End Sub
Public Shared Sub Open(ByVal ConnectionString As String)
TDBACore.OpenConnection(ConnectionString)
End Sub
End Class
I test connect to postgresql by Npgsql.dll 3.2.2.0:
TDBOpener.Open("Server=192.168.1.10;Port=5434;UserId=postgres;Password=123456;Database=testdb;CommandTimeout=300;")
I using Win 10, 64bit, vs 2017 .Net Framework 4.6
If i run by mode [Debug]: it can run function New() of TDBACore and connect ok.
But if i run by mode [Release], it not go to function New() of TDBACore.
Why? thanks all.(Notes: I try on win 7, it is ok.)
Win 10, 64bit, vs 2017 .Net Framework 4.6:
If i run by mode [Release], it can't init Public Shared ReadOnly _TDBOpener As New TDBOpener()
My solution edit function Open of TDBOpener:
Public Shared Sub Open(ByVal ConnectionString As String)
If TDBACore.FConnection Is Nothing Then
Dim obOpen = _TDBOpener
End If
TDBACore.OpenConnection(ConnectionString)
End Sub
The reason you were having the problem, and the reason that the fix works, is because the compiler was optimizing out _TDBOpener since it wasn't being referenced. This question has more details.

ActiveX component can't create object when using .net class in vba

I am trying to use a custom class exported as a .tlb in vba. I have done the regasm stuff but I keep getting this error when I try to call a subroutine within the class:
Run-time error '429': ActiveX component can't create object
I've referenced the class in vba, I've built the class for 32bit and 64bit CPUs and nothing worked. Anyways, vba code:
Sub test()
Dim test As New Mail.Class1
test.test
End Sub
And the vb.net code:
Imports System.Runtime.InteropServices
Public Class Class1
Public Sub test()
MsgBox("hello")
End Sub
End Class
That class won't be exposed to COM. Simplest way to do this is to Add New Item and select COM Class. This generates a Class skeleton that looks like this:
<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
Public Class ComClass1
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "e19c541f-8eda-4fdd-b030-abed31518344"
Public Const InterfaceId As String = "e2122f92-5752-4135-a416-4d499d022295"
Public Const EventsId As String = "6b03de7e-90d7-4227-90ec-9121c4ce1288"
#End Region
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
MyBase.New()
End Sub
End Class
Also remember to check the "Make assembly COM Visible" in the Assembly Information dialog (Project properties>Application tab>Assembly Information)
Now when you compile this and call RegAsm, it should have an entry point for this class

Error. .. Main not accessible by program

I wrote a module and into which a Public Sub Main method. But, when I run the program. It gives " No accessible 'Main' method with an appropriate signature was found in 'abc'."
Could you please suggest possible solutions to the error.
Public Sub Main(ByVal cmdArgs As String)
Dim returnValue As Integer
If cmdArgs.Length > 0 Then
returnValue = Import_Start(cmdArgs, "f9880")
Console.WriteLine("Import end with an error " & returnValue)
Else
Console.WriteLine("parameter failure")
End If
End Sub
End Module
If you want to start your app from a Sub Main, the correct signature is:
Public Sub Main(args As String())
' or
Public Sub Main()
The command line args will be passed as a string array. Yours just declares it as String resulting in the compiler error. You also need to set the StartUp object to Sub Main in Project Properties, but that already seems to have been done.
If you do not want/need to use a module you can add it to a form (it is not clear if this is even a WinForms app) using:
Public Shared Sub Main(args As String())
' or
Public Shared Sub Main()

VB6 and VB.NET interoperability using com.visible

Please see the code below:
Imports System.Runtime.InteropServices
Public Class TestClass
<ComVisible(True)> _
Public Function Hello() As String
Return "Hello Ian"
End Function
Public Function Goodbye() As String
Return "Goodbye Ian"
End Function
End Class
I have created the Type Library using Regasm and I have added a reference to the TLB in a VB6 project. The code for VB6 is below:
Private Sub Form_Load()
Dim tc As TestClass
Set tc = New TestLibrary.TestClass
MsgBox (tc.Hello)
MsgBox (tc.GoodBye)
End Sub
I do not understand why the message prints the value of: tc.Goodbye as this is not visible.
I believe it is because the default value is true. Is there a way to set the default value to false.

unloading a DLL until it's needed

I'm having a hard time wrapping my head around some of the answers I've been reading here about unloading a plugin DLL using AppDomains. Here's my architecture:
In my solution, I have a SharedObjects project containing a ModuleBase class that all plugins (separate projects within the solution) inherit. In the SharedObjects project I also have an interface that all plugins implement (so if I have six plugins, they all implement the same interface and therefore the main program using these plugins doesn't need to know or even care what the name of the plugin's class was when it was compiled; they all implement the same interface and therefore expose the same information). Each plugin project has a project reference to the SharedObjects project. (As a side note, may be important, may not be - that SharedObjects project has a project reference to another solution, CompanyObjects containing a number of commonly-used classes, types, objects, etc.) When it's all said and done, when any given plugin compiles, the output directory contains the following DLLs:
The compiled DLL of the plugin itself
The DLL from the SharedObjects project
The DLL from the CompanyObjects project
Four prerequisite 3rd-party DLLs referenced in the CompanyObjects project
My main program creates a reference to the class where I'm doing all my plugin-related work (that class, PluginHelpers, is stored in the SharedObjects project). The program supplies an OpenFileDialog so that the user can choose a DLL file. Now, as it's running right now, I can move just the plugin DLLs to a separate folder and load them using the Assembly.LoadFrom(PathToDLL) statement. They load without error; I check to make sure they're implementing the interface in the SharedObjects project, gather some basic information, and initialize some background work in the plugin DLL itself so that the interface has something to expose. Problem is, I can't upgrade those DLLs without quitting the main program first because as soon as I use LoadFrom those DLLs are locked.
From this MSDN site I found a solution to the problem of locked DLLs. But I'm getting the same "File or dependency not found" error as the OP using the code that worked for the OP. I even get the error when I open the DLL from the release folder which includes the rest of those DLLs.
The FusionLog is even more confusing: there's no mention of the path I was trying to open; it's trying to look in the directory where I'm debugging the main program from, which is a completely different project on a completely different path than the plugins, and the file it's looking for is the name of the DLL but in the folder where the program is running. At this point I have no idea why it's disregarding the path I gave it and looking for the DLL in a completely different folder.
For reference, here's my Loader class and the code I'm using to (try to) load the DLLs:
Private Class Loader
Inherits MarshalByRefObject
Private _assembly As [Assembly]
Public ReadOnly Property TheAssembly As [Assembly]
Get
Return _assembly
End Get
End Property
Public Overrides Function InitializeLifetimeService() As Object
Return Nothing
End Function
Public Sub LoadAssembly(ByVal path As String)
_assembly = Assembly.Load(AssemblyName.GetAssemblyName(path))
End Sub
Public Function GetAssembly(ByVal path As String) As Assembly
Return Assembly.Load(AssemblyName.GetAssemblyName(path)) 'this doesn't throw an error
End Function
End Class
Public Sub Add2(ByVal PathToDll As String)
Dim ad As AppDomain = AppDomain.CreateDomain("TempPluginDomain")
Dim l As Loader = ad.CreateInstanceAndUnwrap(
GetType(Loader).Assembly.FullName,
GetType(Loader).FullName
)
Dim theDll As Assembly = l.GetAssembly(PathToDll) 'error happens here
'there's another way to do it that makes the exact point of the error clear:
'Dim theDll As Assembly = Nothing
'l.LoadAssembly(PathToDll) 'No problems here. The _assembly variable is successfully set
'theDll = l.TheAssembly 'Here's where the error occurs, as soon as you try to read that _assembly variable.
AppDomain.Unload(ad)
End Sub
Can anyone point me in the right direction so I can load and unload DLLs only as-needed and without any dependency errors?
I think I finally got it. It ended up being a few things - I needed the shared DLLs all in one place, and as Hans mentioned above, I needed my appdomains squared away. My solution architecture looks like this: a folder with all my plugin projects; a "Shared Objects" assembly with one class file for the base plugin architecture, and a second class containing my "plugin wrapper" class and supporting classes; and a console app that ties everything together. Each plugin project has a project reference to the shared objects project, as does the console app. Nothing references the plugins directly.
So in my Shared Objects project, I have the code for my PluginBase class and my IPlugin interface:
Public Interface IPlugin
ReadOnly Property Result As Integer
Sub Calculate(ByVal param1 As Integer, ByVal param2 As Integer)
End Interface
Public MustInherit Class PluginBase
Inherits MarshalByRefObject
'None of this is necessary for the example to work, but I know I'll need to use an inherited base class later on so I threw it into the example now.
Protected ReadOnly Property PluginName As String
Get
Return CustomAttributes("AssemblyPluginNameAttribute")
End Get
End Property
Protected ReadOnly Property PluginGUID As String
Get
Return CustomAttributes("AssemblyPluginGUIDAttribute")
End Get
End Property
Protected IsInitialized As Boolean = False
Protected CustomAttributes As Dictionary(Of String, String)
Protected Sub Initialize()
CustomAttributes = New Dictionary(Of String, String)
Dim attribs = Me.GetType.Assembly.GetCustomAttributesData
For Each attrib In attribs
Dim name As String = attrib.Constructor.DeclaringType.Name
Dim value As String
If attrib.ConstructorArguments.Count = 0 Then
value = ""
Else
value = attrib.ConstructorArguments(0).ToString.Replace("""", "")
End If
CustomAttributes.Add(name, value)
Next
IsInitialized = True
End Sub
End Class
<AttributeUsage(AttributeTargets.Assembly)>
Public Class AssemblyPluginNameAttribute
Inherits System.Attribute
Private _name As String
Public Sub New(ByVal value As String)
_name = value
End Sub
Public Overridable ReadOnly Property PluginName As String
Get
Return _name
End Get
End Property
End Class
<AttributeUsage(AttributeTargets.Assembly)>
Public Class AssemblyPluginGUIDAttribute
Inherits System.Attribute
Private _g As String
Public Sub New(ByVal value As String)
_g = value
End Sub
Public Overridable ReadOnly Property PluginGUID As String
Get
Return _g
End Get
End Property
End Class
And I have my PluginWrapper class with its supporting classes:
Imports System.IO
Imports System.Reflection
''' <summary>
''' The wrapper for plugin-related activities.
''' </summary>
''' <remarks>Each wrapper contains: the plugin; code to load and unload it from memory; and the publicly-exposed name and GUID of the plugin.</remarks>
Public Class PluginWrapper
Private _pluginAppDomain As AppDomain = Nothing
Private _isActive As Boolean = False
Private _plugin As IPlugin = Nothing
Private _pluginInfo As PluginInfo = Nothing
Private _pluginPath As String = ""
Public ReadOnly Property IsActive As Boolean
Get
Return _isActive
End Get
End Property
Public ReadOnly Property PluginInterface As IPlugin
Get
Return _plugin
End Get
End Property
Public ReadOnly Property PluginGUID As String
Get
Return _pluginInfo.PluginGUID
End Get
End Property
Public ReadOnly Property PluginName As String
Get
Return _pluginInfo.PluginName
End Get
End Property
Public Sub New(ByVal PathToPlugin As String)
_pluginPath = PathToPlugin
End Sub
Public Sub Load()
Dim l As New PluginLoader(_pluginPath)
_pluginInfo = l.LoadPlugin()
Dim setup As AppDomainSetup = New AppDomainSetup With {.ApplicationBase = System.IO.Directory.GetParent(_pluginPath).FullName}
_pluginAppDomain = AppDomain.CreateDomain(_pluginInfo.PluginName, Nothing, setup)
_plugin = _pluginAppDomain.CreateInstanceAndUnwrap(_pluginInfo.AssemblyName, _pluginInfo.TypeName)
_isActive = True
End Sub
Public Sub Unload()
If _isActive Then
AppDomain.Unload(_pluginAppDomain)
_plugin = Nothing
_pluginAppDomain = Nothing
_isActive = False
End If
End Sub
End Class
<Serializable()>
Public NotInheritable Class PluginInfo
Private _assemblyname As String
Public ReadOnly Property AssemblyName
Get
Return _assemblyname
End Get
End Property
Private _typename As String
Public ReadOnly Property TypeName
Get
Return _typename
End Get
End Property
Private _pluginname As String
Public ReadOnly Property PluginName As String
Get
Return _pluginname
End Get
End Property
Private _pluginguid As String
Public ReadOnly Property PluginGUID As String
Get
Return _pluginguid
End Get
End Property
Public Sub New(ByVal AssemblyName As String, ByVal TypeName As String, ByVal PluginName As String, ByVal PluginGUID As String)
_assemblyname = AssemblyName
_typename = TypeName
_pluginname = PluginName
_pluginguid = PluginGUID
End Sub
End Class
Public NotInheritable Class PluginLoader
Inherits MarshalByRefObject
Private _pluginBaseType As Type = Nothing
Private _pathToPlugin As String = ""
Public Sub New()
End Sub
Public Sub New(ByVal PathToPlugin As String)
_pathToPlugin = PathToPlugin
Dim ioAssemblyFile As String = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(_pathToPlugin), GetType(PluginBase).Assembly.GetName.Name) & ".dll")
Dim ioAssembly As Assembly = Assembly.LoadFrom(ioAssemblyFile)
_pluginBaseType = ioAssembly.GetType(GetType(PluginBase).FullName)
End Sub
Public Function LoadPlugin() As PluginInfo
Dim domain As AppDomain = Nothing
Try
domain = AppDomain.CreateDomain("Discovery")
Dim loader As PluginLoader = domain.CreateInstanceAndUnwrap(GetType(PluginLoader).Assembly.FullName, GetType(PluginLoader).FullName)
Return loader.Load(_pathToPlugin)
Finally
If Not IsNothing(domain) Then
AppDomain.Unload(domain)
End If
End Try
End Function
Private Function Load(ByVal PathToPlugin As String) As PluginInfo
Dim r As PluginInfo = Nothing
Try
Dim objAssembly As Assembly = Assembly.LoadFrom(PathToPlugin)
For Each objType As Type In objAssembly.GetTypes
If Not ((objType.Attributes And TypeAttributes.Abstract) = TypeAttributes.Abstract) Then
If Not objType.GetInterface("SharedObjects.IPlugin") Is Nothing Then
Dim attribs = objAssembly.GetCustomAttributes(False)
Dim pluginGuid As String = ""
Dim pluginName As String = ""
For Each attrib In attribs
Dim name As String = attrib.GetType.ToString
If name = "SharedObjects.AssemblyPluginGUIDAttribute" Then
pluginGuid = CType(attrib, AssemblyPluginGUIDAttribute).PluginGUID.ToString
ElseIf name = "SharedObjects.AssemblyPluginNameAttribute" Then
pluginName = CType(attrib, AssemblyPluginNameAttribute).PluginName.ToString
End If
If (Not pluginGuid = "") And (Not pluginName = "") Then
Exit For
End If
Next
r = New PluginInfo(objAssembly.FullName, objType.FullName, pluginName, pluginGuid)
End If
End If
Next
Catch f As FileNotFoundException
Throw f
Catch ex As Exception
'ignore non-valid dlls
End Try
Return r
End Function
End Class
Finally, each plugin project looks a little like this:
Imports SharedObjects
<Assembly: AssemblyPluginName("Addition Plugin")>
<Assembly: AssemblyPluginGUID("{4EC46939-BD74-4665-A46A-C99133D8B2D2}")>
Public Class Plugin_Addition
Inherits SharedObjects.PluginBase
Implements SharedObjects.IPlugin
Private _result As Integer
#Region "Implemented"
Public Sub Calculate(ByVal param1 As Integer, ByVal param2 As Integer) Implements SharedObjects.IPlugin.Calculate
If Not IsInitialized Then
MyBase.Initialize()
End If
_result = param1 + param2
End Sub
Public ReadOnly Property Result As Integer Implements SharedObjects.IPlugin.Result
Get
Return _result
End Get
End Property
#End Region
End Class
To set it all up, the main program creates a new instance of the PluginWrapper class, supplies a path to a DLL, and loads it:
Dim additionPlugin As New PluginWrapper("C:\path\to\Plugins\Plugin_Addition.dll")
additionPlugin.Load()
Once you're done doing whatever you need to do with the program...
additionPlugin.PluginInterface.Calculate(3, 2)
...and retrieving the results...
Console.WriteLine("3 + 2 = {0}", additionPlugin.PluginInterface.Result)
...just unload the plugin:
additionPlugin.Unload()
If you need to reload it while the wrapper is still in memory, just call the Load() method again - all the information it needs to create a new AppDomain and reload the assembly is in there. And, in answer to my initial question, once the Unload() method has been called, the assembly is freed and can be replaced/upgraded as necessary, which was the whole point of doing this in the first place.
Part of where I was getting tripped up earlier was that I wasn't including the SharedObjects.dll file in the same folder as the plugins. What I found is that any referenced assembly needs to be present. So in my post-build events for both my plugins and the Shared Objects project, I have this: xcopy /y $(ProjectDir)$(OutDir)$(TargetFileName) c:\path\to\Plugins. Every time I build the solution, all the DLLs are placed in the folder where they need to be.
Sorry if this is a little long, but this is a little complicated. Maybe there's a shorter way to do it...but at the moment, this gets me everything I need.