Help Required Interfacing COM and .NET

M

Michael M

All,

I have a silly issue that I'd like to handle. I am certain there is a
way to do this, but I can't seem to figure it out...so please help.

I have to VB6 objects that I'm working with:

CController
IHandler

IHandler is a VB6 "inteface" that declares a method (sub) "DoProcess"
CController is your basic object that has a method (sub) "DoHandle".
This DoHandle method expects a parameter of (ByRef) IHandler like so:

public sub DoHandle(ByRef objHandler as IHandler)

Within the DoHandle method in CController, the "DoProcess" method if
the implemented IHandler is called.

My .NET application has 1 class and one form.

the Class, CHandler, successfully implements the IHandler and the
IHandler.DoProcess method.

The form creates an instance of the CControllerClass and an instance
of the new .NET CHandler class. However, I am getting an exception
when I try to do the following in my form:

public sub cmdProcess_Click(...)
dim objController as new CControllerClass()
dim objHandler as new CHandler()

objController.DoHandle(objHandler) <= throws an exception
end sub

When the new CController tries to process the DoHandle method, it
bombs out.

What do I need to do to be able to pass a .NET object that implements
a VB6 interface back to a VB6 Component?

Any assistance will be greatly appreciated.

The exception I am getting is a NullReferenceException.

I have double checked my code and even created a test project with a
similar set up...to no avail...am I missing something?

Here is the code that I have:

-------
..NET:
-------
(Form1.vb)

Public Class Form1
Inherits System.Windows.Forms.Form

Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdExit.Click
Application.Exit()
End Sub

Private Sub cmdProcess_Click(ByVal sender As System.Object, ByVal e
As
System.EventArgs) Handles cmdProcess.Click
Dim obj As New ADMB003.CControllerClass()
Dim x As New Class1()

obj.DoHandle(x) ' <= Throws NullReferenceException...WHY?
End Sub
End Class
-------
(Class1.vb)

Public Class Class1
Implements ADMB003.IHandler

Public Function DoProcess(ByVal str As String) As Integer Implements
ADMB003.IHandler.DoProcess
Return Len(str)
End Function
End Class
-------
VB COM (Project name is ADMB003:

(CController.vb)

Public Function DoHandle(ByRef obj As IHandler) As Long
' just return junk
DoHandle = obj.DoProcess("this is a test") * 2
End Function
-------
(IHandler.vb - public not createable interface)

Public Function DoProcess(ByVal str As String) As Long
' to be implemented by child class
End Function
-------

The code in Class1 is as is...Am I missing something? I totally
thought this would be as simple as doing just what I did above, but I
seem to be having issues.

I am currently using Visual Studio .NET (not Visual Studio .NET 2003)
and 1.1 version of the .NET Framework.

HELP! :)
 
F

Fergus Cooney

Hi Michael,

Can I suggest that you stick MsgBox all over the place so that you can
track what's what at every step?

I think you'll find that CController can't call obj.DoProcess("..") as obj
is a .NET object.

Regards,
Fergus
 
S

solex

Michael,

I believe you will need to cast the object in the DoHandle call to IHandler.
For instance

Dim x as New Class1()

obj.DoHandle(CType(x, IHandler))

Here is my test source code, In vb6 my Handler.DLL contains:

IHandler Interface:
 
S

solex

Michael,

I believe you will need to cast the object in the DoHandle call to IHandler.
For instance:

Dim x as New Class1()

obj.DoHandle(CType(x, IHandler))

Test source code is shown below and I had no problems implementing the
interface,

In vb6 my Handler.DLL contains:

IHandler Interface:

Option Explicit

Public Sub DoProcess()

End Sub

CHandler Class:

Option Explicit

Public Sub DoHandle(ByRef aHandler As IHandler)
Call aHandler.DoProcess
End Sub

In VB.NET Console App:

Public Class AHander

Implements Handler.IHandler

Public Sub DoProcess() Implements Handler._IHandler.DoProcess

Debug.WriteLine("Do Process")

End Sub

End Class



Module Module1

Sub Main()

Dim actl As New Handler.CHandlerClass

Dim myhandler As New AHander

actl.DoHandle(CType(myhandler, Handler.IHandler))

End Sub

End Module



Dan
 

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