VC跨进程实现自动托放

//这个版本在64位系统上会造成目标进程闪退。
void test()
{
	char szFile[] = "d:\\jinkexy.mse";
	HWND hWnd = ::FindWindowA("3DSMAX", NULL);
	if (hWnd == NULL) return;

	DWORD dwBufSize = sizeof(DROPFILES) + sizeof(szFile) + 1;
	BYTE *pBuf = NULL;
	LPSTR pszRemote = NULL;
	HANDLE hProcess = NULL;
	__try {
		pBuf = new BYTE[dwBufSize];
		if (pBuf == NULL) __leave;
		memset(pBuf, 0, dwBufSize);
		DROPFILES *pDrop = (DROPFILES *)pBuf;
		pDrop->pFiles = sizeof(DROPFILES);
		strcpy((char *)(pBuf + sizeof(DROPFILES)), szFile);
		DWORD dwProcessId;
		GetWindowThreadProcessId(hWnd, &dwProcessId);
		hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, dwProcessId);
		if (hProcess == NULL) __leave;
		pszRemote = (LPSTR)VirtualAllocEx(hProcess, NULL, dwBufSize, MEM_COMMIT, PAGE_READWRITE);
		if (pszRemote == NULL) __leave;
		if (WriteProcessMemory(hProcess, pszRemote, pBuf, dwBufSize, 0))
			::SendMessage(hWnd, WM_DROPFILES, (WPARAM)pszRemote, NULL);
	}
	__finally {
		if (pBuf != NULL) delete[]pBuf;
		if (pszRemote != NULL) VirtualFreeEx(hProcess, pszRemote, dwBufSize, MEM_FREE);
		if (hProcess != NULL) CloseHandle(hProcess);
	}
}



//这个版本可以正常使用,和操作系统位数无关
/**
* Dia is running, so we simulate a drag & drop event.
* Uses __argv & __argc for list of files to drop.
*/
int CWork::DragAndDropDia(HWND hWnd)
{
	if (hWnd == NULL)
	{
		//char szFile[] = MSE_FILE;
		hWnd = ::FindWindowA("3DSMAX", NULL);
		if (hWnd == NULL)
		{
			AfxMessageBox(_TEXT("请先打开3dsmax窗口"));
			return -1;
		}
		}

		char szFile[MAX_PATH];
		GetModuleFileNameA(NULL, szFile, MAX_PATH);
		PathRemoveFileSpecA(szFile);
		strcat_s(szFile, MAX_PATH, "\\maxscript.mse");

		// int iNumFiles = (gUseRegVal) ? __argc - 1 : __argc - 2;
		int iNumFiles = 1;
		int iCurBytePos = sizeof(DROPFILES);
		LPDROPFILES pDropFiles;
		HGLOBAL hGlobal;
		int i;

		/* May use more memory than is needed... oh well. */
		hGlobal = GlobalAlloc(GHND | GMEM_SHARE,
		sizeof(DROPFILES) +
		(_MAX_PATH * iNumFiles) + 1);

		/* memory failure? */
		if (hGlobal == NULL)
		return -1;

		/* lock the memory */
		pDropFiles = (LPDROPFILES)GlobalLock(hGlobal);

		/* set offset where the file list begins */
		pDropFiles->pFiles = sizeof(DROPFILES);

		/* no wide chars and drop point is in client coordinates */
		pDropFiles->fWide = FALSE;
		pDropFiles->pt.x = pDropFiles->pt.y = 100;
		pDropFiles->fNC = FALSE;

		//for (i = (gUseRegVal) ? 1 : 2; i < __argc; ++i)
		//{
		// strcpy(((LPSTR)(pDropFiles)+iCurBytePos), __argv[i]);
		// /**
		// * Move the current position beyond the file name copied.
		// * +1 for NULL terminator
		// */
		// iCurBytePos += strlen(__argv[i]) + 1;
		//}
		strcpy(((LPSTR)(pDropFiles)+iCurBytePos), szFile);
		iCurBytePos += strlen(szFile) + 1;


		((LPSTR)(pDropFiles))[iCurBytePos] = 0;
		GlobalUnlock(hGlobal);

		/* Force dia to the foreground */
		SetForegroundWindow(hWnd);

	/* restore only if minimized */
	if (IsIconic(hWnd))
	{
		ShowWindow(hWnd, SW_RESTORE);
	}

	//AfxMessageBox(L"准备发送消息");

	/* send the file list */
	::PostMessage(hWnd, WM_DROPFILES, (WPARAM)hGlobal, 0);


	//::GlobalFree(hGlobal);
	return 0;
}

本文为“技术点滴”的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注