AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Chris
Expertise
I can answer pretty much any question relating to VB.NET and its use in a Windows environment. I specialize in ASP.NET web development and MSSQL database access.

Experience
I have over 5 years of industry experience using VB.NET and other .NET technologies for web and database development.

Education/Credentials
I have some college education, but does it really matter in this field of work?

 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > Processes

Topic: VB.NET



Expert: Chris
Date: 8/28/2007
Subject: Processes

Question
I have written a program in vs2005 VB which lists all the processes on the local machine. I check to see if more then 1 cmd.exe (Dos Window) is open. If yes I kill the processes 2,3, etc. If only 1 cmd.exe (Dos Window) is open I want to switch to that process or window and make it active. Could you help at all ? I have achieved all except to make the only cmd.exe process active.

Answer
You'll need to make a few API calls to make this work, so start by adding a new Module to your project... we'll call it APICalls.vb:

'--[ APICalls.vb ]--
Imports System.Runtime.InteropServices

Module APICalls
 <DllImport("user32.dll")> _
 Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
 End Function

 <DllImport("user32.dll")> _
 Function ShowWindowAsync(ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
 End Function

 <DllImport("user32.dll")> _
 Function IsIconic(ByVal hWnd As IntPtr) As Boolean
 End Function

 Public Const SW_HIDE As Integer = 0
 Public Const SW_SHOWNORMAL As Integer = 1
 Public Const SW_SHOWMINIMIZED As Integer = 2
 Public Const SW_SHOWMAXIMIZED As Integer = 3
 Public Const SW_SHOWNOACTIVATE As Integer = 4
 Public Const SW_RESTORE As Integer = 9
 Public Const SW_SHOWDEFAULT As Integer = 10
End Module
'--[ end APICalls.vb ]--

And here's a quick example of how to use it:

'--[ Form1.vb ]--
Imports System.Diagnostics

Public Class Form1
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   Dim arrProcesses As Process() = Process.GetProcesses()

   For i As Integer = 0 To arrProcesses.Length - 1
     Dim objProc As Process = arrProcesses(i)
     If (objProc.ProcessName = "cmd") OrElse (objProc.MainWindowTitle.IndexOf("cmd.exe") >= 0) OrElse _
        ((objProc.MainModule IsNot Nothing) AndAlso (objProc.MainModule.FileName.IndexOf("cmd.exe") >= 0)) Then

       ' Get a pointer to the process' main window handle
       Dim hWnd As IntPtr = objProc.MainWindowHandle
       ' If it's minimized, restore it
       If IsIconic(hWnd) Then ShowWindowAsync(hWnd, SW_RESTORE)
      ' Set the window to foreground
      SetForegroundWindow(hWnd)

       Exit For
     End If
   Next
 End Sub
End Class
'--[ end Form1.vb ]--

Hope this helps!

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.