Calling .NET from VB6

AlisonB

Newcomer
Joined
Aug 20, 2003
Messages
6
We have a VB6 application which dynamically loads OCXs as they are chosen by users. We'd like to start moving to .NET.

Is there any way we can write controls in .NET and still call them from the main VB6 app, or would we have to re-write this first?
 
Moving the whole application would be better then single components. You can create a wrapper for .NET class and use it, but framework will still be required.
Go to your projects properties, then from the left choose Configuration Properties, then build. Then check, Register For COM Interop.
 
Last edited:
There is other stuff you need to do, too, like use the GUID Generator to make you GUIDs for the COM object, the typelib, etc. You also need to make sure you give your class the ComClass attribute. Read the MSDN for more info.
 
Thanks for this - still a couple of teething problems, but this certainly got us started.

We'd really like to rewrite everything in one go, but there are time constraints and a Testing Manager who's liable to throw a wobbly if we suggest it!
 
If you need some help with the COM Class, please let me know.
I'm finishing something like that now ;) and I have a simple sample that might help you.
 
To test this you should open a new project (Visual Basic Project \ Class Library) and copy the code below to the class:

Code:
Imports System.Runtime.InteropServices

<Assembly: ComVisible(False)> 
<Assembly: ClassInterface(ClassInterfaceType.None)> 

<ComVisible(True), Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"), InterfaceType(ComInterfaceType.InterfaceIsDual)> _
Public Interface SubsToExpose

   <DispIdAttribute(1)> Sub Load(ByVal strConn As String, ByVal strSelect As String, ByVal strTemDoc As String, ByVal strArcDoc As String)
   <DispIdAttribute(2)> Sub ExecuteWord()

   'NOTE:  If you add in more methods, increment DispIdAttribute by one for each...

End Interface

<ComVisible(True), Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"), ClassInterface(ClassInterfaceType.None), ProgId("YourLibNameHere.YourClassNameHere")> _
Public NotInheritable Class clsAutomation
   Implements SubsToExpose

   Private Sub SubsToExpose_Load(ByVal strConn As String, ByVal strSelect As String, ByVal strTemDoc As String, ByVal strArcDoc As String) Implements SubsToExpose.Load
      On Error GoTo ShowError

      gstrTitle = Diagnostics.Process.GetCurrentProcess.ProcessName

      gstrConn = strConn
      gstrSelect = strSelect

      gstrTemDoc = strTemDoc
      gstrArcDoc = strArcDoc

      SetLicence()

      Exit Sub
ShowError:
      InfoError(Err.Number, Err.Description)
      Exit Sub
   End Sub

   Private Sub SubsToExpose_ExecuteWord() Implements SubsToExpose.ExecuteWord
      On Error GoTo ShowError

      ReplaceWord()

      Exit Sub
ShowError:
      InfoError(Err.Number, Err.Description)
      Exit Sub
   End Sub
End Class

It seems to be a little complicated at first time, but if you use this class just to gave a name to the procedures is very simple.

I have some modules with the code itself and here I just call the code.

The guid is a kind of identity, and can generate it with a very little program i've made just for this. (Attached) Then you just have to press 'Create new Guid' and replace XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX in the code by the string in the textbox.

You you have posted all public functions and procedures you'll need in the class go to Project Properties\Configuration properties\Build and check Register for COM Interop.

You'll have a .dll File, a .pdb File and .tlb File.

When you finish this, please let me know if you don't know how to register the file.
 

Attachments

Back
Top