Solved

GetClassName API

Posted on 1998-01-28
3
560 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
Comment Utility
What exactly you want to do with the class name?
0
 

Accepted Solution

by:
grimhosp earned 50 total points
Comment Utility
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
Comment Utility
Thanks a lot.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now