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 / April 2005

Tip: Looking for answers? Try searching our database.

Problem implementing a COM interface

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Craig - 25 Apr 2005 15:14 GMT
Hi
I am trying to implement a COM interface, defined in a Type Library, in a
VB.Net COM Class.
At this point it is a proof of concept project and I am trying to use the
minimum amount of code but I have to implement the interface
MyTypeLib.IInterface1.

In the implemented interface is the following property

  Public Property Key() As Object Implements MyTypeLib.IInterface1.Key
     Get
        Key = m_objKey
     End Get
     Set(ByVal Value As Object)
        m_objKey = Value
     End Set
  End Property

Key is defined in the interface as a Variant and is translated to an Object.
However, for the Let procedure I get this warning:-

'Public Property Key() As Object' cannot be exposed to COM as a property
'Let'. You will not be able to assign non-object values (such as numbers or
strings) to this property from Visual Basic 6.0 using a 'Let' statement.

I ignored the warning and tried to run the software but I get errors:
'Specified cast is not valid' and 'Automation Error' (440)

This error may be the result of something else in the code. For example, I
am trying to pass an icon via the interface via the implemented interface
property:-

  Public ReadOnly Property Icon() As stdole.StdPicture Implements
MyTypeLib.IInterface1.Icon
     Get
        Icon = CType(New Icon("EmisPocUI.ico"), stdole.StdPicture)
     End Get
  End Property

Two questions:
1). How can I work around the Key property problem?
2). Is there anything obviously wrong with the Icon implementation?

Thanks in advance.
Signature

Regards,
Craig

Craig - 26 Apr 2005 12:04 GMT
Hi
I can confirm the error 'Specified cast is not valid' is arising when the
Icon property is accessed.

>    Public ReadOnly Property Icon() As stdole.StdPicture Implements
> MyTypeLib.IInterface1.Icon
>       Get
>          Icon = CType(New Icon("EmisPocUI.ico"), stdole.StdPicture)
>       End Get
>    End Property

What's wrong with the Icon assignment?
Craig

> Hi
> I am trying to implement a COM interface, defined in a Type Library, in a
[quoted text clipped - 40 lines]
>
> Thanks in advance.
Nick Hall - 26 Apr 2005 13:22 GMT
Craig,

Unfortunately it's not quite that simple!

The NET equivalent of the StdPicture class (System.Drawing.Bitmap,
System.Drawing.Icon) are not directly compatible - you need to do the
conversion yourself using the OleCreatePictureIndirect API.

I created the following code to create and return a StdPicture containing a
bitmap; you should be able to adapt it to your needs.

<StructLayout(LayoutKind.Explicit)> _
Private Structure PICTDESC
     ' Methods
Public Sub New(ByVal hBitmap As IntPtr) : Me.New(hBitmap, IntPtr.Zero)
End Sub

Public Sub New(ByVal type As PICTYPE, ByVal handle As IntPtr) : Me.New(type,
handle, IntPtr.Zero, 0, 0)
End Sub

Public Sub New(ByVal hMetafile As IntPtr, ByVal x As Integer, ByVal y As
Integer) : Me.New(PICTYPE.PICTYPE_METAFILE, hMetafile, IntPtr.Zero, x, y)
End Sub

Private Sub New(ByVal type As PICTYPE, ByVal h1 As IntPtr, ByVal h2 As
IntPtr, ByVal x As Integer, ByVal y As Integer)
     Me.cbSizeOfStruct = 0
     Me.picType = type
     Me.hBitmap = IntPtr.Zero
     Me.hEnhMetaFile = IntPtr.Zero
     Me.hIcon = IntPtr.Zero
     Me.hMetafile = IntPtr.Zero
     Me.hPalette = IntPtr.Zero
     Me.xExt = 0
     Me.yExt = 0
     Me.cbSizeOfStruct = Marshal.SizeOf(Me)
     Select Case type
           Case PICTYPE.PICTYPE_BITMAP
                 Me.hBitmap = h1
                 Me.hPalette = h2
                 Return
           Case PICTYPE.PICTYPE_METAFILE
                 Me.hMetafile = h1
                 Me.xExt = x
                 Me.yExt = y
                 Return
           Case PICTYPE.PICTYPE_ICON
                 Me.hIcon = h1
                 Return
           Case PICTYPE.PICTYPE_ENHMETAFILE
                 Me.hEnhMetaFile = h1
                 Return
     End Select
     Throw New NotSupportedException(String.Format("picType '{0}' is not
supported", Me.picType.ToString))
End Sub

     ' Fields
     <MarshalAs(UnmanagedType.U4), FieldOffset(0)> _
     Public cbSizeOfStruct As Integer
     <FieldOffset(8)> _
     Public hBitmap As IntPtr
     <FieldOffset(8)> _
     Public hEnhMetaFile As IntPtr
     <FieldOffset(8)> _
     Public hIcon As IntPtr
     <FieldOffset(8)> _
     Public hMetafile As IntPtr
     <FieldOffset(12)> _
     Public hPalette As IntPtr
     <MarshalAs(UnmanagedType.U4), FieldOffset(4)> _
     Public picType As PICTYPE
     <FieldOffset(12)> _
     Public xExt As Integer
     <FieldOffset(16)> _
     Public yExt As Integer
End Structure

Private Enum PICTYPE
     ' Fields
     PICTYPE_BITMAP = 1
     PICTYPE_ENHMETAFILE = 4
     PICTYPE_ICON = 3
     PICTYPE_METAFILE = 2
     PICTYPE_NONE = 0
     PICTYPE_UNINITIALIZED = -1
End Enum

<DllImport("OLEPRO32.DLL", PreserveSig:=False)> _
Private Shared Function OleCreatePictureIndirect(<In> ByRef pPictDesc As
PICTDESC, <In> ByRef riid As Guid, ByVal fOwn As Boolean) As
<MarshalAs(UnmanagedType.IUnknown)> Object
End Function

'*** Example code

           Dim pictdesc1 As PICTDESC
           Dim bitmap1 As Bitmap = Me.SignatureImage
           If (bitmap1 Is Nothing) Then
                 Return Nothing
           End If

           Dim ptr1 As IntPtr = bitmap1.GetHbitmap
           pictdesc1 = New PICTDESC(ptr1)
           Dim type1 As Type = GetType(IPictureDisp)
           Dim guid1 As Guid = type1.GUID
           Return DirectCast(Signature.OleCreatePictureIndirect(pictdesc1,
guid1, True),IPictureDisp)

Hope this helps,

Nick Hall

> Hi
> I can confirm the error 'Specified cast is not valid' is arising when the
[quoted text clipped - 57 lines]
>>
>> Thanks in advance.
Craig - 26 Apr 2005 13:59 GMT
Hi Nick,
Thanks for the reply.

I'll let you know how I get on.

Craig
(It's not so easy migrating from VB6 to VB.NET after all)

> Craig,
>
[quoted text clipped - 171 lines]
> >>
> >> Thanks in advance.
Craig - 26 Apr 2005 18:23 GMT
Hi,
I'm glad to say, after a little more searching I found a simpler solution
(my VB.NET skills are not quite up to Nick's).

I have implemented a resource file and I have added a reference to the
Microsoft.VisualBasic.Compatibility assembly. It's the latter that provides
the cast capability (VB6.IconToIPicture). The Icon property now contains the
following code:-

        Dim resMan As New
System.Resources.ResourceManager("MyAssembly.MyAssemblyResource",
Me.GetType.Assembly)
        Dim resIcon As System.Drawing.Icon
        resIcon = CType(resMan.GetObject("MyIcon"), System.Drawing.Icon)
        Icon =
Microsoft.VisualBasic.Compatibility.VB6.IconToIPicture(resIcon)
        resIcon.Dispose()

Regards
Craig

> Craig,
>
[quoted text clipped - 171 lines]
> >>
> >> Thanks in advance.

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.