[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Windows

using API, i need code to find some stuff about all currently opened windows or programs.
Window Name and/or Title (sometimes the same)
exe file where it originated
0
TCPIP2600
Asked:
TCPIP2600
  • 2
1 Solution
 
samopalCommented:
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Const WM_CLOSE = &H10
Dim strCaptions() As String

Dim lngHandle() As Long

'get window by the part of it's caption
Public Function GetWindow(strCaption As String) As Boolean
   Dim iCount As Integer
   Dim i As Integer
   Dim Pos As Integer
   Dim lngEnum As Long

   ReDim strCaptions(0)
   ReDim lngHandle(0)
   lngEnum = EnumWindows(AddressOf Callback1_EnumWindows, 0)

   For i = 0 To UBound(strCaptions)
' if window found
      Pos = InStr(1, strCaptions(i), strCaption, vbTextCompare)
      If Pos > 0 Then
' close it for example
             SendMessage lngHandle(i), WM_CLOSE, 0, 0
            iCount = iCount + 1
       End If
   Next
   If iCount >= 1 Then
        CloseProg = True
   Else
         CloseProg = False
   End If
End

Function Public Function Callback1_EnumWindows(ByVal hwnd As Long, _
       ByVal lpData As Long) As Long
   Dim cnt As Long
   Dim rttitle As String * 256
   cnt = GetWindowText(hwnd, rttitle, 255)

   If cnt > 0 Then
      ReDim Preserve lngHandle(UBound(strCaptions) +1)
      ReDim Preserve strCaptions(UBound(strCaptions) + 1)
      strCaptions(UBound(strCaptions)) = Left$(rttitle, cnt)
      lngHandle(UBound(lngHandle)) = hwnd
   End If
   Callback1_EnumWindows = 1
End Function

0
 
samopalCommented:
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" ( ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Const WM_CLOSE = &H10
Dim strCaptions() As String

Dim lngHandle() As Long

'get window by the part of it's caption
Public Function GetWindow(strCaption As String) As Boolean
   Dim iCount As Integer
   Dim i As Integer
   Dim Pos As Integer
   Dim lngEnum As Long

   ReDim strCaptions(0)
   ReDim lngHandle(0)
   lngEnum = EnumWindows(AddressOf Callback1_EnumWindows, 0)

   For i = 0 To UBound(strCaptions)
' if window found
      Pos = InStr(1, strCaptions(i), strCaption, vbTextCompare)
      If Pos > 0 Then
' close it for example
             SendMessage lngHandle(i), WM_CLOSE, 0, 0
            iCount = iCount + 1
       End If
   Next
   If iCount >= 1 Then
        CloseProg = True
   Else
         CloseProg = False
   End If
End Function

Public Function Callback1_EnumWindows(ByVal hwnd As Long, _
       ByVal lpData As Long) As Long
   Dim cnt As Long
   Dim rttitle As String * 256
   cnt = GetWindowText(hwnd, rttitle, 255)

   If cnt > 0 Then
      ReDim Preserve lngHandle(UBound(strCaptions) +1)
      ReDim Preserve strCaptions(UBound(strCaptions) + 1)
      strCaptions(UBound(strCaptions)) = Left$(rttitle, cnt)
      lngHandle(UBound(lngHandle)) = hwnd
   End If
   Callback1_EnumWindows = 1
End Function

0
 
mcriderCommented:
Check out these microsoft KB articles:

MODLIST.EXE Shows How to Enumerate Processes and Modules
http://support.microsoft.com/support/kb/articles/q192/9/86.asp?LNG=ENG&SA=MSDN 

HOWTO: List Running Processes
http://support.microsoft.com/support/kb/articles/Q187/9/13.ASP 
 
HOWTO: Enumerate Windows Using the WIN32 API
http://support.microsoft.com/support/kb/articles/q183/0/09.asp?LNG=ENG&SA=MSDN 


 
If you want the descriptive names like in the TASK LIST, Create a new project with a listbox and add the following code to the form...


Cheers!


THE CODE:

    DefInt A-Z
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _
        ByVal wCmd As Long) As Long

    Private Declare Function GetWindowText Lib "user32" Alias _
        "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _
        ByVal cch As Long) As Long

    Private Declare Function GetWindowTextLength Lib "user32" _
        Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

    'Declare constants used by GetWindow.
    Const GW_CHILD = 5
    Const GW_HWNDFIRST = 0
    Const GW_HWNDLAST = 1
    Const GW_HWNDNEXT = 2
    Const GW_HWNDPREV = 3
    Const GW_OWNER = 4
   Sub LoadTaskList()
      'Get the hWnd of the first item in the master list
      'so we can process the task list entries (top-level only).
      CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)

      'Loop while the hWnd returned by GetWindow is valid.
      While CurrWnd <> 0
         'Get the length of task name identified by CurrWnd in the list.
         Length = GetWindowTextLength(CurrWnd)

         'Get task name of the task in the master list.
         ListItem$ = Space$(Length + 1)
         Length = GetWindowText(CurrWnd, ListItem$, Length + 1)

         'If there is a task name in the list, add the item to the list.
         If Length > 0 Then
            List1.AddItem ListItem$
         End If

         'Get the next task list item in the master list.
         CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)

         'Process Windows events.
         x = DoEvents()
      Wend
   End Sub
 
   Sub Form_Load()
      Call LoadTaskList
   End Sub
 


0
 
RuchiCommented:
Declaration:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Form:

Sub Get_User_Name()

     ' Dimension variables
     Dim lpBuff As String * 25
     Dim ret As Long, UserName As String

     ' Get the user name minus any trailing spaces found in the name.
     ret = GetUserName(lpBuff, 25)
     UserName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)

     ' Display the User Name
     MsgBox UserName
End Sub
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now