Coomunicatioin between .Net and VB6 Apps

M

Martin Horner

Hi,

I have an existing VB6 application that I intend to add some additional
functionality to. This app will be eventually converted to .NET but probably
not in the near future. The new functionality will not have a really close
dependency on the exiting application other than sharing a common database.
One option that I have been considering is to build the new functionality
into a VB.Net application. It is likely that I will use a standard EXE or an
ActiveX.Exe in VB6 because the existing application is MDI and I also need a
new MDI window for the new functionality.

So here is my question. I am looking for a simple way to establish two way
communications between the two applications. I would like to avoid using the
SendMessage function if possible so that the communications has room to
grow. In the first instance this is what I would like to do:
1. Start the .NET application from the VB6 or activate it if it is running.
2. Provide it with some start up data.
3. Optionally hide or leave the VB6 application running when the .Net
application is running.
3. Activate or close the VB6 application when the .Net application closes

Ideally I would also like to raise events (or otherwise send messages) from
the .Net application to the VB6 application. Can anyone point me in the
right direction on the best tools I can use to implement this? In VB6 I
would almost certainly implement the new application as an ActiveX server.
So I guess I am looking for a replacement for this in a combined COM and
..Net world.

Thanks.
 
S

smith

This is what Interop is all about.

Make your new VB.Net app and give it events you want ot expose to the VB6
app, add COM GUIDs, compile and build a type library.

declare the tlb in your vb6 app WithEvents and you can code against those
events (and all of the Public objects and interfaces) ... there's your
communication.

Pick up the Lhotka & Hollis book on Iterop:

http://www.amazon.com/exec/obidos/t...103-4703029-8183852?v=glance&s=books&n=507846

It was on WROX but you should still be able to find it at bigger bookstores.

You'll find it all falls into place very simply ... wait till you get into
using autodeploy on those vb7 applets you're calling from VB6! (And then
when you make all of those autodeployed applets use remoting!) That's when
this stuff gets fun to code :)

-smith
kirkland, wa
 
M

Martin Horner

That is very good information. One of the reasons I was looking at an
ActiveX.Exe in VB6 is that it allowed both the old and the new application
to use an MDI. When I put together a quick pair of test applications I found
that both the client VB6 and the server .Net application could show MDI
windows. This is great if there are no adverse side effects (and the only
reason that I think there might be is because you can't add one to a VB6
dll.

In my test I couldn't declare the .Net component WithEvents but I guess the
technique for doing this must be included in Professional Visual Basic
Interopability that you recommended and that I will get hold of

Thanks again.
 
P

Peter Huang

Hi Martin,

Here I write a sample about how to expose the event of .NET component to
VB6.
[TestComObject.Class1]
Imports System.Runtime.InteropServices
Public Delegate Sub testdelegate()
<GuidAttribute("1F98211C-7A71-4588-8D4A-AD85CA80BAE7"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface ControlEvents
<DispIdAttribute(1)> _
Sub testevent()
End Interface
<ComSourceInterfaces(GetType(ControlEvents)),
ClassInterface(ClassInterfaceType.AutoDual)> _
Public Class Class1
Dim WithEvents fm As Form1
Public Sub test()
fm = New Form1
fm.Show()
End Sub
Public Event testevent()
Private Sub fm_Closed(ByVal sender As Object, ByVal e As
System.EventArgs) Handles fm.Closed
RaiseEvent testevent()
End Sub
End Class


[VB6 code]
Dim WithEvents o As TestComObject.Class1
Private Sub Command1_Click()
Set o = New TestComObject.Class1
o.test
End Sub
Private Sub o_testevent()
MsgBox "event fired"
End Sub

HOW TO: Sink Managed Visual Basic .NET Events in Internet Explorer Script
http://support.microsoft.com/?kbid=316516

Exposing .NET Framework Components to COM
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconexposingnetframeworkcomponentstocom.asp

If you have any concern please post here.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

Martin Horner

Hi Peter,

That sample is great and the links are very useful too. This gives me the
foundation to perform all the processing I will need to do in the first
instance. I thought I would need to wait until I got some more reference
material but I now have everything I need to get going. Thanks

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top