How do I get Process ID from HWND using managed VB.net code? -
is there managed vb.net way process id hwnd rather using windows api call.
private declare auto function getwindowthreadprocessid lib "user32.dll" (byval hwnd intptr, _ byref lpdwprocessid integer) integer sub getprocessid() 'start application dim xlapp object = createobject("excel.application") 'get window handle dim xlhwnd integer = xlapp.hwnd 'this have process id after call getwindowthreadprocessid dim procidxl integer = 0 'get process id getwindowthreadprocessid(xlhwnd, procidxl) 'get process dim xproc process = process.getprocessbyid(procidxl) end sub
no, isn't wrapped .net. there's absolutely nothing wrong calling native api functions. that's framework internally, , that's why p/invoke invented, make simple possible yourself. i'm not sure why you're seeking avoid it.
of course, recommend using new-style declaration, more idiomatic way of doing things in .net (rather old vb 6 way):
<dllimport("user32.dll", setlasterror:=true)> _ private shared function getwindowthreadprocessid(byval hwnd intptr, _ byref lpdwprocessid integer) integer end function
your other option, if absolutely cannot on irrational compulsion stay managed code, make use of process
class. can used start external process, , has property (id
) can used retrieve process's id. i'm not sure if work you. avoid telling why you're using createobject
in first place.
Comments
Post a Comment