如何将被调用*.exe程序的数据复制过来(调过来)?(50分)

  • 主题发起人 主题发起人 江上游者
  • 开始时间 开始时间

江上游者

Unregistered / Unconfirmed
GUEST, unregistred user!
现在已经找到需要的主窗体句柄,主窗体中有edit或者窗体中的其它编辑框框,需要将窗体<br>中的数据复制或者调过来,使用什么方法。
 
// 试试下面代码, 使用 EnumWindows与EnumChildWindows ,<br>// 想找指定内容可以在里面设置过滤条件,返回值为 = False 时停止枚举窗口<br><br>var<br> &nbsp;RootNode: &nbsp; TTreeNode; &nbsp; &nbsp; &nbsp; // 第一层节点对象 - 每个顶层窗口都有(除了排除列表)<br> &nbsp;ChildNodes: array [0..30] of TTreeNode; &nbsp;// 预设最深层=31<br> &nbsp;TreeView: &nbsp; TTreeView; &nbsp; &nbsp; &nbsp; // 窗口树对象<br> &nbsp;CengChi: &nbsp; &nbsp;integer; &nbsp; &nbsp; &nbsp; &nbsp; // 当前层次<br><br> &nbsp;// Class Name , Text Buffer<br> &nbsp;NoList: array [0..11,0..1] of string =<br> &nbsp; &nbsp;(('IME','Default IME'),('MSCTFIME UI','M'),('TfrmKBSetValue','KBSetValue'),<br> &nbsp; &nbsp; ('SystemTray_Main','电表'),('SysFader','SysFader'), ('BCWDBKSYNCHWND','DBK'),<br> &nbsp; &nbsp; ('MS_WebcheckMonitor','MS_WebcheckMonitor'),('OleDdeWndClass','DDE Server Window'),<br> &nbsp; &nbsp; ('#43','MCI command handling window'),('ATI video bios poller','ATI video bios poller'),<br> &nbsp; &nbsp; ('Progman','Program Manager'),('NDDEAgnt','NetDDE Agent')<br> &nbsp; &nbsp;);<br><br>function IsInNoList(const psClassName,psBufferText: string): boolean;<br>var<br> &nbsp;i: integer;<br>begin<br> &nbsp;// 字串是否在列表中<br> &nbsp;Result := False;<br> &nbsp;for i := Low(NoList) to High(NoList) do<br> &nbsp; &nbsp;if SameText(NoList[i,0],psClassName) and<br> &nbsp; &nbsp; &nbsp; SameText(NoList[i,1],psBufferText) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp; &nbsp;Break;<br> &nbsp; &nbsp;end;<br>end;<br><br>function GetClassText(HWND: HWND): string;<br>var<br> &nbsp;Text: PChar;<br>begin<br> &nbsp;GetMem(Text, 144);<br> &nbsp;GetClassName(HWnd, Text, 144);<br> &nbsp;Result := Text;<br> &nbsp;freemem(Text);<br>end;<br><br>function GetText(HWND: HWND): string;<br>var<br> &nbsp;TextLength: integer;<br> &nbsp;Text: &nbsp; &nbsp; &nbsp; PChar;<br>begin<br> &nbsp;TextLength := SendMessage(HWND, WM_GETTEXTLENGTH, 0, 0);<br> &nbsp;if TextLength = 0 then<br> &nbsp; &nbsp;Result := ''<br> &nbsp;else<br> &nbsp;begin<br> &nbsp; &nbsp;getmem(Text, TextLength + 1);<br> &nbsp; &nbsp;SendMessage(HWND, WM_GetTEXT, TextLength + 1, integer(Text));<br> &nbsp; &nbsp;Result := Text;<br> &nbsp; &nbsp;freemem(Text);<br> &nbsp;end;<br>end;<br><br>function EnumChildWindowProc(HWND: HWND; lParam: LPARAM): BOOL; stdcall;<br>var<br> &nbsp;S, S1: string;<br> &nbsp;Node: &nbsp;TTreeNode;<br>begin<br> &nbsp;// 枚举子窗口回调函数<br> &nbsp;Result := True;<br> &nbsp;S &nbsp; &nbsp; &nbsp;:= GetText(HWND);<br> &nbsp;S1 &nbsp; &nbsp; := GetClassText(HWND);<br> &nbsp;Node &nbsp; := TreeView.Items.AddChild(ChildNodes[CengChi], 'H:(' + <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IntToHex(HWND, 8) +<br> &nbsp; &nbsp;')' + S + '(' + S1 + ')');<br> &nbsp;Node.ImageIndex := -1;<br> &nbsp;Node.StateIndex := HWND;<br> &nbsp;Inc(CengChi);<br> &nbsp;ChildNodes[CengChi] := Node;<br> &nbsp;EnumChildWindows(HWND, @EnumChildWindowProc, lParam); &nbsp; // 递归枚举子窗口<br> &nbsp;Dec(CengChi);<br>end;<br><br>function EnumAllWindow(HWND: HWND; lParam: LPARAM): BOOL; stdcall;<br>var<br> &nbsp;S, S1: string;<br>begin<br> &nbsp;// 枚举所有顶层窗口<br> &nbsp;Result := True;<br> &nbsp;S &nbsp; &nbsp; &nbsp;:= GetText(HWND);<br> &nbsp;S1 &nbsp; &nbsp; := GetClassText(HWND);<br> &nbsp;if (S &lt;&gt; '') and<br> &nbsp; ( ((lParam=-1) and (Not IsInNoList(S1,S))) or<br> &nbsp; &nbsp; ((lParam=0) and (Not SameText(S1,'TfrmKBSetValue')) ))<br> &nbsp; &nbsp; then // 排除定义列表<br> &nbsp;begin<br> &nbsp; &nbsp;RootNode := TreeView.Items.AddChild(nil, 'H:(' + IntToHex(HWND, 8) + ')' + S + '(' + S1 + ')');<br> &nbsp; &nbsp;RootNode.ImageIndex := -1;<br> &nbsp; &nbsp;RootNode.StateIndex := HWND;<br> &nbsp; &nbsp;CengChi &nbsp;:= 0;<br> &nbsp; &nbsp;ChildNodes[CengChi] := RootNode;<br> &nbsp; &nbsp;EnumChildWindows(HWND, @EnumChildWindowProc, lParam); &nbsp;// 枚举所有子窗口<br> &nbsp;end;<br>end;<br><br>// 应用<br>procedure TfrmKBSetValue.mmRefreshAllClick(Sender: TObject);<br>begin<br> &nbsp;// 枚举所有窗口以及窗口的控件<br> &nbsp;<br> &nbsp;Tree.Items.BeginUpdate;<br> &nbsp;Screen.Cursor := crHourGlass;<br> &nbsp;try<br> &nbsp; &nbsp;Tree.Items.Clear;<br> &nbsp; &nbsp;EnumWindows(@EnumAllWindow, StrToInt(BoolToStr(chkFilterSys.Checked)));<br> &nbsp; &nbsp;chkFilterSys.Caption := '过滤系统窗口 &nbsp;' + IntToStr(Tree.Items.Count);<br> &nbsp;finally<br> &nbsp; &nbsp;Screen.Cursor := crDefault ;<br> &nbsp; &nbsp;Tree.Items.EndUpdate;<br> &nbsp;end;<br>end;<br><br>procedure TfrmKBSetValue.TreeChange(Sender: TObject; Node: TTreeNode);<br>var<br> &nbsp;H: HWND;<br>begin<br> &nbsp;if Tree.Selected = nil then<br> &nbsp; &nbsp;Exit;<br> &nbsp;H := Node.StateIndex;<br> &nbsp;ClassEdit.Text := GetClassText(H); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 类名<br> &nbsp;TextEdit.Text := GetText(H); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 文本<br>end;<br><br><br>// ---- 按类名与出现次数查找窗口句柄<br>var<br> &nbsp;EnumWindowComClass: string;<br> &nbsp;EndmWIndowComHwnd: &nbsp;HWND;<br> &nbsp;ChildIndex, ChildINC :integer;<br><br>function EnumChildProc_Class(HWND: HWND; lParam: LPARAM): BOOL; stdcall;<br>var<br> &nbsp;S: string;<br>begin<br> &nbsp;Result := True;<br> &nbsp;S &nbsp; &nbsp; &nbsp;:= GetClassText(HWND);<br> &nbsp;if Uppercase(S) = EnumWindowComClass then<br> &nbsp;begin<br> &nbsp; &nbsp;if ChildInc = ChildIndex then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;EndmWIndowComHwnd := HWND;<br> &nbsp; &nbsp; &nbsp;Result := False;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;Inc(ChildInc);<br> &nbsp;end<br> &nbsp;else<br> &nbsp;begin<br> &nbsp; &nbsp;// 递归查找子窗口,要在同层查找则不要递归调用。<br> &nbsp; &nbsp;EnumChildWindows(HWND, @EnumChildProc_Class, 0); <br> &nbsp;end;<br>end; &nbsp;// Local<br><br>function FindWindowClass(FormHWND: HWND; ComClass: string; Index: integer): HWND;<br>begin<br> &nbsp;ChildIndex := Index; &nbsp; &nbsp; // 取第Index次出现的次数<br> &nbsp;ChildINC := 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 找到的次数<br> &nbsp;EndmWIndowComHwnd := 0; &nbsp;// 找到的句柄<br> &nbsp;EnumWindowComClass := Uppercase(ComClass); // 查找类名<br> &nbsp;EnumChildWindows(FormHWND, @EnumChildProc_Class, 0);<br> &nbsp;Result := EndmWIndowComHwnd;<br>end;
 
关键是把数据从已知的*.exe窗体中复制过来,不是查找的句柄问题。
 
(我的程序)SendMessage--&gt;Edit(*.exe)....已经可以运行<br>(*.exe)Edit--&gt;'XXX'(如何到我的程序).....使用什么方法
 
楼主需要注意一个问题,你是在读取其他程序窗体上的信息,而这个信息,只能通过你自己的程序主动读,而不是让别人的软件给你发.逻辑先搞清楚.实现这个功能是在你的软件上.其实上面的给的代码可以实现了.
 
没有liqj_,老兄的这么麻烦吧,定义一个自定义的消息,发送sendmessage(Handle,wpar,lpar)就可以啦,最好能知道调用*.exe要取的对应的控件的名称就好啦
 
在*.exe的程序中的控件句柄可以找,但使用什么方法使*.exe的程序中Edit的内容返回到程序中。
 
自己定义i的值,直到找出对应的控件:<br>function GetLocalTextInfoTest(var ReturnEditHandle:Integer):String;<br>var AHandle, BHandle: Integer;<br> &nbsp; &nbsp;R:Boolean;<br> &nbsp; &nbsp;mbuffer: array[0..255] of Char;<br> &nbsp; &nbsp;ss:string; &nbsp;<br>{$WRITEABLECONST ON}<br> &nbsp;function GetComponentHandle(hwnd: Integer; lparam: Longint):Boolean; stdcall;<br> &nbsp;var s,sssss:string;<br> &nbsp; &nbsp; &nbsp;i,j:integer;<br> &nbsp; &nbsp; &nbsp;buffer: array[0..255] of Char; <br> &nbsp;const<br> &nbsp; &nbsp; &nbsp;somedata: Integer = 0;<br> &nbsp;begin<br> &nbsp; &nbsp;j:=0;i:=22;<br> &nbsp; &nbsp;if DebugState then sssss:=Uppercase('.SCROLLBAR.')//'TEDIT'<br> &nbsp; &nbsp;else sssss:='.EDIT.';<br> &nbsp; &nbsp;Result := True;<br> &nbsp; &nbsp;//得到目标窗口的控件<br> &nbsp; &nbsp;j:=GetClassName(hwnd, buffer, 256);<br> &nbsp; &nbsp;//找到目标窗口的TButton类目标控件<br> &nbsp; &nbsp;s:=StrPas(Buffer);<br> &nbsp; &nbsp;if Pos(sssss,uppercase(s))&gt;0 then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if DebugState then begin<br> &nbsp; &nbsp; &nbsp; &nbsp;inc(somedata);<br> &nbsp; &nbsp; &nbsp; &nbsp;if somedata=i then begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;somedata:=0;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result:=False; &nbsp;//终止循环<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end else begin<br> &nbsp; &nbsp; &nbsp; &nbsp;PInteger(lparam)^ := hwnd; //得到目标控件的Hwnd(句柄)<br> &nbsp; &nbsp; &nbsp; &nbsp;Result:=False; &nbsp;//终止循环<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;end; &nbsp; <br>{$WRITEABLECONST OFF}<br>begin<br> &nbsp;//取句柄<br> &nbsp;AHandle := FindWindow(nil, 'MasterLabelPrint'); &nbsp;//就是窗口的Caption<br> &nbsp;//AHandle := FindWindow(nil, 'G/BOX组装防错系统'); &nbsp;//就是窗口的Caption<br> &nbsp;if AHandle&lt;&gt;0 then<br> &nbsp;begin<br> &nbsp; &nbsp;//在这里循环取到想要的句柄为止<br> &nbsp; &nbsp;R:=EnumChildWindows(AHandle, @GetComponentHandle, Integer(@BHandle));<br> &nbsp; &nbsp;if not R then begin<br> &nbsp; &nbsp; &nbsp;ReturnEditHandle:=BHandle;<br> &nbsp; &nbsp; &nbsp;SendMessage(BHandle,WM_GETTEXT,254,Integer(@mbuffer));<br> &nbsp; &nbsp; &nbsp;//GetWindowText(BHandle, mbuffer, 100);<br> &nbsp; &nbsp; &nbsp;ss:=StrPas(mBuffer);<br> &nbsp; &nbsp; &nbsp;Result:=ss;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;//此时,BHandle就是你要的句柄<br> &nbsp; &nbsp;//PostMessage(BHandle, BM_Click, 0, 0); //向这个按钮发一个点击消息<br> &nbsp;end;<br>end;
 
句柄已经找到,但是得不到窗体上中的数据,是不是需要使用窗体取词的方法。
 
var <br> &nbsp;Str: array[0..255] of Char;<br>GetWindowText(Handle, @Str, Length(Str));
 
晕,150元,我来做<br>QQ 87797301
 
zlkxzy<br>你说说,这个问题需要使用什么方法解决,能够解决到什么程度。
 
我也碰到同类的问题,就是得到另外一个程序的主窗口的句柄,又得到主窗口的子窗口的句柄,例如得到的是stringgrid的句柄,问题在于怎么才能得到stringgrid的数据呢?如有人解决这个问题,我另开一个提问给500分。
 
简单:<br>var<br> &nbsp;hWindow,hEdit:THandle;<br> &nbsp;str:array [0..225] of char;<br> &nbsp;len1:integer;<br>begin<br> &nbsp;hWindow:=findwindow(nil,'你的窗口名);<br> &nbsp;if hWindow&gt;0 then<br> &nbsp;begin<br> &nbsp; &nbsp;hEdit:=findwindowex(hWindow,0,'Edit','');//edit为编辑框的类<br> &nbsp; &nbsp;len1:=sendmessage(hEdit,wm_gettextlength,0,0);<br> &nbsp; &nbsp;sendmessage(hEdit,wm_gettext,len1+1,integer(@str));<br> &nbsp; &nbsp;showmessage(str); //str为编辑框字符值<br>end
 
测试了下楼上的代码,通过<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;hWindow,hEdit:THandle;<br> &nbsp;Str:array [0..6539] of char; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//要取的字符串的长度<br> &nbsp;len1:integer;<br>begin<br> &nbsp;hWindow:=Findwindow(nil,'无标题 - 记事本'); &nbsp; &nbsp;//查找主窗体句柄<br> &nbsp;if hWindow &lt;&gt;0 then<br> &nbsp;begin<br> &nbsp; &nbsp;hEdit:=FindwindowEx(hWindow,0,'Edit',nil); &nbsp; &nbsp; &nbsp; //Edit为编辑框的类<br> &nbsp; &nbsp;Len1:=SendMessage(hEdit,wm_gettextlength,0,0);<br> &nbsp; &nbsp;SendMessage(hEdit,wm_gettext,Len1+1,Integer(@Str));<br> &nbsp; &nbsp;Memo1.Lines.Add(str);<br> &nbsp;end;<br>end;<br><br>主窗口标题、类名事先使用Spy4Win查看
 
后退
顶部