c# 獲得QQ聊天輸入框中的內容(原創:半水之劍)

來源:互聯網
上載者:User

using System.Runtime.InteropServices;<br />[DllImport("User32.DLL")]<br />public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);<br />public delegate bool WNDENUMPROC(IntPtr hwnd, int lParam);<br />[DllImport("user32.dll")]<br />public static extern int EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);<br />[DllImport("user32.dll")]<br />public static extern int EnumChildWindows(IntPtr hWndParent,<br /> WNDENUMPROC lpEnumFunc, int lParam);<br />[DllImport("user32.dll")]<br />public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString,<br /> int nMaxCount);<br />[DllImport("user32.dll")]<br />public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName,<br /> int nMaxCount);<br />[DllImport("user32.dll")]<br />public static extern bool IsWindow(IntPtr hWnd);<br />[DllImport("user32.dll")]<br />public static extern bool IsWindowVisible(IntPtr hWnd);<br />[DllImport("user32.DLL")]<br />public static extern IntPtr FindWindowEx(IntPtr hwndParent,<br /> IntPtr hwndChildAfter, string lpszClass, string lpszWindow);<br />[DllImport("user32.dll")]<br />public static extern uint GetWindowThreadProcessId(IntPtr hWnd,<br /> out uint dwProcessId);<br />[DllImport("psapi.dll")]<br />public static extern uint GetModuleBaseName(IntPtr hProcess, IntPtr hModule,<br /> StringBuilder lpBaseName, uint nSize);<br />public const uint PROCESS_VM_OPERATION = 0x0008;<br />public const uint PROCESS_VM_READ = 0x0010;<br />public const uint PROCESS_VM_WRITE = 0x0020;<br />public const uint PROCESS_QUERY_INFORMATION = 0x0400;<br />[DllImport("kernel32.dll")]<br />public static extern IntPtr OpenProcess(uint dwDesiredAccess,<br /> bool bInheritHandle, uint dwProcessId);<br />[DllImport("kernel32.dll")]<br />public static extern bool CloseHandle(IntPtr handle);<br />[DllImport("user32.dll")]<br />public static extern int GetWindowLong(IntPtr hWnd, int nIndex);<br />public const int GWL_STYLE = -16;<br />public const int ES_READONLY = 0x800;<br />public const uint MEM_COMMIT = 0x1000;<br />public const uint MEM_RELEASE = 0x8000;<br />public const uint MEM_RESERVE = 0x2000;<br />public const uint PAGE_READWRITE = 4;<br />[DllImport("kernel32.dll")]<br />public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress,<br /> uint dwSize, uint flAllocationType, uint flProtect);<br />[DllImport("kernel32.dll")]<br />public static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress,<br /> uint dwSize, uint dwFreeType);<br />[DllImport("kernel32.dll")]<br />public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,<br /> IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);<br />[DllImport("kernel32.dll")]<br />public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,<br /> IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead);<br />private IntPtr richHandle;<br />public string GetProcessName(uint AProcessId)<br />{<br /> StringBuilder vBuffer = new StringBuilder(256);<br /> IntPtr vProcess = OpenProcess(<br /> PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, AProcessId);<br /> try<br /> {<br /> if (GetModuleBaseName(vProcess, IntPtr.Zero, vBuffer,<br /> (uint)vBuffer.Capacity) > 0)<br /> return vBuffer.ToString();<br /> else return string.Empty;<br /> }<br /> finally<br /> {<br /> CloseHandle(vProcess);<br /> }<br />}<br />public bool EnumChild(IntPtr hwnd, int lParam)<br />{<br /> if (!IsWindowVisible(hwnd)) return true; // 不可見<br /> StringBuilder vBuffer = new StringBuilder(256);<br /> GetClassName(hwnd, vBuffer, vBuffer.Capacity);<br /> if (vBuffer.ToString().ToLower() == "richedit20a")<br /> {<br /> if ((GetWindowLong(hwnd, GWL_STYLE) & ES_READONLY) != ES_READONLY) // 非唯讀<br /> {<br /> richHandle = hwnd;<br /> return false;<br /> }<br /> }<br /> return true;<br />}<br />public bool EnumFunc(IntPtr hwnd, int lParam)<br />{<br /> if (!IsWindowVisible(hwnd)) return true; // 不可見<br /> StringBuilder vBuffer = new StringBuilder(256);<br /> GetClassName(hwnd, vBuffer, vBuffer.Capacity);<br /> if (vBuffer.ToString() == "#32770")<br /> {<br /> uint vProcessId;<br /> GetWindowThreadProcessId(hwnd, out vProcessId);<br /> if (GetProcessName(vProcessId).ToLower() == "qq.exe")<br /> {<br /> GetWindowText(hwnd, vBuffer, vBuffer.Capacity);<br /> if (vBuffer.ToString().IndexOf("聊天中") >= 0) // 標題中含"聊天中"<br /> {<br /> EnumChildWindows(hwnd, @EnumChild, lParam);<br /> return false;<br /> }<br /> }<br /> }<br /> return true;<br />}<br />[StructLayout(LayoutKind.Sequential)]<br />public struct GETTEXTLENGTHEX<br />{<br /> public uint flags;<br /> public uint codepage;<br />}<br />[StructLayout(LayoutKind.Sequential)]<br />public struct GETTEXTEX<br />{<br /> public int cb;<br /> public int flags;<br /> public int codepage;<br /> public IntPtr lpDefaultChar;<br /> public IntPtr lpUsedDefChar;<br />};<br />public const int GTL_DEFAULT = 0;<br />public const int GT_DEFAULT = 0;<br />public const int WM_USER = 0x0400;<br />public const int EM_GETTEXTEX = WM_USER + 94;<br />public const int EM_GETTEXTLENGTHEX = WM_USER + 95;<br />public string Process_ReadRichEditText(IntPtr AHandle)<br />{<br /> if (!IsWindow(AHandle)) return string.Empty;<br /> string vReturn = string.Empty;<br /> uint vProcessId;<br /> GetWindowThreadProcessId(AHandle, out vProcessId);<br /> IntPtr vProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ |<br /> PROCESS_VM_WRITE, false, vProcessId);<br /> try<br /> {<br /> uint vNumberOfBytesRead = 0;<br /> IntPtr vPointer = VirtualAllocEx(vProcess, IntPtr.Zero, 0x1000,<br /> MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);<br /> GETTEXTLENGTHEX vGetTextLengthEx = new GETTEXTLENGTHEX();<br /> vGetTextLengthEx.flags = GTL_DEFAULT;<br /> vGetTextLengthEx.codepage = 1200; // Unicode<br /> IntPtr vAddress = Marshal.AllocCoTaskMem(Marshal.SizeOf(vGetTextLengthEx));<br /> Marshal.StructureToPtr(vGetTextLengthEx, vAddress, false);<br /> WriteProcessMemory(vProcess, vPointer, vAddress,<br /> Marshal.SizeOf(vGetTextLengthEx), ref vNumberOfBytesRead);<br /> Marshal.FreeCoTaskMem(vAddress);<br /> int L = SendMessage(AHandle, EM_GETTEXTLENGTHEX, (int)vPointer, 0);<br /> VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);<br /> if (L <= 0) return vReturn;<br /> GETTEXTEX vGetTextEx = new GETTEXTEX();<br /> vGetTextEx.cb = L * 2 + 2;<br /> vGetTextEx.flags = GT_DEFAULT;<br /> vGetTextEx.codepage = 1200; // Unicode<br /> vGetTextEx.lpDefaultChar = IntPtr.Zero;<br /> vGetTextEx.lpUsedDefChar = IntPtr.Zero;<br /> vPointer = VirtualAllocEx(vProcess, IntPtr.Zero,<br /> (uint)(Marshal.SizeOf(vGetTextEx) + L * 2 + 2),<br /> MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);<br /> vAddress = Marshal.AllocCoTaskMem(Marshal.SizeOf(vGetTextEx));<br /> Marshal.StructureToPtr(vGetTextEx, vAddress, false);<br /> WriteProcessMemory(vProcess, vPointer, vAddress,<br /> Marshal.SizeOf(vGetTextEx), ref vNumberOfBytesRead);<br /> Marshal.FreeCoTaskMem(vAddress);<br /> SendMessage(AHandle, EM_GETTEXTEX, (int)vPointer,<br /> (int)vPointer + Marshal.SizeOf(vGetTextEx));<br /> vAddress = Marshal.AllocCoTaskMem(L * 2);<br /> ReadProcessMemory(vProcess,<br /> (IntPtr)((int)vPointer + Marshal.SizeOf(vGetTextEx)),<br /> vAddress, L * 2, ref vNumberOfBytesRead);<br /> vReturn = Marshal.PtrToStringUni(vAddress, L * 2);<br /> Marshal.FreeCoTaskMem(vAddress);<br /> VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);<br /> }<br /> finally<br /> {<br /> CloseHandle(vProcess);<br /> }<br /> return vReturn;<br />}<br />private void button1_Click(object sender, EventArgs e)<br />{<br /> richHandle = IntPtr.Zero;<br /> EnumWindows(EnumFunc, 0);<br /> if (richHandle == IntPtr.Zero) return;<br /> Console.WriteLine(Process_ReadRichEditText(richHandle));<br />} 

原文:http://blog.csdn.net/zswang/archive/2008/01/02/2009868.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.