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.
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
.
added (#2):
here's short console program can enumerate windows, process & thread ids. there few differences snippet.
- i use intptr, not handleref. other people have pointed out, may confusing things you.
- i didn't specify
return
attribute. if required, should able add in. - i'm running administrator; things may run differently if running user-level privileges.
Comments
Post a Comment