前提: | 主程序 DWMain 必须先启动。 |
源码: | 安装目录 :/api/windows32_qt5.9.4[vs2015]/dwApiLib2.cpp |
调用: | C_DWIMEAPI_SendCmdA (/命令1/命令2/命令3/..) |
例子: | 参考安装目录: help/Demo-Windows-Cpp-2 工程 |
好处: | 不依赖DLL,不依赖QT环境,32位,64位通用。 |
/********************************************************************************************************** * * 这个 cpp 通过Windows 消息机制来实现命令的传送,这种方便不依赖DLL更加通用。 * **********************************************************************************************************/ // Includes #include <windows.h> #include <stdio.h> bool C_DWIMEAPI_SendCmdA(const char * cmd) { HWND hWnd = FindWindowW(L"Qt5QWindowToolSaveBits", L"DWMain"); if (IsWindow(hWnd)) { COPYDATASTRUCT cds; memset(&cds, 0, sizeof(cds)); cds.dwData = *((int*)"aCmd"); cds.cbData = (DWORD)(strlen(cmd) + 1); cds.lpData = (void*)cmd; DWORD_PTR dwResult = 0; SendMessageTimeout(hWnd, WM_COPYDATA, 0, (LPARAM)&cds, SMTO_ABORTIFHUNG, 200, &dwResult); return (dwResult == 1); } else { MessageBoxW(GetForegroundWindow(), L"多文输入法还没有启动!", L"API接口提示", 0); } return false; } bool C_DWIMEAPI_SendCmdW(const wchar_t * cmd) { HWND hWnd = FindWindowW(L"Qt5QWindowToolSaveBits", L"DWMain"); if (IsWindow(hWnd)) { COPYDATASTRUCT cds; memset(&cds, 0, sizeof(cds)); cds.dwData = *((int*)"wCmd"); cds.cbData = (DWORD)(sizeof(wchar_t) * (wcslen(cmd) + 1)); cds.lpData = (void*)cmd; DWORD_PTR dwResult = 0; SendMessageTimeout(hWnd, WM_COPYDATA, 0, (LPARAM)&cds, SMTO_ABORTIFHUNG, 200, &dwResult); return (dwResult == 1); } else { MessageBoxW(GetForegroundWindow(), L"多文输入法还没有启动!", L"API接口提示", 0); } return false; }