dcsimg
Results 1 to 4 of 4

Thread: Code

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    669

    Code

    Hi

    Wht the below code does

    Public Function GetWindowClassName() As String
    Dim strB(256) As Char

    Dim tmpLen As Integer
    tmpLen = GetClassName(hwnd, sClass, 255)
    GetWindowClassName = Left(sClass, tmpLen)
    End Function

    Thanks

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,091

    Re: Code

    It calls the GetClassName function (which you haven't shown), passing in parameters which you haven't shown the declaration/values of (hwnd and sClass) and stores the result to the variable tmpLen. It then returns the start of sClass up the the length tmpLen.

    I would assume that GetClassName is a Windows API function, and that the function you showed is supposed to get the class name of the window specified by the handle hwnd. There are a very large amount of class names, but one example would be "TextBox".


    However that code is written badly in several ways, and would be better like this:
    Code:
    Public Function GetWindowClassName(hwnd as IntPtr) As String
       Dim sClass As String
       Dim tmpLen As Integer
    
       tmpLen = GetClassName(hwnd, sClass, 255)
       Return sClass.SubString(0, tmpLen)
    End Function
    (I've guessed at the data types)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Posts
    669

    Re: Code

    Hi

    I have below . For what purpose it is used.
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer

    Thanks

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,091

    Re: Code

    As I suspected, that is a declaration that refers to a Windows API function, and what the function does is also what I thought.

    "user32" is the DLL that contains the function (this is pre-installed on Windows, you should not attempt to distribute it), GetClassName is the name used in your code, and "GetClassNameA" is the name of the actual function inside the DLL. Because the name of the actual function ends in A (instead of W), it does not deal with extended characters (such as Japanese characters etc).

    A quick search shows that my assumed data types are correct, and the declaration you are using has a mistake... it should be: ByVal hwnd As IntPtr,

    Info about the GetClassNameA function (and links to the general concepts) is here:
    https://docs.microsoft.com/en-us/win...-getclassnamea

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width