int Length=300; IntPtr THandle=APIs.GetLocalWindow();//取得當前鼠標所在位置的控件句柄 int Address=APIs.VirtualAllocEx(Process.GetCurrentProcess().Handle,0,Length,0x1000,0x04);//在本進程內分配Length大小的內存 APIs.SendMessage(THandle,0x000D,new IntPtr(255),new IntPtr(Address));//發送消息到目標控件,0x000D就是WM_GETTEXT,255的意思是保存返回的值,new IntPtr(Address)是指保存到Address指定的地址 byte[] buf=new byte[Length]; APIs.ReadProcessMemory(Process.GetCurrentProcess().Handle,Address,buf,Length,0);//讀取剛才保存的內容 MessageBox.Show(Encoding.Default.GetString(buf));//顯示出來測試一下.
其中APIs開頭的,是我自己寫的API類庫,相關聲明如下: [DllImport("user32.dll")] public static extern IntPtr WindowFromPoint( POINT lpPoint );
[DllImport("user32.dll")] public static extern int GetCursorPos( out POINT lpPoint ); public static IntPtr GetLocalWindow()//這個只是把上兩個結合了一下 { POINT point; GetCursorPos(out point); return WindowFromPoint(point); }
[DllImport("User32.dll")] public static extern IntPtr SendMessage( IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam );
[ DllImport( "Kernel32.dll" )] public static extern int ReadProcessMemory( System.IntPtr hProcess, System.Int32 lpBaseAddress, byte[] lpBuffer, long nSize, long lpNumberOfBytesWritten );