#include <ShlObj.h>
BOOL SimulateDropFile(CString strFilePath)
{
char szFile[MAX_PATH] = {};
wcstombs(szFile, strFilePath.GetBuffer(), _MAX_PATH);
DWORD dwBufSize = sizeof(DROPFILES) + strlen(szFile) + ;
//通过类名或窗口标题 找到接受拖拽的窗口
HWND hMain = ::FindWindow(NULL, _T("XX播放器"));
if (hMain == NULL)
return FALSE;
BYTE* pBuf = new BYTE[dwBufSize];
if (pBuf == NULL)
return FALSE;
BOOL bResult = FALSE;
memset(pBuf, , dwBufSize);
DROPFILES* pDrop = (DROPFILES*)pBuf;
pDrop->pFiles = sizeof(DROPFILES);
strcpy((char*)(pBuf + sizeof(DROPFILES)), szFile);
DWORD dwProcessId = ;
GetWindowThreadProcessId(hMain, &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, ))
{
::SendMessage(hMain, WM_DROPFILES, (WPARAM)pszRemote, NULL);
bResult = TRUE;
}
}
}
if (pBuf)
{
delete[] pBuf;
pBuf = NULL;
}
return bResult;
}
在调用该函数之前,先打开接受拖拽的进程
ShellExecute(NULL, L"open", exe完整路径, NULL, NULL, SW_SHOWNORMAL);