Results 1 to 7 of 7

Thread: Module Help...[resolved]

  1. #1

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Resolved Module Help...[resolved]

    Hey, thanks for stopping by, i have a slight problem with my module, i cannot get my forms to get the resulting string from it, how would i go about doing it ?

    Here is my Forms code -

    VB Code:
    1. Private Sub Command1_Click()
    2. Call getWinampWindow
    3. MsgBox NowPlayingStr
    4. End Sub

    And here is my Module code (Feel free to re-use this code if needbe, its fully fnctioning)
    VB Code:
    1. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Dim lHandle As Long
    4. Dim NowPlayingStr As String
    5.  
    6. Function getWinampWindow()
    7. lHandle = FindWindow("Winamp v1.x", vbNullString)
    8. If lHandle > 0 Then
    9.     Dim lRet As Long
    10.     Dim sTitle As String * 256
    11.     Dim sCaption As String
    12.     lRet = GetWindowText(lHandle, sTitle, Len(sTitle))
    13.     sCaption = Left(sTitle, InStr(1, sTitle, vbNullChar, vbTextCompare) - 10)
    14.     Do
    15.         If IsNumeric(Left(sCaption, 1)) = True Or Left(sCaption, 1) = "." Or Left(sCaption, 1) = " " Then
    16.         sCaption = Right(sCaption, Len(sCaption) - 1)
    17.         Else
    18.         Exit Do
    19.         End If
    20.     Loop
    21.     Do
    22.         If IsNumeric(Right(sCaption, 1)) = True Or Right(sCaption, 1) = ")" Or Right(sCaption, 1) = " " Or Right(sCaption, 1) = ":" Then
    23.         sCaption = Left(sCaption, Len(sCaption) - 1)
    24.         ElseIf Right(sCaption, 1) = "(" Then
    25.         sCaption = Left(sCaption, Len(sCaption) - 1)
    26.         Exit Do
    27.         Else
    28.         Exit Do
    29.         End If
    30.     Loop
    31.     NowPlayingStr = sCaption
    32. Else
    33.     NowPlayingStr = "Winamp 6 Not Found"
    34. End If
    35. End Function

    Any suggestions as to how to get this to work ?

    EDIT : Final Code in Last post
    Last edited by thegreatone; Apr 27th, 2005 at 12:44 PM.
    Zeegnahtuer?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Module Help...(i feel dumb....)

    Try changing Dim to Public.

    When declaring variables in an event, use Dim.
    When declaring variables at the form level, use Private
    When declaring variables at the module level, use Public

  3. #3

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: Module Help...(i feel dumb....)

    Ok, that didn't work, but i have found a workaround, by simpling making the MsgBox pop-up from the module itself... a little un-orthodox but still, it works.

    here is the code i am now using, anymore answers are welcome

    VB Code:
    1. 'Form1
    2. Private Sub Command1_Click()
    3. Call getWinampWindow
    4. End Sub

    and the module -
    VB Code:
    1. 'Module1
    2. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Public lHandle As Long
    5. Public NowPlayingStr As String
    6.  
    7. Function getWinampWindow()
    8. lHandle = FindWindow("Winamp v1.x", vbNullString)
    9. If lHandle > 0 Then
    10.     Dim lRet As Long
    11.     Dim sTitle As String * 256
    12.     Dim sCaption As String
    13.     lRet = GetWindowText(lHandle, sTitle, Len(sTitle))
    14.     sCaption = Left(sTitle, InStr(1, sTitle, vbNullChar, vbTextCompare) - 10)
    15.     Do
    16.         If IsNumeric(Left(sCaption, 1)) = True Or Left(sCaption, 1) = "." Or Left(sCaption, 1) = " " Then
    17.         sCaption = Right(sCaption, Len(sCaption) - 1)
    18.         Else
    19.         Exit Do
    20.         End If
    21.     Loop
    22.     Do
    23.         If IsNumeric(Right(sCaption, 1)) = True Or Right(sCaption, 1) = ")" Or Right(sCaption, 1) = " " Or Right(sCaption, 1) = ":" Then
    24.         sCaption = Left(sCaption, Len(sCaption) - 1)
    25.         ElseIf Right(sCaption, 1) = "(" Then
    26.         sCaption = Left(sCaption, Len(sCaption) - 1)
    27.         Exit Do
    28.         Else
    29.         Exit Do
    30.         End If
    31.     Loop
    32.     NowPlayingStr = sCaption
    33.     MsgBox NowPlayingStr
    34. Else
    35.     NowPlayingStr = "Winamp 6 Not Found"
    36.     MsgBox NowPlayingStr
    37. End If
    38. End Function
    Zeegnahtuer?

  4. #4
    Hyperactive Member Arachnid13's Avatar
    Join Date
    Jan 2003
    Location
    England
    Posts
    327

    Re: Module Help...(i feel dumb....)

    as the code you have in your module is a Funtion, it needs to return a value, but to do that you have to set the function to have a value within the function, so what you should do is get rid of the variable NowPlayingStr and wherever you mention it in the code (like NowPlayingStr = sCaption), replace it with getWinampWindow (so it would say getWinampWindow = sCaption) and then in your code instead of saying
    VB Code:
    1. Call getWinampWindow
    2. msgbox NowPlayingStr
    just use:
    VB Code:
    1. msgbox getWinampWindow()
    Do you wake up in the morning feeling sleepy and grumpy? Then you must be Snow White

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Module Help...(i feel dumb....)

    Quote Originally Posted by Arachnid13
    as the code you have in your module is a Funtion, it needs to return a value,
    Good point Arachnid13. I missed that.

    thegreatone: Change your function to Public Function getWinampWindow() As String and you should get your return string.

  6. #6

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: Module Help...(i feel dumb....)

    Finished, your both getting good rep. thanks so much.

    Final Code-

    VB Code:
    1. 'in a module
    2. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Public lHandle As Long
    5.  
    6. Function getWinampWindow()
    7. lHandle = FindWindow("Winamp v1.x", vbNullString)
    8. If lHandle > 0 Then
    9.     Dim lRet As Long
    10.     Dim sTitle As String * 256
    11.     Dim sCaption As String
    12.     lRet = GetWindowText(lHandle, sTitle, Len(sTitle))
    13.     sCaption = Left(sTitle, InStr(1, sTitle, vbNullChar, vbTextCompare) - 10)
    14.     Do
    15.         If IsNumeric(Left(sCaption, 1)) = True Or Left(sCaption, 1) = "." Or Left(sCaption, 1) = " " Then
    16.         sCaption = Right(sCaption, Len(sCaption) - 1)
    17.         Else
    18.         Exit Do
    19.         End If
    20.     Loop
    21.     Do
    22.         If IsNumeric(Right(sCaption, 1)) = True Or Right(sCaption, 1) = ")" Or Right(sCaption, 1) = " " Or Right(sCaption, 1) = ":" Then
    23.         sCaption = Left(sCaption, Len(sCaption) - 1)
    24.         ElseIf Right(sCaption, 1) = "(" Then
    25.         sCaption = Left(sCaption, Len(sCaption) - 1)
    26.         Exit Do
    27.         Else
    28.         Exit Do
    29.         End If
    30.     Loop
    31.     getWinampWindow = sCaption
    32. Else
    33.     getWinampWindow = "Winamp 6 Not Found"
    34. End If
    35. End Function

    It is simply accessed like this -
    VB Code:
    1. msgbox getWinampWindow

    Do you think this is worth posting in the codebank ?
    Zeegnahtuer?

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Module Help...(i feel dumb....)

    Quote Originally Posted by thegreatone
    Do you think this is worth posting in the codebank ?
    If you are going to do that, make sure you include a detailed explanation of what your code does, and how it does it.

Posting Permissions

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



Click Here to Expand Forum to Full Width