Interfacing with COM from .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.

- MM
 
M

Mattias Sjögren

However, I am getting an exception
when I try to do the following in my form:

What exception?

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

It should "just work".



Mattias
 
M

Michael M

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 Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Application.Exit()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim obj As New ADMB003.CADMB003Class()
Dim x As New Class1()

obj.DoHandle(x)
End Sub
End Class
-------
(Class1.vb)

Public Class Class1
Implements ADMB003.IADMB003

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

(CADMB003.vb)

Public Function DoHandle(ByRef obj As IADMB003) As Long
' just return junk
DoHandle = obj.DoProcess("this is a test") * 2
End Function
-------
(IADMB003.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.

HELP! :)

I even created a similar scenario where by
 
M

Michael M

Incidentally, on the code I just submitted, I am getting a
NullReferenceException on the Button1_Click sub on the line that
actually calls "DoHandle". I have checked both objects in previous
tests and both of them are indeed set to references. I believe it may
be something wrong with passing .NET objects to COM. I have to be
missing something...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim obj As New ADMB003.CADMB003Class()
Dim x As New Class1()

obj.DoHandle(x) <= NullReferenceException
End Sub
 

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