这里仅仅是以putty作为演示消息发送机制和控件搜索机制
程序一:代填IP和端口,并建立远程连接
#include"stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
HWND FindTextBox(HWND hWnd,DWORD select_edit=1)
{
HWND hEdit = NULL;
hEdit = FindWindowExW(hWnd, hEdit, L"Edit", 0);
switch (select_edit)
{
case 1:
return hEdit;
case 2:
hEdit=GetNextWindow(hEdit,2);
hEdit=GetNextWindow(hEdit,2);
return hEdit;
default:
break;
}
/*遍历子窗体
if (hEdit != NULL) return hEdit;
HWND hChild = NULL;
while(true) {
hChild = FindWindowExW(hWnd, hChild, 0, 0);
if (hChild == NULL)
break;
hEdit = FindTextBox(hChild);
if (hEdit != NULL)
break;
}
*/
return hEdit;
}
HWND FindButton(HWND hWnd,DWORD select_button=0){
HWND hButton=NULL;
hButton=FindWindowExW(hWnd,hButton,L"Button",0);
return hButton;
}
int _tmain (int argc, LPTSTR argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess("C:\Program Files\sunsi\third\putty\putty.exe", // No module name (use command line)
NULL, // Command line //简单的方法是调用putty传递参数,把NULL改为"-ssh -l root -pw 123456 -P 22 1.1.1.1"即可
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).
", GetLastError() );
return 1;
}
// Wait until child process exits.
// WaitForSingleObject( pi.hProcess, INFINITE ); //之前忘记注释这一行,导致失败,丢大发了。。。
Sleep(2000);//等待窗体打开
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
puts("-------------------------------");
HWND hWnd=FindWindowW(0,L"PuTTY Configuration");
if(hWnd==0){
puts("search failed!");
system("pause");
return 1;
}
HWND hEditIp=NULL;
HWND hEditPort=NULL;
HWND hButton=NULL;
hEditIp=FindTextBox(hWnd,1);
hEditPort=FindTextBox(hWnd,2);
hButton=FindButton(hWnd);
if(hEditIp!=NULL&&hEditPort!=NULL){
SendMessageW(hEditIp,WM_SETTEXT,0,(LPARAM)L"172.18.4.202");
SendMessageW(hEditPort,WM_SETTEXT,0,(LPARAM)L"422");
}else{
puts("search edit frame failed!");
system("pause");
return 1;
}
if(hButton!=NULL){
SendMessageW(hButton,WM_LBUTTONDOWN,0,0);
SendMessageW(hButton,WM_LBUTTONUP,0,0);
system("pause");
}else{
puts("search button failed!");
system("pause");
return 1;
}
//system("pause");
return 0;
}
程序二:获取文本框内容
#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HWND hText;
HWND hWnd=::FindWindowW(NULL,L"PuTTY Configuration");
char ch[100];//用于接收获取到的文本框的内容
if(hWnd==NULL)
cout<<"error"<<endl;
else{
hText=GetWindow(hWnd,GW_CHILD); //获取子窗口句柄
hText=GetWindow(hText,GW_HWNDFIRST);//该句可不加
while(hText)
{
//cout<<GetWindowLong(hText,GWL_STYLE)<<endl; //获取文本框的样式,为下面判断做准备,可通过spy++查看,但spy出来的是十六进制,需要转换。
if(1342242944==GetWindowLong(hText,GWL_STYLE)){
SendMessage(hText,WM_GETTEXT,100,(LPARAM)ch);//获取文本内容,存到ch中
cout<<ch<<endl;
}
hText=GetWindow(hText,GW_HWNDNEXT);
}
}
system("pause");
}