c++ 发送消息,模拟拖拽文件

 1 #include <ShlObj.h>
 2 BOOL SimulateDropFile(CString strFilePath)
 3 {
 4     char szFile[MAX_PATH] = {0};
 5     wcstombs(szFile, strFilePath.GetBuffer(0), _MAX_PATH);
 6     DWORD dwBufSize = sizeof(DROPFILES) + strlen(szFile) + 1;
 7     
 8     //通过类名或窗口标题 找到接受拖拽的窗口
 9     HWND hMain = ::FindWindow(NULL, _T("XX播放器"));
10     if (hMain == NULL)
11         return FALSE;
12     BYTE* pBuf = new BYTE[dwBufSize];
13     if (pBuf == NULL)
14         return FALSE;
15 
16     BOOL bResult = FALSE;
17     memset(pBuf, 0, dwBufSize);
18     DROPFILES* pDrop = (DROPFILES*)pBuf;
19     pDrop->pFiles = sizeof(DROPFILES);
20     strcpy((char*)(pBuf + sizeof(DROPFILES)), szFile);
21 
22     DWORD dwProcessId = 0;
23     GetWindowThreadProcessId(hMain, &dwProcessId);
24     if (dwProcessId != NULL)
25     {
26         HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, dwProcessId);
27         if (hProcess != NULL)
28         {
29             LPSTR pszRemote = (LPSTR)VirtualAllocEx(hProcess, NULL, dwBufSize, MEM_COMMIT, PAGE_READWRITE);
30             if (pszRemote && WriteProcessMemory(hProcess, pszRemote, pBuf, dwBufSize, 0))
31             {
32                 ::SendMessage(hMain, WM_DROPFILES, (WPARAM)pszRemote, NULL);
33                 bResult = TRUE;
34             }
35         }
36     }
37 
38     if (pBuf)
39     {
40         delete[] pBuf;
41         pBuf = NULL;
42     }
43     return bResult;
44 }

在调用该函数之前,先打开接受拖拽的进程
ShellExecute(NULL, L"open", exe完整路径, NULL, NULL, SW_SHOWNORMAL);
原文地址:https://www.cnblogs.com/pjl1119/p/7509388.html