How do I create a com object?

  • Thread starter Thread starter Andy
  • Start date Start date
A

Andy

I have a simple atl com object which I created with visual C++.
In vb6 there was two ways to use this object. 1. I could add
it as a componenet, and then it appered on the toolbox, and
2. I could add it as a reference, and then use CreateObject
in runtime. Please explain how this has changed for .net.
 
Right click your toolbox and choose Add/Remove Items. From the Customize
Toolbox dialog, select the COM tab. Locate your COM component, if it is
registered on the system, and choose Ok, The component should now appear in
your toolbox. .NET will create the necessary wrappers for you and you can
now use the component in your code.

If you do not see your component listed in the available components, choose
Browse to locate the item on your hard drive.
 
Andy said:
How do I respond to events ?


Assume you have a com object named ABC.

Then you would go:


Private Sub EventHandler(Byval sender as object, Byval e as eventargs)
handles ABC.SomeEvent

End Sub


Byval sender as object, Byval e as eventargs depends on the obeject you're
handling the event for - different events may have different parameters.

Also, make sure you declare ABC withevents as:

Private WithEvents ABC as SomeComObject
 
I did it differently, I used

Dim mycom As New paLib.o1
AddHandler mycom.yy, AddressOf event1
..
..
..

Sub event1()
MsgBox("ev")
End Sub

Didn't dim "withevents", but it *did* work, maybe you know why.

Thanks
Andy.
 
Andy said:
I did it differently, I used

Dim mycom As New paLib.o1
AddHandler mycom.yy, AddressOf event1
.
.
.

Sub event1()
MsgBox("ev")
End Sub

Didn't dim "withevents", but it *did* work, maybe you know why.

It's basically the same thing - you explicitly created an event handler in
code.

Eiher method works fine - it depends on which method you prefer. AddHandler
does have one advantage, it allows you to dynamically create event handlers
during runtime.
 
Back
Top