I'm trying to simulate Drag and Drop to drop a file in another application by C++ code (or .NET) with IDropTarget interface.
I have read and tested different solutions. I have a little code to drop a file that work with the mouse.
But this code should work throws an access violation exception.
Do you have an explanation? Thank you.
W7 64Bits, VC6, VS2013 C++ with same problem.
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;
}
Thank you Hans Passant for good reason. I m try another way by adding control in main form and write this code
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));