c# 4.0 - C# - user32.dll - GetWindowRect problem -


[dllimport("user32.dll")] [return: marshalas(unmanagedtype.bool)] static extern bool getwindowrect(handleref hwnd, out rect lprect);  [structlayout(layoutkind.sequential)] public struct rect {     public int left;        // x position of upper-left corner     public int top;         // y position of upper-left corner     public int right;       // x position of lower-right corner     public int bottom;      // y position of lower-right corner }  foreach (process pr in process.getprocesses()) {     rect rc;     getwindowrect(???, out rc); 

what should put " ??? " ? . tells me have put handleref object not know how handleref object process method.

if need window coordinates window in process, there other ways window handle don't require enumerating processes.

for winforms windows, use handle property.

system.windows.forms.control ... handle property @ msdn

for wpf applications, use windowinterophelper

system.windows.interop ... windowinterophelper class @ msdn

if trying enumerate windows aren't able access directly .net; 3rd party control creates top-level window out of scope of code, may wish enumerate via win32 enumwindows function.

enumwindows (win32) @ msdn

signatures p/invoke enumwindows available here:

user32.dll enumwindows @ pinvoke.net

added:

looks want enumerate windows & associated processes. use enumwindows, call getwindowthreadprocessid associated process & unmanaged thread id each window.

getwindowthreadprocessid (win32) @ msdn

the p/invoke signature available here:

user32.dll getwindowthreadprocessid @ pinvoke.net

last, can process object via static method getprocessbyid.

process.getprocessbyid @ msdn

added (#2):

here's short console program can enumerate windows, process & thread ids. there few differences snippet.

  1. i use intptr, not handleref. other people have pointed out, may confusing things you.
  2. i didn't specify return attribute. if required, should able add in.
  3. i'm running administrator; things may run differently if running user-level privileges.

c# source code example @ gist.github


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -