OpenProcess Help

J

Jake Thompson

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
 
A

Armin Zingler

Jake said:
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/lib...nteropservices.marshal.getlastwin32error.aspx

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

Family Tree Mike

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

Try p.MainWindowHandle.
 
J

Jake Thompson

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
 
A

Armin Zingler

Jake said:
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.
 
J

Jake Thompson

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


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.


        hwnd as intptr


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

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


              Dim pid As integer


                  pid = p.Id


Use Marshal.GetLastWin32Error instead as I told you.



Got it working using MainWindowHandle turns out i did not have to use
openprocess after all

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

Const SW_MAXIMIZE As Integer = 3
Const SW_RESTORE As Integer = 9
Const SHOWNORMAL As Integer = 1
Const SW_MINIMIZE As Integer = 6
Const SW_HIDE As Integer = 0

Dim hWnd As UInt32
Dim theid As String = selection.Substring(26, 6)
Dim pid As String
Dim theProcesses() As Process = Process.GetProcessesByName("Foo")
For Each p As Process In theProcesses
pid = p.Id.ToString("000000")
If theid = pid Then
hWnd = p.MainWindowHandle.ToInt32
ShowWindow(hWnd, SHOWNORMAL)


Thanks Jake
 
A

Armin Zingler

Jake said:
Got it working using MainWindowHandle turns out i did not have to use
openprocess after all

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

Still: ByVal handle As IntPtr. Otherwise it fails on 64 bit OS.
And if you will ever be interested in the return value, it's
not wrong to declare it As Boolean.
Dim hWnd As UInt32

Dim hWnd As IntPtr

Dim theid As String = selection.Substring(26, 6)
Dim pid As String
Dim theProcesses() As Process = Process.GetProcessesByName("Foo")
For Each p As Process In theProcesses
pid = p.Id.ToString("000000")
If theid = pid Then

Why do you treat numbers as Strings?
hWnd = p.MainWindowHandle.ToInt32

hWnd = p.MainWindowHandle
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top