Getting List View Items from different application

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Runtime. InteropServices;
Using System. Drawing;
Using System. Reflection;
Using System. Windows. Forms;
Namespace CPTC_System_Log
{
Class GetWindowNameByPoint
{
Public delegate bool CallBackPtr (IntPtr hwnd, IntPtr lParam );
[DllImport ("user32.dll")]
Static extern IntPtr WindowFromPoint (Point );
[StructLayout (LayoutKind. Sequential)]
Private struct LVITEM
{
Public int mask;
Public int Item;
Public int SubItem;
Public int state;
Public int stateMask;
Public IntPtr pszText;
Public int cchTextMax;
Public int iImage;
Internal int lParam;
Internal int iIndent;
}
[StructLayout (LayoutKind. Sequential)]
Private struct RECT
{
Public int left;
Public int top;
Public int right;
Public int bottom;
}
[StructLayout (LayoutKind. Sequential)]
Public struct TVITEM
{
Public int mask;
Public IntPtr hItem;
Public int state;
Public int stateMask;
Public IntPtr pszText;
Public int cchTextMax;
Public int iImage;
Public int iSelectedImage;
Public int cChildren;
Public IntPtr lParam;
}
[DllImport ("user32.dll")]
Public static extern int SendMessage (
IntPtr hWnd, // handle to destination window
Uint Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter
);
[DllImport ("user32.dll")]
Private static extern int SendMessage (IntPtr hwnd, int msg, int wParam, StringBuilder sb );
[DllImport ("user32.dll", CharSet = CharSet. Auto)]
Static extern int GetClassName (IntPtr hWnd, StringBuilder lpClassName, int nMaxCount );
// Win32 constants.
Const int WM_GETTEXT = 0x000D;
Const int WM_GETTEXTLENGTH = 0x000E;

Public string GetText (IntPtr hWnd)
{
// StringBuilder dummy = new StringBuilder (10 );
Int length = SendMessage (hWnd, WM_GETTEXTLENGTH, IntPtr. Zero, IntPtr. Zero );
StringBuilder sb = new StringBuilder (length + 1 );
SendMessage (hWnd, WM_GETTEXT, length + 1, sb );
Return sb. ToString ();
}

Private const int LVM_FIRST = 0x1000;
Private const int LVM_GETSELECTEDCOUNT = (LVM_FIRST + 50 );
Private const int LVM_GETITEMSTATE = (LVM_FIRST + 44 );
Private const int LVM_GETITEMTEXT = (LVM_FIRST + 45 );
Private const int LVM_GETITEMCOUNT = LVM_FIRST + 4;
Private const int LVIF_TEXT = 0x1;
Private const int LVM_GETITEMRECT = (LVM_FIRST + 14 );
Private const int TV _FIRST = 0x1100;
Private const int TVM_GETNEXTITEM = TV _FIRST + 10;
Private const int TVM_GETITEM = TV _FIRST + 12;
Private const int TVM_GETVISIBLECOUNT = TV _FIRST + 16;
Private const int TVM_EXPAND = TV _FIRST + 2;
Private const int TVM_GETITEMRECT = TV _FIRST + 4;
Private const int TVM_GETCOUNT = TV _FIRST + 5;
Private const int TVM_GETINDENT = TV _FIRST + 6;
Private const int TVM_SETINDENT = TV _FIRST + 7;
Private const int TVM_ENSUREVISIBLE = TV _FIRST + 20;
Private const int TVGN_ROOT = 0x0;
[DllImport ("user32.dll", SetLastError = true)]
Static extern uint GetWindowThreadProcessId (IntPtr hWnd, out uint lpdwProcessId );
[DllImport ("kernel32.dll", SetLastError = true)]
Static extern IntPtr OpenProcess (UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId );
Const int PROCESS_TERMINATE = 0x1;
Const int PROCESS_CREATE_THREAD = 0x2;
Const int PROCESS_VM_OPERATION = 0x8;
Const int PROCESS_VM_READ = 0x10;
Const int PROCESS_VM_WRITE = 0x20;
Const int PROCESS_DUP_HANDLE = 0x40;
Const int PROCESS_CREATE_PROCESS = 0x80;
Const int PROCESS_SET_QUOTA = 0x100;
Const int PROCESS_SET_INFORMATION = 0x200;
Const int PROCESS_QUERY_INFORMATION = 0x400;
[DllImport ("kernel32.dll", SetLastError = true, ExactSpelling = true)]
Static extern IntPtr VirtualAllocEx (IntPtr hProcess, IntPtr lpAddress,
Uint dwSize, uint flAllocationType, uint flProtect );
Const int MEM_COMMIT = 0x1000;
Const int MEM_RELEASE = 0x8000;
Const int PAGE_READWRITE = 0x4;
[DllImport ("kernel32.dll")]
Public static extern bool ReadProcessMemory (
IntPtr hProcess,
IntPtr lpBaseAddress,
// [In, Out] byte [] buffer,
IntPtr buffer,
UInt32 size,
Out IntPtr lpNumberOfBytesRead
);
[DllImport ("kernel32.dll")]
Public static extern Int32 ReadProcessMemory (
IntPtr hProcess,
IntPtr lpBaseAddress,
[In, Out] byte [] buffer,
UInt32 size,
Out IntPtr lpNumberOfBytesRead
);
[DllImport ("kernel32")]
Static extern bool WriteProcessMemory (IntPtr hProcess, IntPtr lpBaseAddress,
Ref LVITEM buffer, int dwSize, IntPtr lpNumberOfBytesWritten );
[DllImport ("kernel32")]
Static extern bool WriteProcessMemory (IntPtr hProcess, IntPtr lpBaseAddress,
Ref TVITEM buffer, int dwSize, IntPtr lpNumberOfBytesWritten );
[DllImport ("kernel32")]
Static extern bool WriteProcessMemory (IntPtr hProcess, IntPtr lpBaseAddress,
Ref RECT buffer, int dwSize, IntPtr lpNumberOfBytesWritten );

[DllImport ("kernel32.dll", SetLastError = true, ExactSpelling = true)]
Static extern bool VirtualFreeEx (IntPtr hProcess, IntPtr lpAddress,
UIntPtr dwSize, uint dwFreeType );
[DllImport ("user32.dll")]
[Return: financialas (UnmanagedType. Bool)]
Static extern bool GetWindowRect (IntPtr hWnd, out RECT lpRect );
Public string GetWindowName (Point p)
{
StringBuilder sb = new StringBuilder ();
IntPtr hwnd = WindowFromPoint (p );
StringBuilder ClassName = new StringBuilder (100 );
// Get the window class name
If (GetClassName (hwnd, ClassName, ClassName. Capacity )! = 0)
{
RECT rect;
GetWindowRect (hwnd, out rect );
Point p1 = new Point (p. X-rect. left, p. Y-rect. top );
String itemText = GetListViewItemFromPoint (hwnd, p1 );
If (itemText. Length> 0)
{
Return itemText;
}
// ItemText = GetTreeViewItemFromPoint (hwnd, p1 );
// If (itemText. Length> 0)
//{
// Return itemText;
//}
}
String buf = GetText (hwnd );
Int len;
For (len = 0; len <buf. Length & buf [len]! = '\ 0'; len ++ );
Return buf. Substring (0, len); // + "(CLASS:" + ClassName + ")";
}
Public string GetListViewItemFromPoint (IntPtr hwnd, Point p)
{
StringBuilder sb = new StringBuilder ();
Int count = (int) SendMessage (hwnd, LVM_GETITEMCOUNT, IntPtr. Zero, IntPtr. Zero );
Lvitem lvi = new LVITEM ();
IntPtr pLVI = IntPtr. Zero;
Byte [] itemBuf = new byte [1, 512];
// Byte [] subItemBuf = new byte [1, 512];
IntPtr pItemBuf = IntPtr. Zero;
// IntPtr pSubItemBuf = IntPtr. Zero;
IntPtr pRect = IntPtr. Zero;
Uint pid = 0;
IntPtr process = IntPtr. Zero;
RECT rect = new RECT ();
GetWindowThreadProcessId (hwnd, out pid );
Process = OpenProcess (PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, 0, pid );
PLVI = VirtualAllocEx (process, IntPtr. Zero, (uint) Marshal. SizeOf (typeof (LVITEM), MEM_COMMIT, PAGE_READWRITE );
PItemBuf = VirtualAllocEx (process, IntPtr. Zero, 512, MEM_COMMIT, PAGE_READWRITE );
// PSubItemBuf = VirtualAllocEx (process, IntPtr. Zero, 512, MEM_COMMIT, PAGE_READWRITE );
PRect = VirtualAllocEx (process, IntPtr. Zero, (uint) Marshal. SizeOf (typeof (RECT), MEM_COMMIT, PAGE_READWRITE );
LVI. cchTextMax = 512;
For (int I = 0; I <count; I ++)
{
LVI. SubItem = 0;
LVI. pszText = pItemBuf;
WriteProcessMemory (process, pLVI, ref LVI, Marshal. SizeOf (typeof (LVITEM), IntPtr. Zero );
SendMessage (hwnd, LVM_GETITEMTEXT, (IntPtr) I, pLVI );
// LVI. SubItem = 1;
// LVI. pszText = pSubItemBuf;
// WriteProcessMemory (process, pLVI, ref LVI, Marshal. SizeOf (typeof (LVITEM), IntPtr. Zero );
// SendMessage (hwnd, LVM_GETITEMTEXT, (IntPtr) I, pLVI );
Unsafe
{
WriteProcessMemory (process, pRect, ref rect, Marshal. SizeOf (typeof (RECT), IntPtr. Zero );
SendMessage (hwnd, LVM_GETITEMRECT, (IntPtr) I, pRect );
IntPtr bytesReaded;
ReadProcessMemory (process, pItemBuf, itemBuf, 512, out bytesReaded );
// ReadProcessMemory (process, pSubItemBuf, subItemBuf, 512, out bytesReaded );
ReadProcessMemory (process, pRect, (IntPtr) (& rect), (uint) Marshal. SizeOf (typeof (RECT), out bytesReaded );
Int len;
For (len = 0; len <512 & itemBuf [len]! = '\ 0'; len ++ );
/// Sb. append (String. format ("{0}, {1}-{2}, {3 }:{ 4}, {5}", rect. left, rect. top, rect. right, rect. bottom, p. x, p. Y ));
If (rect. left <= p. X & rect. right> = p. X & rect. top <= p. Y & rect. bottom> = p. y)
{
Sb. Append (Encoding. ASCII. GetString (itemBuf). Substring (0, len ));
// Sb. Append ("\ r \ n ");
}
Else
{
// Sb. Append (Encoding. ASCII. GetString (itemBuf). Substring (0, len ));
/// Sb. Append ("\ r \ n ");
}
Rect. left = rect. right = rect. top = rect. bottom = 0;
}
}
VirtualFreeEx (process, pLVI, (UIntPtr) 0, MEM_RELEASE );
VirtualFreeEx (process, pItemBuf, (UIntPtr) 0, MEM_RELEASE );
// VirtualFreeEx (process, pSubItemBuf, (UIntPtr) 0, MEM_RELEASE );
Return sb. ToString ();
}
Public string GetTreeViewItemFromPoint (IntPtr hwnd, Point p)
{
StringBuilder sb = new StringBuilder ();
Int count = (int) SendMessage (hwnd, TVM_GETCOUNT, IntPtr. Zero, IntPtr. Zero );
Tvitem tvi = new TVITEM ();
IntPtr pTVI = IntPtr. Zero;
Byte [] itemBuf = new byte [1, 512];
// Byte [] subItemBuf = new byte [1, 512];
IntPtr pItemBuf = IntPtr. Zero;
// IntPtr pSubItemBuf = IntPtr. Zero;
IntPtr pRect = IntPtr. Zero;
Uint pid = 0;
IntPtr process = IntPtr. Zero;
RECT rect = new RECT ();
GetWindowThreadProcessId (hwnd, out pid );
Process = OpenProcess (PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, 0, pid );
PTVI = VirtualAllocEx (process, IntPtr. Zero, (uint) Marshal. SizeOf (typeof (TVITEM), MEM_COMMIT, PAGE_READWRITE );
PItemBuf = VirtualAllocEx (process, IntPtr. Zero, 512, MEM_COMMIT, PAGE_READWRITE );
// PSubItemBuf = VirtualAllocEx (process, IntPtr. Zero, 512, MEM_COMMIT, PAGE_READWRITE );
PRect = VirtualAllocEx (process, IntPtr. Zero, (uint) Marshal. SizeOf (typeof (RECT), MEM_COMMIT, PAGE_READWRITE );
For (int I = 0; I <count; I ++)
{
Unsafe
{
TVI. cchtexmax = 512;
TVI. hItem = (IntPtr) SendMessage (hwnd, TVM_GETNEXTITEM, (IntPtr) I, IntPtr. Zero );
TVI. mask = LVIF_TEXT;
TVI. pszText = pItemBuf;
WriteProcessMemory (process, pTVI, ref TVI, Marshal. SizeOf (typeof (TVITEM), IntPtr. Zero );
SendMessage (hwnd, TVM_GETITEM, (IntPtr) 0, pTVI );
// LVI. SubItem = 1;
// LVI. pszText = pSubItemBuf;
// WriteProcessMemory (process, pLVI, ref LVI, Marshal. SizeOf (typeof (LVITEM), IntPtr. Zero );
// SendMessage (hwnd, LVM_GETITEMTEXT, (IntPtr) I, pLVI );
WriteProcessMemory (process, pRect, ref rect, Marshal. SizeOf (typeof (RECT), IntPtr. Zero );
SendMessage (hwnd, TVM_GETITEMRECT, (IntPtr) TVI. hItem, pRect );
IntPtr bytesReaded;
ReadProcessMemory (process, pItemBuf, itemBuf, 512, out bytesReaded );
/// ReadProcessMemory (process, pSubItemBuf, subItemBuf, 512, out bytesReaded );
ReadProcessMemory (process, pRect, (IntPtr) (& rect), (uint) Marshal. SizeOf (typeof (RECT), out bytesReaded );
Int len;
For (len = 0; len <512 & itemBuf [len]! = '\ 0'; len ++ );
Sb. append (String. format ("{0}, {1}-{2}, {3} {4} \ r \ n", rect. left, rect. top, rect. right, rect. bottom,
Encoding. ASCII. GetString (itemBuf). Substring (0, len )));
// If (rect. left <= p. X & rect. right> = p. X & rect. top <= p. Y & rect. bottom> = p. y)
//{
// Sb. Append (Encoding. ASCII. GetString (itemBuf). Substring (0, len ));
/// Sb. Append ("\ r \ n ");
//}
// Else
//{
/// Sb. Append (Encoding. ASCII. GetString (itemBuf). Substring (0, len ));
//// Sb. Append ("\ r \ n ");
//}
Rect. left = rect. right = rect. top = rect. bottom = 0;
}
}
VirtualFreeEx (process, pTVI, (UIntPtr) 0, MEM_RELEASE );
VirtualFreeEx (process, pItemBuf, (UIntPtr) 0, MEM_RELEASE );
// VirtualFreeEx (process, pSubItemBuf, (UIntPtr) 0, MEM_RELEASE );
Return sb. ToString ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.