Api seams not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yves

    #1

    Api seams not working

    Hello,
    Using VB6, i am trying to get the module filename from the cursor positioned
    on any windows, with the following code (modified from Article ID: Q112649
    in MSDN LIBRARY for Visual Studio 6.0A)

    Call GetCursorPos(pt 32) ' Get cursor position
    ptx = pt32.x
    pty = pt32.y
    hWndOver = WindowFromPoint XY(ptx, pty) ' Get window cursor is over
    If hWndOver <> hWndLast Then ' If changed update display
    hWndLast = hWndOver ' Save change
    Cls ' Clear the form
    Print "hWndOver: &H"; hex(hWndOver)
    ' Get window instance:
    hInstance = GetWindowLong(h WndOver, GWW_HINSTANCE)
    Print "hinstance: &H"; hex(hInstance)
    ' Get module file name:
    r = GetModuleFileNa me(hInstance, sModuleFileName , 100)
    Print "Module: "; Left(sModuleFil eName, r)
    End If


    The hWndOver change depending the cursor position, but the hInstance is not
    updated correctly , and consequently, the sModuleFileName is not correct.
    The initial API was GetWindowWord, but MSDN say that it is replaced by
    GetWindowLong (anyway it does not work also with GetWindowWord)
    I am using VB6 and Windows XP. I tried VB5 and Windows 98SE with the same
    problem.
    Any idea ?? suggestion ??
    Many thanks for your help
    Yves









  • Randy Birch

    #2
    Re: Api seams not working

    As you stated you modified the code in the KB article, ensure you did not
    change the declaration

    Dim sModuleFileName As String * 100

    to

    Dim sModuleFileName As String

    If you did, sModuleFileName must be pre-padded with spaces for the call to
    succeed (and ditto for the other four strings in that demo defined as
    fixed-length strings) ...

    const MAX_PATH = 260

    ' Get module file name:
    sModuleFileName = space$(MAX_PATH )
    r = GetModuleFileNa me(hInstance, sModuleFileName , MAX_PATH)
    if r > 0 then
    Print "Module: "; Left(sModuleFil eName, r)
    end if

    --

    Randy Birch
    MVP Visual Basic

    Please respond only to the newsgroups so all can benefit.

    There's no place like 127.0.0.1


    "Yves" <yves.martin17. nospam@libertys urf.fr> wrote in message
    news:bup53u$ddr $1@news.tiscali .fr...
    : Hello,
    : Using VB6, i am trying to get the module filename from the cursor
    positioned
    : on any windows, with the following code (modified from Article ID: Q112649
    : in MSDN LIBRARY for Visual Studio 6.0A)
    :
    : Call GetCursorPos(pt 32) ' Get cursor position
    : ptx = pt32.x
    : pty = pt32.y
    : hWndOver = WindowFromPoint XY(ptx, pty) ' Get window cursor is
    over
    : If hWndOver <> hWndLast Then ' If changed update
    display
    : hWndLast = hWndOver ' Save change
    : Cls ' Clear the form
    : Print "hWndOver: &H"; hex(hWndOver)
    : ' Get window instance:
    : hInstance = GetWindowLong(h WndOver, GWW_HINSTANCE)
    : Print "hinstance: &H"; hex(hInstance)
    : ' Get module file name:
    : r = GetModuleFileNa me(hInstance, sModuleFileName , 100)
    : Print "Module: "; Left(sModuleFil eName, r)
    : End If
    :
    :
    : The hWndOver change depending the cursor position, but the hInstance is
    not
    : updated correctly , and consequently, the sModuleFileName is not correct.
    : The initial API was GetWindowWord, but MSDN say that it is replaced by
    : GetWindowLong (anyway it does not work also with GetWindowWord)
    : I am using VB6 and Windows XP. I tried VB5 and Windows 98SE with the same
    : problem.
    : Any idea ?? suggestion ??
    : Many thanks for your help
    : Yves
    :
    :
    :
    :
    :
    :
    :
    :
    :


    Comment

    • Yves

      #3
      Re: Api seams not working

      Hello Randy,
      Nice to read you.
      The string was declared without change: Dim sModuleFileName As String * 100.
      I tried to init the string to 260 spaces as you indicated without any
      success.
      Most of the time, the "hInstance" is equal to &H0 or to &H400000, giving an
      error to the GetModuleFileNa me API returning the caller module name (this
      code) or nothing (r=0).
      Here is the complete small code:

      Private Type POINTAPI
      x As Long
      y As Long
      End Type
      Private Declare Function GetCursorPos Lib "user32" (lpPoint As _
      POINTAPI) As Long
      Private Declare Function WindowFromPoint XY Lib "user32" Alias _
      "WindowFromPoin t" (ByVal xPoint As Long, ByVal yPoint As Long) _
      As Long
      Private Declare Function GetModuleFileNa me Lib "kernel32" Alias _
      "GetModuleFileN ameA" (ByVal hModule As Long, ByVal lpFileName As _
      String, ByVal nSize As Long) As Long
      Private Declare Function GetWindowLong Lib "user32" Alias _
      "GetWindowLongA " (ByVal hwnd As Long, ByVal nIndex As Long) As _
      Long
      Const GWW_HINSTANCE = (-6)
      Const MAX_PATH = 260

      Private Sub Timer1_Timer()
      Dim pt32 As POINTAPI
      Dim ptx As Long
      Dim pty As Long
      Dim hWndOver As Long
      Dim hInstance As Long
      Dim sModuleFileName As String
      Static hWndLast As Long

      Call GetCursorPos(pt 32) ' Get cursor position
      ptx = pt32.x
      pty = pt32.y
      hWndOver = WindowFromPoint XY(ptx, pty) ' Get window cursor is over
      If hWndOver <> hWndLast Then ' If changed update display
      hWndLast = hWndOver ' Save change
      Cls ' Clear the form
      ' Get window instance:
      hInstance = GetWindowLong(h WndOver, GWW_HINSTANCE)
      Print "hwndOver: &H"; Hex(hWndOver)
      Print "hinstance: &H"; Hex(hInstance)
      ' Get module file name:
      sModuleFileName = Space$(MAX_PATH )
      r = GetModuleFileNa me(hInstance, sModuleFileName , MAX_PATH)
      Print "r: "; r
      If r > 0 Then
      Print "Module: "; Left(sModuleFil eName, r)
      End If
      End If

      End Sub

      Many thanks for helping me.
      Yves



      "Randy Birch" <rgb_removethis @mvps.org> a écrit dans le message de
      news:T__Pb.6863 0$lGr.1803@twis ter01.bloor.is. net.cable.roger s.com...[color=blue]
      > As you stated you modified the code in the KB article, ensure you did not
      > change the declaration
      >
      > Dim sModuleFileName As String * 100
      >
      > to
      >
      > Dim sModuleFileName As String
      >
      > If you did, sModuleFileName must be pre-padded with spaces for the call to
      > succeed (and ditto for the other four strings in that demo defined as
      > fixed-length strings) ...
      >
      > const MAX_PATH = 260
      >
      > ' Get module file name:
      > sModuleFileName = space$(MAX_PATH )
      > r = GetModuleFileNa me(hInstance, sModuleFileName , MAX_PATH)
      > if r > 0 then
      > Print "Module: "; Left(sModuleFil eName, r)
      > end if
      >
      > --
      >
      > Randy Birch
      > MVP Visual Basic
      > http://vbnet.mvps.org/
      > Please respond only to the newsgroups so all can benefit.
      >
      > There's no place like 127.0.0.1
      >
      >
      > "Yves" <yves.martin17. nospam@libertys urf.fr> wrote in message
      > news:bup53u$ddr $1@news.tiscali .fr...
      > : Hello,
      > : Using VB6, i am trying to get the module filename from the cursor
      > positioned
      > : on any windows, with the following code (modified from Article ID:[/color]
      Q112649[color=blue]
      > : in MSDN LIBRARY for Visual Studio 6.0A)
      > :
      > : Call GetCursorPos(pt 32) ' Get cursor position
      > : ptx = pt32.x
      > : pty = pt32.y
      > : hWndOver = WindowFromPoint XY(ptx, pty) ' Get window cursor is
      > over
      > : If hWndOver <> hWndLast Then ' If changed update
      > display
      > : hWndLast = hWndOver ' Save change
      > : Cls ' Clear the form
      > : Print "hWndOver: &H"; hex(hWndOver)
      > : ' Get window instance:
      > : hInstance = GetWindowLong(h WndOver, GWW_HINSTANCE)
      > : Print "hinstance: &H"; hex(hInstance)
      > : ' Get module file name:
      > : r = GetModuleFileNa me(hInstance, sModuleFileName , 100)
      > : Print "Module: "; Left(sModuleFil eName, r)
      > : End If
      > :
      > :
      > : The hWndOver change depending the cursor position, but the hInstance is
      > not
      > : updated correctly , and consequently, the sModuleFileName is not[/color]
      correct.[color=blue]
      > : The initial API was GetWindowWord, but MSDN say that it is replaced by
      > : GetWindowLong (anyway it does not work also with GetWindowWord)
      > : I am using VB6 and Windows XP. I tried VB5 and Windows 98SE with the[/color]
      same[color=blue]
      > : problem.
      > : Any idea ?? suggestion ??
      > : Many thanks for your help
      > : Yves[/color]





      Comment

      Working...