Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2005

Tip: Looking for answers? Try searching our database.

Simple VB6 - DotNet Example

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rory Becker - 13 Nov 2005 10:31 GMT
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
TDC - 15 Nov 2005 14:06 GMT
> 1.> Why no intellisense For my X Object?

That has to do with the defaults when using .NET Interop

> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.