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 / January 2007

Tip: Looking for answers? Try searching our database.

COM InterOp - Interface ?s (Great COM InterOp Links within)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JerryWEC - 26 Jan 2007 16:54 GMT
Hi all,  below is most of my code for my COM InterOp object (For those following previous posts - I'm not using Inheritance any longer).

My class Logging is implementing ILogging and exposing LogToFile() and LogToScreen().  In the VB6 project the properties are showing up under the ILogging interface which is public.  Is this the best way to expose my "Non-Event" methods (subs, functions, properties) to the COM world (VB6)?  Are there other options to expose my public methods and properties?

Note: The ILoggingEvents interface is used to handle the events of my Logging class (ComSourceInterfaces() attribute).

<Guid("26014480-5D35-49ed-B83C-1B862BF92517")> _

<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _

Public Interface ILoggingEvents

   <DispId(1)> _

   Sub LogExitDisplayLoggingWindow()

   <DispId(2)> _

   Sub LogErrorOccurred(ByVal outEx As Exception)

End Interface



<Guid("542FA6CF-F2C8-46d3-8601-EF370BBEF146")> _

<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _

Public Interface ILogging

   Property LogToFile() As Boolean

   Property LogToScreen() As Boolean

End Interface



#End Region



<Guid("64439B0F-D1BF-43d7-BCD9-BA908BF2CCF6")> _

<ClassInterface(ClassInterfaceType.None)> _

<ProgId("CLA_Logging.Logging")> _

<ComSourceInterfaces(GetType(ILoggingEvents))> _

Public Class Logging

   Implements IDisposable

   Implements ILogging



   Public Delegate Sub LogExitDisplayLoggingWindowHandler()

   Public Delegate Sub LogErrorOccurredHandler(ByVal outEx As Exception)

   Public Event LogExitDisplayLoggingWindow As LogExitDisplayLoggingWindowHandler

   Public Event LogErrorOccurred As LogErrorOccurredHandler



   Public Sub New()

       Try

           If m_formInstance Is Nothing Then

               m_formInstance = New frmLogDisplay()

               m_formInstance.ParentLogging = Me

           End If

       Catch ex As Exception

           ProcessException(ex)

       End Try

   End Sub



   Public Property LogToFile() As Boolean Implements ILogging.LogToFile

       Private Get

           Try

               Return m_LogToFile

           Catch ex As Exception

               ProcessException(ex)

           End Try

       End Get

       Set(ByVal value As Boolean)

           Try

               m_LogToFile = value

           Catch ex As Exception

               ProcessException(ex)

           End Try

       End Set

   End Property



   Public Property LogToScreen() As Boolean Implements ILogging.LogToScreen

       Private Get

           Try

               Return m_LogToScreen

           Catch ex As Exception

               ProcessException(ex)

           End Try

       End Get

       Set(ByVal value As Boolean)

           Try

               m_LogToScreen = value

           Catch ex As Exception

               ProcessException(ex)

           End Try

       End Set

   End Property



'*** Other code not shown.

End Class

Note: I'll have to change my event that is using the Exception object. I'll probably have to make additional changes as well.

Great Links:

(Part 1)

http://www.15seconds.com/issue/040721.htm

(Part 2)...

http://www.15seconds.com/issue/060309.htm

http://www.codeproject.com/dotnet/cominterop.asp

...(See Part 2)

TIA JerryM
JerryWEC - 26 Jan 2007 19:39 GMT
In addition to the two previous questions...

In the VB6 Object browser I can see the ILogging interface and all of the methods in the interface. However, when writing code all that is seen in intellisense is .Dispose from IDisposable.  What do I need to do to modify my previous code to see the methods exposed in ILogging?

What about the two events in the ILoggingEvents interface?
 TIA JerryM
JerryWEC - 26 Jan 2007 22:24 GMT
Hi everyone!

I just figure out one peice of the COM InterOp puzzle... I switched the order of my interfaces inside the class definition.  I put ILogging before IDisposible and not .Dispose() does not show up in the VB6 object browser but my other methods do.

Have a great weekend!

TIA JerryM
Walter Wang [MSFT] - 29 Jan 2007 09:13 GMT
Hi Jerry,

Thank you for sharing the two article links about COM interop.

As you already discovered, the first interface will be the default
interface, and VB6 intellisense only works for the default interface.

I think manually use the attributes instead of ComClass attribute is
preferred if you're concerned about the version compatibility issue that
the ComClass may suffer; since ComClass attribute still generates the
interface on your behalf, explicitly control the interface and DispId
certainly put everything under your control.

In case you haven't read following article, it's worth reading:

http://www.dotnetinterop.com/faq/?q=ClassInterface

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Walter Wang [MSFT] - 29 Jan 2007 09:20 GMT
Hi Jerry,

As for the exception handling, the COM client will get the error message
when an exception is thrown from a method exposed from .NET COM component.
For example:

<Guid("542FA6CF-F2C8-46d3-8601-EF370BBEF146")> _
<InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface ILogging
   Property LogToFile() As Boolean
   Sub Log(ByVal text As String)
End Interface

   Public Sub Log(ByVal text As String) Implements ILogging.Log
       If String.IsNullOrEmpty(text) Then
           Throw New ArgumentException("text cannot be empty")
       End If
   End Sub

Calling this Log method from VB6 with .Log("") will correctly capture the
error message "text cannot be empty" in Err.

However, this mechanism does have a caveat as described here:

#Interop: Get Seamless .NET Exception Logging From COM Clients Without
Modifying Your Code -- MSDN Magazine, January 2005
http://msdn.microsoft.com/msdnmag/issues/05/01/ExceptionLogging/default.aspx

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
JerryWEC - 30 Jan 2007 19:17 GMT
Hi all!

I have another question related to COM InterOp...

I have this one event that was originally designed to sent back an exception
object.  Is there anyway to MarshalAs as string the execption object or some
way to send back the ex.Message concatinated with the ex.stacktrace?  Or do
I have to change my .net code to accomadate COM?

TIA, JerryM
Walter Wang [MSFT] - 31 Jan 2007 03:41 GMT
Hi Jerry,

MarshalAs attribute will be used to help the marshaler understand what the
native memory layout is supposed to look like. It will not be used to help
concat two strings. Also, the article I mentioned in my last reply clearly
states that getting the stack trace from .NET is tricky.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

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.