Exposing .Net Structure to COM

cpsweeper

New member
Joined
Jan 5, 2004
Messages
3
I am trying to expose a .NET Structure to COM in a DLL.
I would like to do this within an exposed .Net CLass. As coded the structure is not exposed to VB6.

MSDN indicates that Types can be exposed but they must have a constructor with no parameters.

.Net will not allow me to declare "Public Sub New()" within a structure declaration in the class.

So far the only way I can see of doing this is to declare a separate class that has its own GUIDs and gets registered separately.


The example below is what I am trying to do. When referenced in VB6, the ComClass 1 is available, but the type MyType does not show up as a datatype.

Example:

Imports System.Runtime.InteropServices

<ComClass(ComClass1.ClassId, ComClass1.InterfaceId, ComClass1.EventsId)> _
Public Class ComClass1

#Region "COM GUIDs"
These GUIDs provide the COM identity for this class
and its COM interfaces. If you change them, existing
clients will no longer be able to access the class.
Public Const ClassId As String = "5A563725-8AD6-4E16-ACA8-7EA378A71762"
Public Const InterfaceId As String = "2423222E-2136-4515-8A67-E6E7B303E62C"
Public Const EventsId As String = "1863DD9A-6C50-49F1-8A61-8476C3CDCCFC"
#End Region

A creatable COM class must have a Public Sub New()
with no parameters, otherwise, the class will not be
registered in the COM registry and cannot be created
via CreateObject.
Public Sub New()
MyBase.New()
End Sub

Public Structure MyType
Public strTypeString As String
End Structure

Private strString As String

Public Property propString() As String
Get
propString = strString
End Get
Set(ByVal Value As String)
strString = Value
End Set
End Property
End Class
 
Thats because the class is exposed but the structure isnt, just because the structure is in the class wont expose it to COM. All that code does is place the structure in a namespace called "ComClass1". I dont know how to expose structures but you can do the same thing with a class, ie:
Code:
<ComClass(MyStructureClass.ClassId, MyStructureClass.InterfaceId, MyStructureClass.EventsId)> _
Public Class MyStructureClass

#Region "COM GUIDs"
 These GUIDs provide the COM identity for this class 
 and its COM interfaces. If you change them, existing 
 clients will no longer be able to access the class.
Public Const ClassId As String = "5A563725-8AD6-4E16-ACA8-7EA378A71762"
Public Const InterfaceId As String = "2423222E-2136-4515-8A67-E6E7B303E62C"
Public Const EventsId As String = "1863DD9A-6C50-49F1-8A61-8476C3CDCCFC"
#End Region

 A creatable COM class must have a Public Sub New() 
 with no parameters, otherwise, the class will not be 
 registered in the COM registry and cannot be created 
 via CreateObject.
Public Sub New()
MyBase.New()
End Sub

Public Structure MyType
Public _strTypeString As String
End Structure

Public Property strTypeString() As String
Get
 Return _strTypeString
End Get
Set(ByVal Value As String)
 _strTypeString = Value
End Set
End Property
End Class
This may look a pain, and it is, usually the approach taken to COM enabled .Net classes is to have a DLL that has classes that wrap the actual .Net classes, this allows you to have classes that VB6 uses like datatypes but the DLL converts the class to a .Net Structure for the main code to use. This also lets you take advantage of both since COM doesnt support overloads, if there is a wrapper you can still have the overloads because the program using the COM object doesnt know about the .Net code behind it.

Someone with more experience may know how to directly marshal the structures but it may not be possible
 
Thanks Ryan. Ill create new classes that .Net exposes to COM to hold my strutrures. It seems like a pain. But the stuctures that I need to pass are large and it is better than putting all the data in signature for the function call.
 
Back
Top