By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

Help Required Interfacing COM and .NET

P: n/a
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! :-)
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 20 '05 #2

P: n/a
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:

"Michael M" <mr*****@yahoo.com> wrote in message
news:e1**************************@posting.google.c om...
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! :-)

Nov 20 '05 #3

P: n/a
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
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.