Solved

GetClassName API

Posted on 1998-01-28
3
579 Views
Last Modified: 2006-11-17
I wish to use an API call to find out an object's class name. Here's the code I used:
----------
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private Declare Function ChildWindowFromPoint Lib "user32" (ByVal hwnd As Long, ByVal xPoint As Long, ByVal yPoint As Long) As Long

dim lngHwnd as Long
Dim classname As String
lngHwnd = ChildWindowFromPoint(Me.hwnd, 10, 10)
GetClassName(lngHwnd, classname, 50)
----------
However, every time 0 is return by GetClassName and classname does not change to the class's name.
I've tried changing:
   ByVal lpClassName As String
to
   ByRef lpClassName As String
in the declaration of GetClassName. But the program crash.

What should I do?
0
Comment
Question by:leeal
3 Comments
 
LVL 2

Expert Comment

by:shogi
ID: 1453988
What exactly you want to do with the class name?
0
 

Accepted Solution

by:
grimhosp earned 50 total points
ID: 1453989
Hi,

I've taken a look at your code and got it to return the classname with no problems.  I did this by doing the following.

Firstly you need to make sure that the 'classname' variable which is used in the GetClassName call is preset to the correct size.  This can be achieved by placing the command 'classname = space$(51)' directly after the dim statement.  The size of 51 is just the last property of the API call +1.

Secondly you need to pass the return value from the API call back to a variable so instead of just doing 'GetClassName(lngHwnd,classname,50)' you need to use 'l% = GetClassName(lngHwnd,classname,50)'.

So the final code will read:

----------------------------------------------------------------

 Dim lngHwnd As Long
 Dim classname As String
 classname = Space$(51)

 lngHwnd = ChildWindowFromPoint(Me.hWnd, 10, 10)
 l% = GetClassName(lngHwnd, classname, 50)

 MsgBox classname

----------------------------------------------------------------

Running the above code should result in the classname for the specified window being displayed.

Hope this helps

Pete

P.S. The value returned to l% is the length of the classname string.
0
 
LVL 1

Author Comment

by:leeal
ID: 1453990
Thanks a lot.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question