获取带有HWND的IDropTarget抛出0xC0000005访问冲突

时间:2015-01-14 17:24:36

标签: c# c++ drag-and-drop ole hwnd


我尝试使用IDropTarget接口模拟拖放以通过C ++代码(或.NET)删除另一个应用程序中的文件。
我已阅读并测试了不同的解决方案。我有一些代码可以删除一个使用鼠标的文件 但是这段代码应该会引发访问冲突异常 你有解释吗?谢谢。

W7 64Bits,VC6,VS2013 C ++有同样的问题。

stdafx.h
#include <stdio.h>
#include <tchar.h>
#include <oleidl.h>
#include <Atlbase.h>

TestDragDrop.cpp
#include "stdafx.h"
IDropTarget* GetRegisteredDropTargetFromWnd(HWND hWnd)
{
    IUnknown *pBuffer = (IUnknown *)GetProp(hWnd, TEXT("OleDropTargetInterface"));
    if (pBuffer != NULL) // pBuffer = address can see on properties tab with WinSpy++
    {        
        IDropTarget *pRetVal = NULL;
        // throw exception 0xC0000005 acess violation
        if (SUCCEEDED(pBuffer->QueryInterface(IID_IDropTarget, (void **)&pRetVal)))
            return pRetVal;
    }
    return NULL;
}
int main(int argc, char* argv[])
{
    CoInitialize(NULL);
    HWND hWnd = (HWND)0x00181E04;  // get with WinSpy++ 
    IDropTarget* pDT = GetRegisteredDropTargetFromWnd(hWnd);
    CoUninitialize();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

谢谢Hans Passant的理由。我通过在主窗体中添加控件来尝试另一种方式并编写此代码

DataObject daobj = new DataObject();
StringCollection strcol = new StringCollection();
strcol.Add("F:\\test.jpg");
daobj.SetFileDropList(strcol);
IDropTarget dt = (IDropTarget)ctrl;
dt.OnDragDrop(new DragEventArgs(daobj, 0, 0, 0, DragDropEffects.Copy, DragDropEffects.Copy));