Windows獲取其他進程中Edit控件的內容


  最近做的MFC項目中,有個獲取其他進程中Edit控件內容的需求,本來以為是個很簡單的問題,但是來來回回折騰了不少時間,發博記錄一下。

  剛開始拿到這個問題,很自然的就想到GetDlgItemText():

UINT GetDlgItemText(
  HWND hDlg,       // handle to dialog box
  int nIDDlgItem,  // control identifier
  LPTSTR lpString, // pointer to buffer for text
  int nMaxCount    // maximum size of string
);

The GetDlgItemText function retrieves the title or text associated with a control in a dialog box. 

  看API的描述和要實現的需求一樣,自然就用起來了,可發現不管怎樣這個API始終調用失敗,獲取不到內容。一直以為是代碼哪里出問題了,調試好久。后來Google之后在CSDN論壇上找到個老帖,得知這個API只有在Windows 2K之前的系統才能跨進程使用,無奈放棄。

 

  於是查了下,還有一個GetWindowText()

int GetWindowText(
  HWND hWnd,        // handle to window or control
  LPTSTR lpString,  // text buffer
  int nMaxCount     // maximum number of characters to copy
);

The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied. However, GetWindowText cannot retrieve the text of a control in another application.


  描述里面很清楚寫着不能對其他程序使用,又一次失敗。

  PS:吐槽一下,為什么GetDlgItemText()里面不寫這句話,浪費俺的時間。╮(╯▽╰)╭

 

解決方法:使用SendMessage()向進程發WM_GETTEXT消息獲取。

  SendMessage(handle,message,Wparam,lparam);

    Handle為窗口句柄,

    message為消息類型,

    wparam和lparam為消息參數;

  WM_GETTEXT
    An application sends a WM_GETTEXT message to copy the text that corresponds to a window into a buffer provided by the caller.

 

其實使用這個消息等於用GetwindowText()。。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM