强制转换IOleDropTarget pDataObj对象时发生异常-无效的FORMATETC结构(HRESULT中的异常:0x80040064(DV_E_FORMATETC)

时间:2019-02-18 19:12:57

标签: vb.net winapi outlook drag-and-drop

我已经实现了IOleDropTarget接口,还使用了IDropTargetHelper接口来显示要拖动的文件的系统图标。

我的DragEnter代码的一部分看起来像这样

 <ComImport, Guid("0000010E-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
    Interface IDataObject
        <PreserveSig>
        Function GetData(
<[In]> ByRef format As FORMATETC, <Out> ByRef medium As STGMEDIUM) As Integer
        <PreserveSig>
        Function GetDataHere(
<[In]> ByRef format As FORMATETC, ByRef medium As STGMEDIUM) As Integer
        <PreserveSig>
        Function QueryGetData(
<[In]> ByRef format As FORMATETC) As Integer
        <PreserveSig>
        Function GetCanonicalFormatEtc(
<[In]> ByRef formatIn As FORMATETC, <Out> ByRef formatOut As FORMATETC) As Integer
        <PreserveSig>
        Function SetData(
<[In]> ByRef formatIn As FORMATETC,
<[In]> ByRef medium As STGMEDIUM,
<MarshalAs(UnmanagedType.Bool)> ByVal release As Boolean) As Integer
        <PreserveSig>
        Function EnumFormatEtc(ByVal direction As ComTypes.DATADIR, <Out> ByRef ppenumFormatEtc As System.Runtime.InteropServices.ComTypes.IEnumFORMATETC) As Integer
        <PreserveSig>
        Function DAdvise(
<[In]> ByRef pFormatetc As FORMATETC, ByVal advf As ComTypes.ADVF, ByVal adviseSink As ComTypes.IAdviseSink, <Out> ByRef connection As Integer) As Integer
        <PreserveSig>
        Function DUnadvise(ByVal connection As Integer) As Integer
        <PreserveSig>
        Function EnumDAdvise(<Out> ByRef enumAdvise As ComTypes.IEnumSTATDATA) As Integer
    End Interface

将pDataObj强制转换为IDataObject适用于除Outlook电子邮件附件之外我可以拖动的每种对象。电子邮件本身可以正常工作,但不能附件。错误是无效的FORMATETC结构(HRESULT的异常:0x80040064(DV_E_FORMATETC)

我从哪里开始找出我做错了什么?我应该显示什么代码?

我的IDataObject接口看起来像这样

 <StructLayout(LayoutKind.Sequential)>
    Public NotInheritable Class FORMATETC
        Public cfFormat As UShort
        Public dummy As Short
        Public ptd As IntPtr
        Public dwAspect As Integer
        Public lindex As Integer
        Public tymed As Integer
    End Class

和这样的FORMATETC结构

Private ddHelper As IDropTargetHelper = CType(New DragDropHelper(), IDropTargetHelper)

已更新 ddHelper是从IDropTargetHelper接口强制转换的类的实例。

<StructLayout(LayoutKind.Sequential)>
Public Structure Win32Point
    Public x As Integer
    Public y As Integer
End Structure

<ComImport>
<Guid("4657278A-411B-11d2-839A-00C04FD918D0")>
Public Class DragDropHelper
End Class

<ComVisible(True)>
<ComImport>
<Guid("4657278B-411B-11D2-839A-00C04FD918D0")>
<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Interface IDropTargetHelper
    Sub DragEnter(
<[In]> ByVal hwndTarget As IntPtr,
<[In], MarshalAs(UnmanagedType.[Interface])> ByVal dataObject As NativeMethods.IDataObject,
<[In]> ByRef pt As Win32Point,
<[In]> ByVal effect As Integer)
    Sub DragLeave()
    Sub DragOver(
<[In]> ByRef pt As Win32Point,
<[In]> ByVal effect As Integer)
    Sub Drop(
<[In], MarshalAs(UnmanagedType.[Interface])> ByVal dataObject As NativeMethods.IDataObject,
<[In]> ByRef pt As Win32Point,
<[In]> ByVal effect As Integer)
    Sub Show(
<[In]> ByVal show As Boolean)
End Interface

类和接口如下

<TextBlock Text="{Binding Converter={StaticResource EntityIDToStrConverter},UpdateSourceTrigger=}" Margin="6" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>

错误看起来像这样。 enter image description here

0 个答案:

没有答案