c# - Why i'm getting Win32Exception when trying to get info from process inside a backgroundworker? -
dowork event:
private void backgroundworker1_dowork(object sender, doworkeventargs e) { backgroundworker worker = sender backgroundworker; while (true) { if ((worker.cancellationpending == true)) { e.cancel = true; break; } else { stopwatch sw = stopwatch.startnew(); populateapplications(); sw.stop(); int msec = 1000 - (int)sw.elapsedmilliseconds; if (msec < 1) msec = 1; // don't consume 100% cpu system.threading.thread.sleep(msec); } } } populateapplications method: private void populateapplications() { this.begininvoke(new methodinvoker(delegate { var icon = icon.extractassociatedicon(getprocessinfo(getprocessintptr()).processfilename); image ima = icon.tobitmap(); ima = resizeimage(ima, new size(25, 25)); ima = (image)(new bitmap(ima, new size(25, 25))); string status = getprocessinfo(getprocessintptr()).processresponding ? "running" : "not responding"; })); }
getprocessinfo method:
public processinfo getprocessinfo(intptr hwnd) { uint pid = 0; getwindowthreadprocessid(hwnd, out pid); process proc = process.getprocessbyid((int)pid); return new processinfo { processfilename = proc.mainmodule.filename.tostring(), processname = proc.processname, processtitle = proc.mainwindowtitle, processresponding = proc.responding }; }
and last processinfo class:
public class processinfo { public string processname { get; set; } public string processfilename { get; set; } public string processtitle { get; set; } public bool processresponding { get; set; } }
if i'm calling method populateapplications in dowork event exception if not call method not throw exception , i'm using getprocessinfo in other places in program , when call dowork event populateapplications method throw exception.
system.componentmodel.win32exception unhandled hresult=-2147467259 message=a 32 bit processes cannot access modules of 64 bit process. source=system errorcode=-2147467259 nativeerrorcode=299 stacktrace: @ system.diagnostics.ntprocessmanager.getmoduleinfos(int32 processid, boolean firstmoduleonly) @ system.diagnostics.ntprocessmanager.getfirstmoduleinfo(int32 processid) @ system.diagnostics.process.get_mainmodule() @ automation.form1.getprocessinfo(intptr hwnd) in d:\c-sharp\automation\automation\automation\form1.cs:line 573 @ automation.form1.<populateapplications>b__1() in d:\c-sharp\automation\automation\automation\form1.cs:line 616 @ system.windows.forms.control.invokemarshaledcallbackdo(threadmethodentry tme) @ system.windows.forms.control.invokemarshaledcallbackhelper(object obj) @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.windows.forms.control.invokemarshaledcallback(threadmethodentry tme) @ system.windows.forms.control.invokemarshaledcallbacks() @ system.windows.forms.control.wndproc(message& m) @ system.windows.forms.scrollablecontrol.wndproc(message& m) @ system.windows.forms.form.wndproc(message& m) @ system.windows.forms.control.controlnativewindow.onmessage(message& m) @ system.windows.forms.control.controlnativewindow.wndproc(message& m) @ system.windows.forms.nativewindow.debuggablecallback(intptr hwnd, int32 msg, intptr wparam, intptr lparam) @ system.windows.forms.unsafenativemethods.dispatchmessagew(msg& msg) @ system.windows.forms.application.componentmanager.system.windows.forms.unsafenativemethods.imsocomponentmanager.fpushmessageloop(intptr dwcomponentid, int32 reason, int32 pvloopdata) @ system.windows.forms.application.threadcontext.runmessageloopinner(int32 reason, applicationcontext context) @ system.windows.forms.application.threadcontext.runmessageloop(int32 reason, applicationcontext context) @ system.windows.forms.application.run(form mainform) @ automation.program.main() in d:\c-sharp\automation\automation\automation\program.cs:line 19 @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args) @ system.appdomain.executeassembly(string assemblyfile, evidence assemblysecurity, string[] args) @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly() @ system.threading.threadhelper.threadstart_context(object state) @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.threadhelper.threadstart() innerexception:
it's not possible because there no "legal" way manage 64bit memory space 32bit application. (some differences in memory managing model, different pointer size etc.)
you should change target platform x32 (or mixed/anycpu) x64. can in project properties.
Comments
Post a Comment