C # obtain the value of the external program syslistview and the value of the Treeview through Win32 API

Source: Internet
Author: User

C # obtain external information through Win32 APIProgramSyslistview Value

 

Using system. runtime. interopservices;

Public const uint lvm_first = 0x1000;
Public const uint lvm_getitemcount = lvm_first + 4;
Public const uint lvm_getitemw = lvm_first + 75;

[Dllimport ("user32.dll")]
Public static extern int sendmessage (intptr hwnd, uint MSG, int wparam, int lparam );
[Dllimport ("user32.dll")]
Public static extern intptr findwindow (string lpszclass, string lpszwindow );
[Dllimport ("user32.dll")]
Public static extern intptr find1_wex (intptr hwndparent,
Intptr hwndchildafter, string lpszclass, string lpszwindow );
[Dllimport ("user32.dll")]
Public static extern uint getwindowthreadprocessid (intptr hwnd,
Out uint dwprocessid );

Public const uint process_vm_operation = 0x0008;
Public const uint process_vm_read = 0x0010;
Public const uint process_vm_write = 0x0020;

[Dllimport ("kernel32.dll")]
Public static extern intptr OpenProcess (uint dwdesiredaccess,
Bool binherithandle, uint dwprocessid );
Public const uint mem_commit = 0x1000;
Public const uint mem_release = 0x8000;

Public const uint mem_reserving = 0x2000;
Public const uint page_readwrite = 4;

[Dllimport ("kernel32.dll")]
Public static extern intptr virtualallocex (intptr hprocess, intptr lpaddress,
Uint dwsize, uint flallocationtype, uint flprotect );

[Dllimport ("kernel32.dll")]
Public static extern bool virtualfreeex (intptr hprocess, intptr lpaddress,
Uint dwsize, uint dwfreetype );

[Dllimport ("kernel32.dll")]
Public static extern bool closehandle (intptr handle );

[Dllimport ("kernel32.dll")]
Public static extern bool writeprocessmemory (intptr hprocess, intptr lpbaseaddress,
Intptr lpbuffer, int nsize, ref uint vnumberofbytesread );

[Dllimport ("kernel32.dll")]
Public static extern bool readprocessmemory (intptr hprocess, intptr lpbaseaddress,
Intptr lpbuffer, int nsize, ref uint vnumberofbytesread );

Public struct lvitem
{
Public int mask;
Public int iItem;
Public int isubitem;
Public int state;
Public int statemask;
Public intptr psztext; // string
Public int cchtextmax;
Public int iimage;
Public intptr lparam;
Public int iindent;
Public int igroupid;
Public int ccolumns;
Public intptr pucolumns;
}
Public int lvif_text = 0x0001;

Public int listview_getitemcount (intptr ahandle)
{
Return sendmessage (ahandle, lvm_getitemcount, 0, 0 );
}

Private void button#click (Object sender, eventargs E)
{
Intptr vhandle = findwindow ("#32770", "Windows Task Manager ");
Vhandle = findwindowex (vhandle, intptr. Zero, "#32770", null );
Vhandle = findwindowex (vhandle, intptr. Zero, "syslistview32", null );
If (vhandle = intptr. Zero) return;
Int vitemcount = listview_getitemcount (vhandle );
Uint vprocessid;
Getwindowthreadprocessid (vhandle, out vprocessid );

Intptr vprocess = OpenProcess (process_vm_operation | process_vm_read |
Process_vm_write, false, vprocessid );
Intptr vpointer = virtualallocex (vprocess, intptr. Zero, 4096,
Mem_reserve | mem_commit, page_readwrite );
Try
{
For (INT I = 0; I <vitemcount; I ++)
{
Byte [] vbuffer = new byte [256];
Lvitem [] vitem = new lvitem [1];
Vitem [0]. Mask = lvif_text;
Vitem [0]. iItem = I;
Vitem [0]. isubitem = 0;
Vitem [0]. cchtextmax = vbuffer. length;
Vitem [0]. psztext = (intptr) (INT) vpointer + marshal. sizeof (typeof (lvitem )));
Uint vnumberofbytesread = 0;

Writeprocessmemory (vprocess, vpointer,
Marshal. unsafeaddrofpinnedarrayelement (vitem, 0 ),
Marshal. sizeof (typeof (lvitem), ref vnumberofbytesread );
Sendmessage (vhandle, lvm_getitemw, I, vpointer. toint32 ());
Readprocessmemory (vprocess,
(Intptr) (INT) vpointer + marshal. sizeof (typeof (lvitem ))),
Marshal. unsafeaddrofpinnedarrayelement (vbuffer, 0 ),
Vbuffer. length, ref vnumberofbytesread );

String vtext = marshal. ptrtostringuni (
Marshal. unsafeaddrofpinnedarrayelement (vbuffer, 0 ));
Console. writeline (vtext );
}
}
Finally
{
Virtualfreeex (vprocess, vpointer, 0, mem_release );
Closehandle (vprocess );
}
}

C # obtain the value of the external program Treeview through Win32 API

 

Using system. runtime. interopservices;

Public const int TV _first = 0x1100;
Public const int tvm_getcount = TV _first + 5;
Public const int tvm_getnextitem = TV _first + 10;
Public const int tvm_getitema = TV _first + 12;
Public const int tvm_getitemw = TV _first + 62;

Public const int tvgn_root = 0x0000;
Public const int tvgn_next = 0x0001;
Public const int tvgn_previous = 0x0002;
Public const int tvgn_parents = 0x0003;
Public const int tvgn_child = 0x0004;
Public const int tvgn_firstvisible = 0x0005;
Public const int tvgn_nextvisible = 0x0006;
Public const int tvgn_previusvisible = 0x0007;
Public const int tvgn_drophilite = 0x0008;
Public constint tvgn_caret = 0x0009;
Public const int tvgn_lastvisible = 0x000a;

Public const int tvif_text = 0x0001;
Public const int tvif_image = 0x0002;
Public const int tvif_param = 0x0004;
Public const int tvif_state = 0x0008;
Public const int tvif_handle = 0x0010;
Public const int tvif_selectedimage = 0x0020;
Public const int tvif_children = 0x0040;
Public const int tvif_integral = 0x0080;

[Dllimport ("user32.dll")]
Public static extern int sendmessage (intptr hwnd,
Uint MSG, int wparam, int lparam );

[Dllimport ("kernel32.dll")]
Public static extern intptr OpenProcess (uint dwdesiredaccess,
Bool binherithandle, uint dwprocessid );
Public const uint mem_commit = 0x1000;
Public const uint mem_release = 0x8000;

Public const uint mem_reserving = 0x2000;
Public const uint page_readwrite = 4;

Public const uint process_vm_operation = 0x0008;
Public const uint process_vm_read = 0x0010;
Public const uint process_vm_write = 0x0020;

[Dllimport ("kernel32.dll")]
Public static extern intptr virtualallocex (intptr hprocess, intptr lpaddress,
Uint dwsize, uint flallocationtype, uint flprotect );

[Dllimport ("kernel32.dll")]
Public static extern bool virtualfreeex (intptr hprocess, intptr lpaddress,
Uint dwsize, uint dwfreetype );

[Dllimport ("kernel32.dll")]
Public static extern bool closehandle (intptr handle );

[Dllimport ("kernel32.dll")]
Public static extern bool writeprocessmemory (intptr hprocess, intptr lpbaseaddress,
Intptr lpbuffer, int nsize, ref uint vnumberofbytesread );

[Dllimport ("kernel32.dll")]
Public static extern bool readprocessmemory (intptr hprocess, intptr lpbaseaddress,
Intptr lpbuffer, int nsize, ref uint vnumberofbytesread );

[Dllimport ("user32.dll")]
Public static extern uint getwindowthreadprocessid (intptr hwnd,
Out uint dwprocessid );

[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;
Public intptr htreeitem;
}

Public static uint treeview_getcount (intptr hwnd)
{
Return (uint) sendmessage (hwnd, tvm_getcount, 0, 0 );
}

Public static intptr treeview_getnextitem (intptr hwnd, intptr hitem, int code)
{
Return (intptr) sendmessage (hwnd, tvm_getnextitem, Code, (INT) hitem );
}

Public static intptr treeview_getroot (intptr hwnd)
{
Return treeview_getnextitem (hwnd, intptr. Zero, tvgn_root );
}

Public static intptr treeview_getchild (intptr hwnd, intptr hitem)
{
Return treeview_getnextitem (hwnd, hitem, tvgn_child );
}

Public static intptr treeview_getnextsibling (intptr hwnd, intptr hitem)
{
Return treeview_getnextitem (hwnd, hitem, tvgn_next );
}

Public static intptr treeview_getparent (intptr hwnd, intptr hitem)
{
Return treeview_getnextitem (hwnd, hitem, tvgn_parent );
}

Public static intptr treenodegetnext (intptr ahandle, intptr atreeitem)
{
If (ahandle = intptr. Zero | atreeitem = intptr. Zero) return intptr. zero;
Intptr result = treeview_getchild (ahandle, atreeitem );
If (result = intptr. Zero)
Result = treeview_getnextsibling (ahandle, atreeitem );

Intptr vparentid = atreeitem;
While (result = intptr. Zero & vparentid! = Intptr. Zero)
{
Vparentid = treeview_getparent (ahandle, vparentid );
Result = treeview_getnextsibling (ahandle, vparentid );
}
Return result;
}

Public static bool gettreeviewtext (intptr ahandle, list <string> aoutput)
{
If (aoutput = NULL) return false;
Uint vprocessid;
Getwindowthreadprocessid (ahandle, out vprocessid );

Intptr vprocess = OpenProcess (process_vm_operation | process_vm_read |
Process_vm_write, false, vprocessid );
Intptr vpointer = virtualallocex (vprocess, intptr. Zero, 4096,
Mem_reserve | mem_commit, page_readwrite );
Try
{
Uint vitemcount = treeview_getcount (ahandle );
Intptr vtreeitem = treeview_getroot (ahandle );
Console. writeline (vitemcount );
For (INT I = 0; I <vitemcount; I ++)
{
Byte [] vbuffer = new byte [256];
Tvitem [] vitem = new tvitem [1];
Vitem [0] = new tvitem ();
Vitem [0]. Mask = tvif_text;
Vitem [0]. hitem = vtreeitem;
Vitem [0]. psztext = (intptr) (INT) vpointer + marshal. sizeof (typeof (tvitem )));
Vitem [0]. cchtextmax = vbuffer. length;
Uint vnumberofbytesread = 0;
Writeprocessmemory (vprocess, vpointer,
Marshal. unsafeaddrofpinnedarrayelement (vitem, 0 ),
Marshal. sizeof (typeof (tvitem), ref vnumberofbytesread );
Sendmessage (ahandle, tvm_getitema, 0, (INT) vpointer );
Readprocessmemory (vprocess,
(Intptr) (INT) vpointer + marshal. sizeof (typeof (tvitem ))),
Marshal. unsafeaddrofpinnedarrayelement (vbuffer, 0 ),
Vbuffer. length, ref vnumberofbytesread );
Console. writeline (marshal. ptrtostringansi (
Marshal. unsafeaddrofpinnedarrayelement (vbuffer, 0 )));

Vtreeitem = treenodegetnext (ahandle, vtreeitem );
}
}
Finally
{
Virtualfreeex (vprocess, vpointer, 0, mem_release );
Closehandle (vprocess );
}
Return true;
}

Private void button#click (Object sender, eventargs E)
{
List <string> voutput = new list <string> ();
Gettreeviewtext (intptr) xxxx, voutput); // replace XXXX with the corresponding Treeview handle. For more information, see use findwindow ().
Foreach (string vline in voutput)
Console. writeline (vline );
}

 

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.