15,979
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#define _AFXDLL
#include <afx.h>
#include <Windows.h>
using namespace std;
int main()
{
HANDLE hRead_ReadPipe = NULL, hWrite_ReadPipe = NULL;
HANDLE hRead_WritePipe = NULL, hWrite_WritePipe = NULL;
char CmdPath[] = "C:\\windows\\system32\\cmd.exe";
SECURITY_ATTRIBUTES sa;
sa.bInheritHandle = true;
sa.lpSecurityDescriptor = NULL;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
::CreatePipe(&hRead_ReadPipe, &hWrite_ReadPipe, &sa, 0);
::CreatePipe(&hRead_WritePipe, &hWrite_WritePipe, &sa, 0);
STARTUPINFO si;
::ZeroMemory(&si, sizeof(si));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = hRead_WritePipe;
si.hStdOutput = si.hStdError = hWrite_ReadPipe;
PROCESS_INFORMATION pi;
::CreateProcess(NULL, CmdPath, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
::CloseHandle(pi.hThread);
::CloseHandle(pi.hProcess);
DWORD dwCount = 0;
char szCmd[] = "ipconfig /all\r\n";
WriteFile(hWrite_WritePipe, szCmd, sizeof(szCmd), &dwCount, NULL);
Sleep(2000);
char buffer[2048];
memset(buffer, sizeof(buffer), 0);
ReadFile(hRead_ReadPipe, buffer, sizeof(buffer), &dwCount, NULL);
cout << buffer << endl;
getchar();
return 0;
}
用SendMessage发送WM_CHAR或WM_IME_CHAR消息也可以实现wchar_t wsz[] = L"Hello,我是小明"; for (size_t i=0; i<wcslen(wsz); i++) ::SendMessage(hCmdWnd, WM_CHAR, wsz[i], NULL);
#include <windows.h>
#include <iostream>
using namespace std;
int main( )
{
HWND hCMDwnd=reinterpret_cast<HWND>(0x00050222);
wchar_t wch[]=L"Hello,我是小明";
for(size_t i=0;i<wcslen(wch);i++)
::SendMessage(hCMDwnd,WM_CHAR,wch[i],NULL);
return 0;
}
哪里错了,怎么测试不通过,dos窗口没有字楼主不妨说下你的具体需求,我们好分析要如何实现
还有前面几位提到的SendInput和管道都可以。 SendInput是模拟键盘输入,要求目标程序的黑窗口在最前端并且处于有输入焦点的状态。 SendMeesage方式不要求目标程序的黑窗口在最前端并且处于有输入焦点的状态,目标程序的黑窗口在最小化状态下也可以模拟输入。 用管道的话好处就是可以让目标程序的cmd黑窗口彻底不出现,完全静默的在后台操作。
[quote=引用 1 楼 oyljerry 的回复:] dos窗口用管道方式比较好
dos窗口用管道方式比较好
wchar_t wsz[] = L"Hello,我是小明";
for (size_t i=0; i<wcslen(wsz); i++)
::SendMessage(hCmdWnd, WM_CHAR, wsz[i], NULL);