需求:本人有一个针对某软件的注册机工具,每次使用时都需要打开这个程序,输入相应的用户码点击生成才能获得激活码,现在我希望能批量根据用户码生成激活码,本文主要介绍在此背景下如何完成自己的扩展工具。
代码说话:
void MySetWindowText(HWND hwnd,char* Data) { int len=StrLen(Data); for(int i=0;i<len;i++) { USHORT wParam = *(USHORT *)(Data+i); SendMessage(hwnd,WM_CHAR,wParam,1); } } void GetCode(HWND handle) { PROCESS_INFORMATION pinfo={0}; STARTUPINFO sinfo={0}; sinfo.cb=sizeof sinfo; // String strFileName=ExtractFileDir(Application->ExeName)+"\\key.exe"; if(CreateProcessA(strFileName.c_str(),NULL,NULL,NULL,FALSE,0,NULL,NULL,&sinfo,&pinfo)) { WaitForSingleObject(pinfo.hProcess,1000); } SetForegroundWindow(handle); // HWND hwnd=FindWindowA(NULL,"key tool"); if(hwnd==NULL) { ShowMessage("程序未启动"); return; } //设置选中某产品类型 HWND combobox=FindWindowEx(hwnd,NULL,"ComboBox",NULL); if(combobox!=NULL) { SendMessage(combobox, CB_SETCURSEL, 3, 0); } HWND child=FindWindowEx(hwnd,NULL,"Edit",NULL); if(child!=NULL) { char Data[50]="123456"; MySetWindowText(child,Data); HWND child2=FindWindowExA(hwnd,child,"Edit",NULL); if(child2!=NULL) { MySetWindowText(child2,"654321"); HWND generate=FindWindowExA(hwnd,NULL,"Button","&Generate"); if(generate!=NULL) { int ctrlid=GetDlgCtrlID(generate); SendMessage(hwnd,WM_COMMAND,MAKEWPARAM(ctrlid, BN_CLICKED),NULL); } HWND regcode1=FindWindowExA(hwnd,child2,"Edit",NULL); if(regcode1!=NULL) { char szTemp[MAX_PATH]=""; SendMessageA(regcode1,WM_GETTEXT,MAX_PATH,(LPARAM)(void*)szTemp); ShowMessage(szTemp); HWND regcode2=FindWindowExA(hwnd,regcode1,"Edit",NULL); if(regcode2!=NULL) { memset(szTemp,0,MAX_PATH); SendMessageA(regcode2,WM_GETTEXT,MAX_PATH,(LPARAM)(void*)szTemp); ShowMessage(szTemp); } } HWND exit=FindWindowExA(hwnd,NULL,"Button","&Exit"); if(exit!=NULL) { int ctrlid=GetDlgCtrlID(exit); SendMessage(hwnd,WM_COMMAND,MAKEWPARAM(ctrlid, BN_CLICKED),NULL); } } } }
流程:
1、启动目标程序;
2、等待目标程序窗口创建完成
3、根据窗口类名查找窗口句柄,根据父窗口句柄查找目标子窗口句柄,设置选择索引或设置文本或发送点击消息或获取文本。
时间: 2024-10-22 13:28:16