HighTechTalks DotNet Forums  

Simple VB6 - DotNet Example

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss Simple VB6 - DotNet Example in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Rory Becker
 
Posts: n/a

Default Simple VB6 - DotNet Example - 11-13-2005 , 05:31 AM






Assuming that the following is wrapped in a suitable VB.net project with
register for interop turned on

Option Strict On
Imports System.Runtime.InteropServices
<ComVisible(True)> _
Public Interface IClass
ReadOnly Property Message() As String
End Interface
<ComVisible(True)> _
Public Class Class1
Implements IClass
Public ReadOnly Property Message() As String Implements
IClass.Message
Get
Return "Test Message"
End Get
End Property
End Class
<ComVisible(True)> _
Public Class ClassFactory
Public Function CreateIClass() As IClass
Return New Class1
End Function
End Class


I now compile this and reference it from a simple VB6 Project(1 form
with 1 Button) thus:

Option Explicit
Private Sub Command1_Click()
Dim X As TestLibrary1.ClassFactory
Set X = New ClassFactory
Dim Y As TestLibrary1.IClass
set Y = X.CreateIClass
MsgBox (Y.Message)
End Sub

Now I compile to an exe in the dotnet\Bin folder of the first project

Now this works but I have a few questions.


1.> Why no intellisense For my X Object?
2.> I have seen code with the following dotnet attributes... How would I
benefit from these?
a.> ClassInterface(ClassInterfaceType.AutoDual)
b.> ProgId("Some.NameSpace.And.ClassName")
c.> Guid("SOME GUID")

Thanks very much in advance

Rory




Reply With Quote
  #2  
Old   
TDC
 
Posts: n/a

Default Re: Simple VB6 - DotNet Example - 11-15-2005 , 09:06 AM







Quote:
1.> Why no intellisense For my X Object?
That has to do with the defaults when using .NET Interop

Quote:
2.> I have seen code with the following dotnet attributes... How would I
benefit from these?
a.> ClassInterface(ClassInterfaceType.AutoDual)
b.> ProgId("Some.NameSpace.And.ClassName")
c.> Guid("SOME GUID")
This is what I recoomend:

1. At the Assembly level, make ComVisible False. This allows you to
only expose those classes and interfaces that you want.

2. Use ComVisible True on your Interfaces so they will be exposed to
VB. Also use InterfaceType of InterfaceIsDual. This will allow the
interface to be used via both early and late binging. (You don't have
Intellisense now because you are only supporting late-binding).

3. In the classes that implement these interfaces, again use
ComVisible True but the ClassInterface should be None. This is
because you are explicitly implementing an interface you have exposed
to COM, and you don't want the compiler/typeexporter to try to do it
for you.

At the bare minimum, that should give a great start to doing COM
interop so it works well with VB6.

I usually create GUIDs explicitly, but if you don't then one will be
generated for you. The ProgId lets you create VB6-friendly names
because otherwise you might have conflicts with longer Assembly names,
and I usually take advantage of this feature as well.

Finally, I use the DispIdAttribute on each of my Interfaces methods.
Again they would be generated for you if you don't do it, but I like
everything to be explicit.

Good luck!
Tom



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.