Question

How to get the handle of a running application using vb6

Asked by: 1030071002

How to get the handle of a running application using vb6

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-08 at 13:35:10ID24797484
Tags

How to get the handle of a running application using vb6

Topics

Visual Basic Programming

,

Miscellaneous Programming

Participating Experts
4
Points
500
Comments
7

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. VB6 IIS Application
    I have created a very basic IIS application in VB6. It works fine on Win95 with PWS. If I ftp the files across to my web site, I get the error : Server object error 'ASP 0177 : 800401f3' Server.CreateObject Failed /macey/mcfarlane/scripts/wcdemo.asp, line 8 Invalid cla...
  2. Handling events from a control array in VB6
    Hi. I'm using VB6. On my main form there is a control array of TextBox's. I have a class with which I would like to handle the events for this control array. I'm setting up the association in this manner. ---- MyClass Start ---- Private m_editTextArray As Object Privat...
  3. Global error handling in VB6
    We are using a VB6 front end with many forms to control a robot via a microprocessor. If power to the microprocessor goes down (or the USB cable to the microprocessor is disconnected) the program causes a fatal run-time error, code 1084: The device driver call failed. How can...
  4. handle
    Hi If we have a few internet explorers open how do we obtain a reference to each internet explorer running in vb6 to handle each window separately thanks
  5. Handling Dates in SQL Server <--> VB6
    The answer is probably as clear as day, but I am pulling my hair out over it. I have a SQL Server 2000 database that I am interfacing against with VB6. There are a lot of tables with SmallDateTime date formats. When I run a query, for date fields with "no" value (I ...
  6. error handling in vb6
    i want to know how to use error handling in vb6 my main problem is i don't want to repeat error handler code in each method or events or procedure in details suppose i have two button it has suppose to be click events one is form_load i don't to write separate error han...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: wrmichaelPosted on 2009-10-08 at 13:38:53ID: 25529821

Option Compare Text: Option Explicit: Option Base 0
 
Private Type PROCESSENTRY32
  size As Long
  usage As Long
  processId As Long
  defaultHeapId As Long
  moduleId As Long
  cntThreads As Long
  parentProcessId As Long
  classBase As Long
  flags As Long
  exeFile As String * 500
End Type
 
Private Const INVALID_HANDLE_VALUE As Long = -1
Private Const TH32CS_SNAPPROCESS As Long = 2
 
Private Declare Function kCloseHandle Lib "kernel32.dll" Alias "CloseHandle" (ByVal hObject As Long) As Long
Private Declare Function kCreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" (ByVal flags As Long, ByVal processId As Long) As Long
Private Declare Function kProcessFirst Lib "kernel32.dll" Alias "Process32First" (ByVal hSnapshot As Long, processEntry As PROCESSENTRY32) As Long
Private Declare Function kProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, processEntry As PROCESSENTRY32) As Long
 
Public Sub main(): Call printProcessNames: End Sub
 
Public Sub printProcessNames()
Dim processes() As String, i As Long
  If getProcessNames(processes) Then
    For i = LBound(processes) To UBound(processes)
      Debug.Print i, processes(i)
    Next i
  End If
End Sub
 
Private Function getProcessNames(ByRef processes() As String, Optional ByVal sortResults As Boolean = False) As Long
Dim retVal As Long, hSnapshot As Long, processEntry As PROCESSENTRY32
  Let hSnapshot = kCreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0)
  If hSnapshot = INVALID_HANDLE_VALUE Then
    Let retVal = 0
  Else: Let processEntry.size = Len(processEntry)
    If kProcessFirst(hSnapshot, processEntry) Then
      ReDim processes(0 To 0): Do: Call ensureCapacityString(processes, retVal)
        Let processes(inc(retVal)) = getNullTerminatedString(processEntry.exeFile)
      Loop While kProcessNext(hSnapshot, processEntry)
      If retVal <= UBound(processes) Then ReDim Preserve processes(0 To retVal - 1)
    Else: Let retVal = 0
    End If: Call kCloseHandle(hSnapshot)
    'If sortResults Then Call shellSortString(processes)
  End If: Let getProcessNames = retVal
End Function
 
Private Sub ensureCapacityString(ByRef vector() As String, ByVal size As Long, Optional ByVal chunk As Long = 256)
Dim oldCapacity As Long, newCapacity As Long
  Let oldCapacity = 1 - LBound(vector) + UBound(vector)
  Let newCapacity = IIf(size And 255 = 0, size, 256 * (1 + size \ 256))
  If oldCapacity < newCapacity Then ReDim Preserve vector(LBound(vector) To LBound(vector) - 1 + newCapacity)
End Sub
 
Private Function getNullTerminatedString(ByRef str As String) As String
Dim i As Long
  Let i = InStr(str, vbNullChar)
  Let getNullTerminatedString = Left$(str, IIf(i = 0, Len(str), i - 1))
End Function
 
Private Function inc(ByRef i As Long) As Long: Let inc = i: Let i = i + 1: End Function

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:

Select allOpen in new window

 

by: 1030071002Posted on 2009-10-08 at 13:43:41ID: 25529868

I said that wrong. How can I get a specific application handle like a adobe

 

by: wrmichaelPosted on 2009-10-08 at 13:47:03ID: 25529908

as you enumerate through the process you can check the process name.

once you find what you want, you save the processid/handle and break out.

if you don't find one, set your handle to -1 so you know it wasn't found and be sure to handle that.

 

by: egl1044Posted on 2009-10-08 at 14:01:47ID: 25530040

You use OpenProcess API and pass it a process id. This returns a real process handle.

 

by: game-masterPosted on 2009-10-08 at 18:09:23ID: 25531716



as egl104 said...u can use the OpenProcess API for this...

here is some snippets on how to use it...
i hope i can give u some idea...


game-master


'Remark: If you're using VB4 or VB5, you should first uncomment
' the Replace function (on the end of the code)
 
'In a form
Private Sub Form_Load()
    'Code submitted by Roger Taylor
    'enumerate all the different explorer.exe processes
    GetProcesses "explorer.exe"
End Sub
 
'In a module
 
Public Declare Function GetProcessMemoryInfo Lib "PSAPI.DLL" (ByVal hProcess As Long, ppsmemCounters As PROCESS_MEMORY_COUNTERS, ByVal cb As Long) As Long
Public Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Public Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Public Declare Function EnumProcesses Lib "PSAPI.DLL" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function GetModuleFileNameExA Lib "PSAPI.DLL" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Public Declare Function EnumProcessModules Lib "PSAPI.DLL" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Public Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer
Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)
 
 
 
Public Const PROCESS_QUERY_INFORMATION = 1024
Public Const PROCESS_VM_READ = 16
Public Const MAX_PATH = 260
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SYNCHRONIZE = &H100000
Public Const PROCESS_ALL_ACCESS = &H1F0FFF
Public Const TH32CS_SNAPPROCESS = &H2&
Public Const hNull = 0
Public Const WIN95_System_Found = 1
Public Const WINNT_System_Found = 2
Public Const Default_Log_Size = 10000000
Public Const Default_Log_Days = 0
Public Const SPECIFIC_RIGHTS_ALL = &HFFFF
Public Const STANDARD_RIGHTS_ALL = &H1F0000
 
 
Type MEMORYSTATUS
    dwLength As Long
    dwMemoryLoad As Long
    dwTotalPhys As Long
    dwAvailPhys As Long
    dwTotalPageFile As Long
    dwAvailPageFile As Long
    dwTotalVirtual As Long
    dwAvailVirtual As Long
End Type
 
 
Type PROCESS_MEMORY_COUNTERS
    cb As Long
    PageFaultCount As Long
    PeakWorkingSetSize As Long
    WorkingSetSize As Long
    QuotaPeakPagedPoolUsage As Long
    QuotaPagedPoolUsage As Long
    QuotaPeakNonPagedPoolUsage As Long
    QuotaNonPagedPoolUsage As Long
    PagefileUsage As Long
    PeakPagefileUsage As Long
End Type
 
 
Public Type PROCESSENTRY32
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long ' This process
    th32DefaultHeapID As Long
    th32ModuleID As Long ' Associated exe
    cntThreads As Long
    th32ParentProcessID As Long ' This process's parent process
    pcPriClassBase As Long ' Base priority of process threads
    dwFlags As Long
    szExeFile As String * 260 ' MAX_PATH
    End Type
 
 
Public Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long '1 = Windows 95.
    '2 = Windows NT
    szCSDVersion As String * 128
End Type
 
 
Public Function GetProcesses(ByVal EXEName As String)
 
    Dim booResult As Boolean
    Dim lngLength As Long
    Dim lngProcessID As Long
    Dim strProcessName As String
    Dim lngSnapHwnd As Long
    Dim udtProcEntry As PROCESSENTRY32
    Dim lngCBSize As Long 'Specifies the size, In bytes, of the lpidProcess array
    Dim lngCBSizeReturned As Long 'Receives the number of bytes returned
    Dim lngNumElements As Long
    Dim lngProcessIDs() As Long
    Dim lngCBSize2 As Long
    Dim lngModules(1 To 200) As Long
    Dim lngReturn As Long
    Dim strModuleName As String
    Dim lngSize As Long
    Dim lngHwndProcess As Long
    Dim lngLoop As Long
    Dim b As Long
    Dim c As Long
    Dim e As Long
    Dim d As Long
    Dim pmc As PROCESS_MEMORY_COUNTERS
    Dim lret As Long
    Dim strProcName2 As String
    Dim strProcName As String
 
    'Turn on Error handler
    On Error GoTo Error_handler
 
   booResult = False
 
    EXEName = UCase$(Trim$(EXEName))
    lngLength = Len(EXEName)
 
    'ProcessInfo.bolRunning = False
 
    Select Case getVersion()
        'I'm not bothered about windows 95/98 becasue this class probably wont be used on it anyway.
        Case WIN95_System_Found 'Windows 95/98
 
        Case WINNT_System_Found 'Windows NT
 
            lngCBSize = 8 ' Really needs To be 16, but Loop will increment prior to calling API
            lngCBSizeReturned = 96
 
            Do While lngCBSize <= lngCBSizeReturned
                DoEvents
                'Increment Size
                lngCBSize = lngCBSize * 2
                'Allocate Memory for Array
                ReDim lngProcessIDs(lngCBSize / 4) As Long
                'Get Process ID's
                lngReturn = EnumProcesses(lngProcessIDs(1), lngCBSize, lngCBSizeReturned)
            Loop
 
            'Count number of processes returned
            lngNumElements = lngCBSizeReturned / 4
            'Loop thru each process
 
            For lngLoop = 1 To lngNumElements
            DoEvents
 
            'Get a handle to the Process and Open
            lngHwndProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lngProcessIDs(lngLoop))
 
            If lngHwndProcess <> 0 Then
                'Get an array of the module handles for the specified process
                lngReturn = EnumProcessModules(lngHwndProcess, lngModules(1), 200, lngCBSize2)
 
                'If the Module Array is retrieved, Get the ModuleFileName
                If lngReturn <> 0 Then
 
                    'Buffer with spaces first to allocate memory for byte array
                    strModuleName = Space(MAX_PATH)
 
                    'Must be set prior to calling API
                    lngSize = 500
 
                    'Get Process Name
                    lngReturn = GetModuleFileNameExA(lngHwndProcess, lngModules(1), strModuleName, lngSize)
 
                    'Remove trailing spaces
                    strProcessName = Left(strModuleName, lngReturn)
 
                    'Check for Matching Upper case result
                    strProcessName = UCase$(Trim$(strProcessName))
 
                    strProcName2 = GetElement(Trim(Replace(strProcessName, Chr$(0), "")), "\", 0, 0, GetNumElements(Trim(Replace(strProcessName, Chr$(0), "")), "\") - 1)
 
                    If strProcName2 = EXEName Then
 
                        'Get the Site of the Memory Structure
                        pmc.cb = LenB(pmc)
 
                           lret = GetProcessMemoryInfo(lngHwndProcess, pmc, pmc.cb)
 
                            Debug.Print EXEName & "::" & CStr(pmc.WorkingSetSize / 1024)
 
                    End If
                End If
            End If
            'Close the handle to this process
            lngReturn = CloseHandle(lngHwndProcess)
            DoEvents
        Next
 
    End Select
 
IsProcessRunning_Exit:
 
'Exit early to avoid error handler
Exit Function
Error_handler:
    Err.Raise Err, Err.Source, "ProcessInfo", Error
    Resume Next
End Function
 
 
Private Function getVersion() As Long
 
    Dim osinfo As OSVERSIONINFO
    Dim retvalue As Integer
 
    osinfo.dwOSVersionInfoSize = 148
    osinfo.szCSDVersion = Space$(128)
    retvalue = GetVersionExA(osinfo)
    getVersion = osinfo.dwPlatformId
 
End Function
 
 
Private Function StrZToStr(s As String) As String
    StrZToStr = Left$(s, Len(s) - 1)
End Function
 
 
 
Public Function GetElement(ByVal strList As String, ByVal strDelimiter As String, ByVal lngNumColumns As Long, ByVal lngRow As Long, ByVal lngColumn As Long) As String
 
    Dim lngCounter As Long
 
    ' Append delimiter text to the end of the list as a terminator.
    strList = strList & strDelimiter
 
    ' Calculate the offset for the item required based on the number of columns the list
    ' 'strList' has i.e. 'lngNumColumns' and from which row the element is to be
    ' selected i.e. 'lngRow'.
    lngColumn = IIf(lngRow = 0, lngColumn, (lngRow * lngNumColumns) + lngColumn)
 
    ' Search for the 'lngColumn' item from the list 'strList'.
    For lngCounter = 0 To lngColumn - 1
 
        ' Remove each item from the list.
        strList = Mid$(strList, InStr(strList, strDelimiter) + Len(strDelimiter), Len(strList))
 
        ' If list becomes empty before 'lngColumn' is found then just
        ' return an empty string.
        If Len(strList) = 0 Then
            GetElement = ""
            Exit Function
        End If
 
    Next lngCounter
 
    ' Return the sought list element.
    GetElement = Left$(strList, InStr(strList, strDelimiter) - 1)
 
End Function
 
 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Function GetNumElements (ByVal strList As String,
'                         ByVal strDelimiter As String)
'                         As Integer
'
'  strList      = The element list.
'  strDelimiter = The delimiter by which the elements in
'                 'strList' are seperated.
'
'  The function returns an integer which is the count of the
'  number of elements in 'strList'.
'
'  Author: Roger Taylor
'
'  Date:26/12/1998
'
'  Additional Information:
'
'  Revision History:
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
Public Function GetNumElements(ByVal strList As String, ByVal strDelimiter As String) As Integer
 
    Dim intElementCount As Integer
 
    ' If no elements in the list 'strList' then just return 0.
    If Len(strList) = 0 Then
        GetNumElements = 0
        Exit Function
    End If
 
    ' Append delimiter text to the end of the list as a terminator.
    strList = strList & strDelimiter
 
    ' Count the number of elements in 'strlist'
    While InStr(strList, strDelimiter) > 0
        intElementCount = intElementCount + 1
        strList = Mid$(strList, InStr(strList, strDelimiter) + 1, Len(strList))
    Wend
 
    ' Return the number of elements in 'strList'.
    GetNumElements = intElementCount
 
End Function
 
 
'If you're using VB4 or VB5, uncomment the following function:
'Function Replace(sInput As String, WhatToReplace As String, ReplaceWith As String) As String
    'Dim Ret As Long
    'Replace = sInput
    'Ret = -Len(ReplaceWith) + 1
    'Do
        'Ret = InStr(Ret + Len(ReplaceWith), Replace, WhatToReplace, vbTextCompare)
        'If Ret = 0 Then Exit Do
        'Replace = Left$(Replace, Ret - 1) + ReplaceWith + Right$(Replace, Len(Replace) - Ret - Len(WhatToReplace) + 1)
    'Loop
'End Function
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:

Select allOpen in new window

 

by: PhilChapmanJrPosted on 2009-10-09 at 06:24:39ID: 25534670

The following code returns the title bar and handel of each application that is running.  If you know what is displayed in the application title bar you can loop throught the applications until the desired application if found or all applications have ben reviewed.

' ------------------ Place the code below in the General Section
Option Explicit

Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function GetParent Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Const gw_hwndnext = 2

' --------------------------------------------------------------------------------
Place a command button on the form and place the code below in the click event
Private Sub Command1_Click()

Dim hwnd As Long
Dim nret As Long
Dim titletmp As String
Dim sTitleBar As String
   
hwnd = FindWindow(0&, 0&)
Do Until hwnd = 0
    If GetParent(hwnd) = 0 Then
        titletmp = Space(256)
        nret = GetWindowText(hwnd, titletmp, Len(titletmp))
        If titletmp > " " Then MsgBox nret & vbCrLf & titletmp
    End If
    hwnd = GetWindow(hwnd, gw_hwndnext)
Loop
       
End Sub






20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...