From: Jake Thompson on
Anybody know what I am doing wrong when I try and get the handle of
the process?


Private Declare Function OpenProcess Lib "kernel32" (ByVal
dwDesiredAccess As Long, ByVal blnheritHandle As Boolean, ByVal
dwAppProcessId As IntPtr) As IntPtr

Public Const PROCESS_ALL_ACCESS = &H1F0FFF

Const SW_MAXIMIZE = 3
Const SW_RESTORE = 9
Const SHOWNORMAL = 1
Private Declare Function ShowWindow Lib "user32" (ByVal handle As
IntPtr, ByVal nCmdShow As Integer) As Integer
Private Declare Function GetLastError Lib "kernel32" () As Long

If LBSessions.SelectedIndex > -1 Then


Dim selection As String = LBSessions.SelectedItem.ToString
Dim theid As String = selection.Substring(40, 4)
Dim pid As String

'the usage in the sub
Dim theProcesses() As Process =
Process.GetProcessesByName("Foo")
For Each p As Process In theProcesses
pid = p.Id

MsgBox(theid)
MsgBox(pid)

If theid = pid Then <---theid and the pid match up so
it's finding the process I want
MsgBox("in here")



'hWnd = p.Handle <---Tried this it returns a
number but ShowWindow does not bring up the window

'hWnd = OpenProcess(PROCESS_ALL_ACCESS, False,
pid) <--- tried this hWnd came back 0

hWnd = OpenProcess(PROCESS_ALL_ACCESS, False,
(Convert.ToInt32(pid))) <--- tried this hWnd came back 0
'hWnd =
Diagnostics.Process.GetProcessesByName("Foo")
(0).MainWindowHandle.ToInt32 <--- tried this hWnd came back 0




Dim theerror As Long = GetLastError()
MsgBox(theerror) <----- The last error for the
last three hwnd calls is 87 Invalid Parameter




ShowWindow(hWnd, SW_RESTORE)

End If

Next


End If

What I want to do is get a handle to the process so that I can show
the application that it's related to on the screen

Am I on the right track - completely wrong, or something else.

Help?

Thanks
Jake
From: Armin Zingler on
Jake Thompson schrieb:
> Anybody know what I am doing wrong when I try and get the handle of
> the process?
>
>
> Private Declare Function OpenProcess Lib "kernel32" (ByVal
> dwDesiredAccess As Long, ByVal blnheritHandle As Boolean, ByVal
> dwAppProcessId As IntPtr) As IntPtr

First arg must be DWORD = 32 bit usigned integer = UInteger or UInt32.
Same for dwAppProcessIdLast.


> Private Declare Function GetLastError Lib "kernel32" () As Long

Return value is DWORD = UInteger

However you should better call http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getlastwin32error.aspx

In addition, use Option Strict On and fix the obvious errors. If it still doesn't work,
ask again.


--
Armin


From: Family Tree Mike on


"Jake Thompson" wrote:

>
> 'hWnd = p.Handle <---Tried this it returns a
> number but ShowWindow does not bring up the window
> .
>

Try p.MainWindowHandle.
From: Jake Thompson on
On Feb 4, 11:58 am, Family Tree Mike
<FamilyTreeM...(a)discussions.microsoft.com> wrote:
> "Jake Thompson" wrote:
>
> >                     'hWnd = p.Handle <---Tried this it returns a
> > number but ShowWindow does not bring up the window
> > .
>
> Try p.MainWindowHandle.

First off I tried p.MainWindowHandle and the handle was still 0

I then changed my code around and put on option strict as suggested by
Armin The good news is that I dont get an error anymore for the
openprocess and the return value that is put into hWnd is 420 -

I then call ShowWindow(hWnd, SW_MAXIMIZE)
that does not produce an error but it also does not maximize my
window
I tried it with RESTORE and SHOWNORMAL as well

all came back error free but didnt do anything to display the process
window


Public Class SMonitorForm

Private Declare Function OpenProcess Lib "kernel32" (ByVal
dwDesiredAccess As UInt32, ByVal blnheritHandle As Boolean, ByVal
dwAppProcessId As UInt32) As UInt32

Public Const PROCESS_ALL_ACCESS As UInt32 = &H1F0FFF

Const SW_MAXIMIZE As UInt32 = 3
Const SW_RESTORE As UInt32 = 9
Const SHOWNORMAL As UInt32 = 1

Private Declare Function ShowWindow Lib "user32" (ByVal handle As
UInt32, ByVal nCmdShow As UInt32) As UInt32
Private Declare Function GetLastError Lib "kernel32" () As
UInt32

Private Sub BTNShow_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles BTNShow.Click
Dim hWnd As UInt32
Dim theerror As UInt32

If LBSessions.SelectedIndex > -1 Then


Dim selection As String = LBSessions.SelectedItem.ToString
Dim theid As String = selection.Substring(40, 4)
Dim pid As String

'the usage in the sub
Dim theProcesses() As Process =
Process.GetProcessesByName("FTExec")
For Each p As Process In theProcesses
pid = p.Id.ToString




If theid = pid Then
MsgBox("in here")
MsgBox(theid)
MsgBox(pid)
hWnd = OpenProcess(PROCESS_ALL_ACCESS, False,
(Convert.ToUInt32(pid)))


theerror = GetLastError()
MsgBox("The first error check")
MsgBox(theerror) <-----0
MsgBox("The Handle")
MsgBox(hWnd) <----420

ShowWindow(hWnd, SW_MAXIMIZE)

theerror = GetLastError()
MsgBox("The second error check")
MsgBox(theerror) <-----0

End If

Next


End If
End Sub

End Class

Ideas????

Thanks Jake

From: Armin Zingler on
Jake Thompson schrieb:
> Private Declare Function OpenProcess Lib "kernel32" (ByVal
> dwDesiredAccess As UInt32, ByVal blnheritHandle As Boolean, ByVal
> dwAppProcessId As UInt32) As UInt32

Why did you change the type of the return value? IntPtr was correct.

> Private Declare Function ShowWindow Lib "user32" (ByVal handle As
> UInt32, ByVal nCmdShow As UInt32) As UInt32

Now the 1st argument is wrong. Must be IntPtr. nCmdShow should be Int32 (Integer),
but that's no problem in practice. Return value can be declared As Boolean.


> Private Sub BTNShow_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles BTNShow.Click
> Dim hWnd As UInt32

hwnd as intptr

> Dim theid As String = selection.Substring(40, 4)

Store the IDs in appropriate types, Integer in this case.

Dim theid As integer = integer.parse(selection.Substring(40, 4))


> Dim pid As String

Dim pid As integer


> pid = p.Id.ToString

pid = p.Id




> theerror = GetLastError()

Use Marshal.GetLastWin32Error instead as I told you.



--
Armin