I tried to send WM_DROPFILES message to all of the handles around the cef browser, which are detected from spy++.All failed.
The code as follows works if the target window is the main window of Notepad.exe. But if I change the targetHwnd to any window handle of my cef browser, it fails, the OnDragEnter never be called.
If I drag a file to this browser manually, the OnDragEnter get called.
My question is ,which window should I SendMessage to? Or maybe some configuration of the cef browser is missing.
Forgive me for bad english expressions, Thank you.
- Code: Select all
#include <ShlObj.h>
#include <atlstr.h>
BOOL SimulateDropFile(CString strFilePath, HWND targetHwnd)
{
char szFile[MAX_PATH] = { 0 };
wcstombs(szFile, strFilePath.GetBuffer(0), _MAX_PATH);
DWORD dwBufSize = sizeof(DROPFILES) + strlen(szFile) + 1;
BYTE* pBuf = new BYTE[dwBufSize];
if (pBuf == NULL)
return FALSE;
BOOL bResult = FALSE;
memset(pBuf, 0, dwBufSize);
DROPFILES* pDrop = (DROPFILES*)pBuf;
pDrop->pFiles = sizeof(DROPFILES);
strcpy((char*)(pBuf + sizeof(DROPFILES)), szFile);
DWORD dwProcessId = 0;
GetWindowThreadProcessId(targetHwnd, &dwProcessId);
if (dwProcessId != NULL)
{
HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, dwProcessId);
if (hProcess != NULL)
{
LPSTR pszRemote = (LPSTR)VirtualAllocEx(hProcess, NULL, dwBufSize, MEM_COMMIT, PAGE_READWRITE);
if (pszRemote && WriteProcessMemory(hProcess, pszRemote, pBuf, dwBufSize, 0))
{
::SendMessage(targetHwnd, WM_DROPFILES, (WPARAM)pszRemote, NULL);
bResult = TRUE;
}
}
}
if (pBuf)
{
delete[] pBuf;
pBuf = NULL;
}
return bResult;
}