Simulate drag & drop file to cef browser failed.

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Simulate drag & drop file to cef browser failed.

Postby zgshen » Sat Mar 21, 2020 11:01 am

I want to drop a file to a cef browser automatically by another process.
I tried to send WM_DROPFILES message to all of the handles around the cef browser, which are detected from spy++.All failed.
20200321233812.png
20200321233812.png (5.94 KiB) Viewed 2611 times

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;
}
zgshen
Techie
 
Posts: 25
Joined: Sun Jun 04, 2017 9:35 pm

Return to Support Forum

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 23 guests