Interface implementation

fabio

Member
Joined
Jun 12, 2011
Messages
13
Programming Experience
5-10
Hello, I'm trying to use the following Interface implementation:

VB.NET:
Public Class DropTarget
    Implements [B]IDropTarget
[/B]
    ' ...
End Class

... in this function:

VB.NET:
Public Shared Function RegisterDragDrop(hwnd As IntPtr, pDropTarget As [B]IDropTarget[/B]) As Integer

but when I use an instance of DropTarget in the function, I get an InvalidCastException.

Can someone please help me? Here is the complete project:

Form1.vb:
VB.NET:
Public Class Form1
    Dim drop As New DropTarget

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.AllowDrop = True
        DDInterfaces.Ole32.RegisterDragDrop(Me.Handle, drop) ' <---- ERROR MESSAGE HERE (InvalidCastException)
    End Sub
End Class

Dragdrop.vb:

VB.NET:
Imports System.Runtime.InteropServices

Namespace DDInterfaces

    Public Enum HResult
        DRAGDROP_S_CANCEL = &H40101
        DRAGDROP_S_DROP = &H40100
        DRAGDROP_S_USEDEFAULTCURSORS = &H40102
        DATA_S_SAMEFORMATETC = &H40130
        S_OK = 0
        S_FALSE = 1
        E_NOINTERFACE = CInt(&H80004002)
        E_NOTIMPL = CInt(&H80004001)
        OLE_E_ADVISENOTSUPPORTED = CInt(80040003)
        MK_E_NOOBJECT = CInt(&H800401E5)
    End Enum

    <ComImport()> _
    <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
    <Guid("00000121-0000-0000-C000-000000000046")> _
    Public Interface IDropSource
        <PreserveSig()> _
        Function QueryContinueDrag(fEscapePressed As Boolean, grfKeyState As Integer) As HResult
        <PreserveSig()> _
        Function GiveFeedback(dwEffect As Integer) As HResult
    End Interface

    <ComImport()> _
    <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
    <Guid("00000122-0000-0000-C000-000000000046")> _
    Public Interface IDropTarget
        Sub DragEnter(pDataObj As IDataObject, grfKeyState As Integer, pt As Point, ByRef pdwEffect As Integer)
        Sub DragOver(grfKeyState As Integer, pt As Point, ByRef pdwEffect As Integer)
        Sub DragLeave()
        Sub Drop(pDataObj As IDataObject, grfKeyState As Integer, pt As Point, ByRef pdwEffect As Integer)
    End Interface

    Public Class Ole32

        <DllImport("ole32.dll")> _
        Public Shared Sub CoTaskMemFree(pv As IntPtr)
        End Sub

        <DllImport("ole32.dll")> _
        Public Shared Function DoDragDrop(pDataObject As ComTypes.IDataObject, pDropSource As IDropSource, dwOKEffect As DragDropEffects, ByRef pdwEffect As DragDropEffects) As Integer
        End Function

        <DllImport("ole32.dll")> _
        Public Shared Function RegisterDragDrop(hwnd As IntPtr, pDropTarget As IDropTarget) As Integer
        End Function

        <DllImport("ole32.dll")> _
        Public Shared Function RevokeDragDrop(hwnd As IntPtr) As Integer
        End Function

    End Class

End Namespace

Public Class DropTarget
    Implements IDropTarget

    Public Sub OnDragDrop(e As System.Windows.Forms.DragEventArgs) Implements System.Windows.Forms.IDropTarget.OnDragDrop
    End Sub

    Public Sub OnDragEnter(e As System.Windows.Forms.DragEventArgs) Implements System.Windows.Forms.IDropTarget.OnDragEnter
    End Sub

    Public Sub OnDragLeave(e As System.EventArgs) Implements System.Windows.Forms.IDropTarget.OnDragLeave
    End Sub

    Public Sub OnDragOver(e As System.Windows.Forms.DragEventArgs) Implements System.Windows.Forms.IDropTarget.OnDragOver
    End Sub

End Class

Thank you in advance.
 
The type System.Windows.Forms.IDropTarget is not the same as the unmanaged type DDInterfaces.IDropTarget.
Windows forms has built in support for drag drop without all that.
 
I have found several examples of using the built in drag drop feature, very simple to use indeed, so I will use the easy way.
Thank you for your help.
 
Back
Top