c# - How do i get Process.MainMoudle.FileName? -
i have class:
using system; using system.collections.generic; using system.linq; using system.text; using system.runtime.interopservices; using system.windows.forms; using system.componentmodel; namespace hardwaremonitoring { public class getprocesses: iwin32window { internal intptr handle; internal string title; internal string filename; public intptr handle { { return handle; } } public string title { { return title; } } public string filename { { return filename; } } public static readonly int32 gwl_style = -16; public static readonly uint64 ws_visible = 0x10000000l; public static readonly uint64 ws_border = 0x00800000l; public static readonly uint64 desired_ws = ws_border | ws_visible; public delegate boolean enumwindowscallback(intptr hwnd, int32 lparam); public static list<getprocesses> getallwindows() { list<getprocesses> windows = new list<getprocesses>(); stringbuilder buffer = new stringbuilder(100); enumwindows(delegate(intptr hwnd, int32 lparam) { if ((getwindowlonga(hwnd, gwl_style) & desired_ws) == desired_ws) { getwindowtext(hwnd, buffer, buffer.capacity); getprocesses wnd = new getprocesses(); wnd.handle = hwnd; wnd.title = buffer.tostring(); windows.add(wnd); } return true; }, 0); return windows; } [dllimport("user32.dll")] static extern int32 enumwindows(enumwindowscallback lpenumfunc, int32 lparam); [dllimport("user32.dll")] public static extern void getwindowtext(intptr hwnd, stringbuilder lpstring, int32 nmaxcount); [dllimport("user32.dll")] static extern uint64 getwindowlonga(intptr hwnd, int32 nindex); } } and in form1:
foreach (var wnd in getprocesses.getallwindows()) { } with wnd can handle , titles of processes need filename.
if doing:
foreach (process p in process.getprocesses()) then do:
p.mainmodule.filename how can filename property in class ? add there property how value ?
edit
in class bottom added:
[dllimport("user32.dll")] static extern uint getwindowthreadprocessid(intptr hwnd, intptr processid); then in class used it:
wnd.processid = getwindowthreadprocessid(wnd.handle, intptr.zero); and in form1:
string ff = ""; void populateapplications() { foreach (var wnd in getprocesses.getallwindows()) { uint uuu = wnd.processid; ff = process.getprocessbyid((int)uuu).mainmodule.filename; } } but when it's doing line:
ff = process.getprocessbyid((int)uuu).mainmodule.filename; it nothing exit program. no errors or exceptions.
edit
in class bottom:
[dllimport("user32.dll")] public static extern int getwindowthreadprocessid(intptr hwnd, out int id); then using in form1:
int ff = 0; list<string> filenames = new list<string>(); void populateapplications() { foreach (var wnd in getprocesses.getallwindows()) { getprocesses.getwindowthreadprocessid(wnd.handle, out ff); filenames.add(process.getprocessbyid(ff).mainmodule.filename); } } i'm getting 18 processes filenames.
Comments
Post a Comment