Link to home
Start Free TrialLog in
Avatar of khacharn
khacharn

asked on

has somebody any idea on how to use GetWindowModuleFileName

hi all
i have a simple problem...
i have the code to know the active window on the desktop...
my problem is how to get the filepath of the file open in that window...
i tried using GetWindowModuleFileName() but it is not giving any output...
please help me...as soon as possible..
points will not be aa constraint if somebody help me solve the problem as soon as possible..
khacharn
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Private Declare Function GetWindowModuleFileName Lib "user32" Alias "GetWindowModuleFileNameA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Sub Command1_Click()
  Dim s As String
  Dim ret As Long
  s = String(260, 0)
  ret = GetWindowModuleFileName(hWnd, s, 259)
  s = Left(s, ret)
  Debug.Print s
End Sub

Cheers
Private Declare Function GetWindowModuleFileName Lib "user32" Alias "GetWindowModuleFileNameA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Function FileNameFromHandle(ByVal hWnd As Long) As String
  Dim s As String
  Dim ret As Long
  s = String(260, 0)
  ret = GetWindowModuleFileName(hWnd, s, 259)
  FileNameFromHandle = Left(s, ret)
End Function

'===Using===

Private Sub Command1_Click()
  Debug.Print FileNameFromHandle(hWnd)
End Sub

Cheers
Avatar of khacharn
khacharn

ASKER

both the codes are not working
there is no path being returned...
it gives empty string...
please help soon
khacharn
Works fine for me. Return path to VB50.exe

Which OS do you use?

Cheers
both the codes are not working
there is no path being returned...
it gives empty string...
please help soon
khacharn
i am using WinNT..
donno why is it not working...
but hey this not what i want...
let me explain...
i want to know the filepath of the file which is opened ..
say if my current active application is WORD and i have a file opened from c:\cheeku\2.doc then i need the code which will return me the path above...
i hope i am clear..
please help soon on this...
khacharn
Hi
If you're using NT/2000, these functions won't work (see http://support.microsoft.com/support/kb/articles/Q228/4/69.asp). You have to use psapi.dll instead.
bad news...but can i get my thing done some other way...?
please help
did you get exactly what i want to do ?
khacharn
Sorry, have no more time today, I'll send answer tomorrow.
In brief - you need following API:
GetWindowThreadProcessID to get process pid.
OpenProcess - to get hProcess

Public Declare Function EnumProcessModules Lib "psapi.dll" _
   (ByVal hProcess As Long, ByRef lphModule As Long, _
      ByVal cb As Long, ByRef cbNeeded As Long) As Long

to enum, say, first 1 module in process


Public Declare Function GetModuleFileNameExA Lib "psapi.dll" _
   (ByVal hProcess As Long, ByVal hModule As Long, _
      ByVal ModuleName As String, ByVal nSize As Long) As Long

to get exe name

Cheers
ARK Hi..
i think you are not getting the requirements...
my problem is that i need to finf out the ACTIVE application....it can be WORD/EXCEL/ACROBAT READER...
after this i just need to know the path of the file open the the active application....i don't need the path of the application I NEED THE PATH OF THE ACTIVE DOCUMENT...
i think you must now have a a better idea of what i want..
please help soon
khacharn
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial