LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 91
Default API Class Question

OK, so I'm pretty new to API's so forgive me if this is an idiot
question :-)

I'm putting together a class that uses API calls to user32 to
enumerate and list all running applications by their Title text. One
of the functions uses "AddressOf AnotherFunctionName" to accomplish
this. The code works fantastic in a module. However, when trying to
migrate this to a reusable class, the code crashes on that line with
an error of:

"Invalid use of AddressOf operator"

Looking in Help it says:
"You tried to use AddressOf with the name of a class method.
Only the names of Visual Basic procedures in a .bas module can be
modified with AddressOf. You can't specify a class method."

Well, shucks, that just popped my bubble of wanting to make this code
really efficient by keeping it in a class amongst some other utility
functions I've written. Seems I can only use it in a module instead.

My question is this: Is there a way to work around this in a class?
Here's my working module code that simply returns a Long greater than
zero if an app with all or part of the passed title is found to be
running:

==================================
Option Explicit

Private Declare Function EnumWindows Lib "user32" (ByVal lEnumFunc As
Long, ByVal wParam 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 Type AppWindow
sTitle As String
lHandle As Long
End Type

Public Function FindWindowByTitle(ByVal TheWindowTitle As String) As
Long
'We'll pass a custom structure in as the parameter to store our
result...
Dim tParms As AppWindow
tParms.sTitle = TheWindowTitle

Call EnumWindows(AddressOf GetWindowTitles, VarPtr(tParms))

FindWindowByTitle = tParms.lHandle
End Function

Private Function GetWindowTitles(ByVal TheHandle As Long, TheParms As
AppWindow) As Long
Dim sTitleText As String

'set a generic 260 length empty string to catch the window text
sTitleText = Space(260)
'get the text
Call GetWindowText(TheHandle, sTitleText, 260)
'remove nulls from the text
sTitleText = TrimNull(sTitleText)

'check to see if all or part of the search string is found
'in the window text
If sTitleText Like TheParms.sTitle Then
'if a match is found, then set the handle number
TheParms.lHandle = TheHandle
'and then exit the function
GetWindowTitles = 0
End If

'reset to 1 to keep the recursive loop going if not match found
GetWindowTitles = 1
End Function

Private Function TrimNull(ByVal TheText As String)
'check to see if string has null characters on the end
If Not InStr(TheText, Chr$(0)) = 0 Then
'if so, then remove the null characters from the end
TheText = Left$(TheText, InStr(TheText, Chr$(0)) - 1)
End If
TrimNull = TheText
End Function
=======================================

Of course the usage of this would be to pass a string to the only
public function FindWindowByTitle.

Thanks for any light that can be shed on this.

Cory

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Can't pass an Array to a class Charles Excel Programming 5 April 13th 07 06:06 PM
Class Module Question Andrew Yates Excel Programming 8 March 5th 06 03:50 PM
Class module question David Excel Programming 4 September 8th 05 04:51 PM
Class Question John T Ingato Excel Programming 2 November 7th 03 12:09 PM


All times are GMT +1. The time now is 06:51 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"