使用API 函数 GetWindowModuleFileNameThe GetWindowModuleFileName function retrieves the full path and file name of the module associated with the specified window handle. UINT WINAPI GetWindowModuleFileName(
HWND hwnd, // handle to window
LPTSTR lpszFileName, // file name buffer
UINT cchFileNameMax // max size of file name buffer
);
Parameters
hwnd
[in] Handle to the window whose module file name will be retrieved.
lpszFileName
[out] Pointer to a buffer that receives the path and file name.
cchFileNameMax
[in] Specifies the maximum number of TCHARs that can be copied into the lpszFileName buffer.
Return Values
The return value is the total number of TCHARs copied into the buffer. Requirements
Windows NT/2000: Requires Windows NT 4.0 SP3 or later.
Windows 95/98: Requires Windows 98.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.
要分
HWND hwnd, // handle to window
LPTSTR lpszFileName, // file name buffer
UINT cchFileNameMax // max size of file name buffer
);
Parameters
hwnd
[in] Handle to the window whose module file name will be retrieved.
lpszFileName
[out] Pointer to a buffer that receives the path and file name.
cchFileNameMax
[in] Specifies the maximum number of TCHARs that can be copied into the lpszFileName buffer.
Return Values
The return value is the total number of TCHARs copied into the buffer. Requirements
Windows NT/2000: Requires Windows NT 4.0 SP3 or later.
Windows 95/98: Requires Windows 98.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.
要分
(ByVal hwnd As Long, ByVal szName As Any, ByVal cchLen As Long) As Long
Private Sub Form_Load()
Dim t As String
t = Space(300)
GetWindowModuleFileName Me.hwnd, t, 260
t = Left(t, InStr(t, Chr(0)) - 1)
Debug.Print t
End Sub
Option Explicit'ÒÔÏÂΪ»ñÈ¡²Ù×÷ϵͳ°æ±¾ÉùÃ÷
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long 'Specifies the length, in bytes, of the structure.
dwMajorVersion As Long 'Major Version Number
dwMinorVersion As Long 'Minor Version Number
dwBuildNumber As Long 'Build Version Number
dwPlatformId As Long 'Operating System Running, see below
szCSDVersion As String * 128 'Windows NT: Contains a null-terminated string,
'such as "Service Pack 3", that indicates the latest
'Service Pack installed on the system.
'If no Service Pack has been installed, the string is empty.
'Windows 95: Contains a null-terminated string that provides
'arbitrary additional information about the operating system
End TypePrivate Const hNull = 0' dwPlatformId defines:
Public Enum enuOSVersion
VER_PLATFORM_WIN32s = 0 'Win32s on Windows 3.1.
VER_PLATFORM_WIN32_WINDOWS = 1 'Win32 on Windows 95 or Windows 98.
'For Windows 95, dwMinorVersion is 0.
'For Windows 98, dwMinorVersion is 1.
VER_PLATFORM_WIN32_NT = 2 'Win32 on Windows NT.
End EnumPrivate Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As IntegerPrivate Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long'ÒÔÏÂΪWinNTº¯ÊýÉùÃ÷
Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_VM_READ = 16
Private Const MAX_PATH = 260Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As LongPrivate Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As LongPrivate Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
'ÒÔÏÂΪWin95º¯ÊýÉùÃ÷
Private Const TH32CS_SNAPPROCESS = &H2&Private Type PROCESSENTRY32
dwSize As Long 'Specifies the length, in bytes, of the structure.
cntUsage As Long 'Number of references to the process.
th32ProcessID As Long 'Identifier of the process.
th32DefaultHeapID As Long 'Identifier of the default heap for the process.
th32ModuleID As Long 'Module identifier of the process. (Associated exe)
cntThreads As Long 'Number of execution threads started by the process.
th32ParentProcessID As Long 'Identifier of the process that created the process being examined.
pcPriClassBase As Long 'Base priority of any threads created by this process.
dwFlags As Long 'Reserved; do not use.
szExeFile As String * MAX_PATH 'Path and filename of the executable file for the process.
End TypePrivate Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As LongPrivate Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long
'»ñÈ¡²Ù×÷ϵͳ°æ±¾
Public Function GetOSVersion(Optional sInfo As String) As enuOSVersion
'=======================================
'Returns the Operating System being used
'1 = Windows 95 / Windows 98
'2 = Windows NT
'=======================================
Dim osinfo As OSVERSIONINFO
Dim retvalue As Integer
With osinfo
.dwOSVersionInfoSize = 148
.szCSDVersion = Space$(128)
retvalue = GetVersionExA(osinfo)
GetOSVersion = .dwPlatformId
sInfo = "Version " & .dwMajorVersion & "." & .dwMinorVersion & " (Build " & .dwBuildNumber & ": " & StrZToStr(.szCSDVersion) & ")"
End With
End FunctionPublic Function GetRunApp(ByVal lHWnd As Long) As String
Select Case GetOSVersion()
Case VER_PLATFORM_WIN32_WINDOWS
GetRunApp = GetRunApp95(lHWnd)
Case VER_PLATFORM_WIN32_NT
GetRunApp = GetRunAppNT(lHWnd)
End Select
End Function'ÒÔÏÂΪWin95º¯Êý
Private Function StrZToStr(s As String) As String
If InStr(1, s, Chr$(0)) > 0 Then
StrZToStr = Left$(s, InStr(1, s, Chr$(0)) - 1)
Else
StrZToStr = s
End If
End FunctionPrivate Function GetRunApp95(ByVal lHWnd As Long) As String
On Error Resume Next
Dim lReturnID As Long
Dim hSnapProcess As Long
Dim proc As PROCESSENTRY32
Dim hCurProcess As Long
Dim sModName As String
GetWindowThreadProcessId lHWnd, hCurProcess
hSnapProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If hSnapProcess = hNull Then
GetRunApp95 = ""
Else
proc.dwSize = Len(proc)
lReturnID = Process32First(hSnapProcess, proc)
GetRunApp95 = ""
Do While lReturnID
sModName = UCase(StrZToStr(proc.szExeFile))
If proc.th32ProcessID = hCurProcess Then
GetRunApp95 = sModName
Exit Do
End If
lReturnID = Process32Next(hSnapProcess, proc)
Loop
CloseHandle hSnapProcess
End If
End Function'ÒÔÏÂΪWinNTº¯Êý
Private Function GetRunAppNT(ByVal lHWnd As Long) As String Dim cbNeeded As Long
Dim Modules(1 To 200) As Long
Dim lRet As Long
Dim nSize As Long
Dim hProcess As Long
Dim hCurProcess As Long
Dim sModName As String
On Error Resume Next
GetWindowThreadProcessId lHWnd, hCurProcess
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, hCurProcess)
If hProcess Then
lRet = EnumProcessModules(hProcess, Modules(1), 200, cbNeeded)
If lRet <> 0 Then
sModName = Space(MAX_PATH)
nSize = 500
lRet = GetModuleFileNameExA(hProcess, Modules(1), sModName, nSize)
sModName = Left$(sModName, lRet)
GetRunAppNT = sModName
End If
Else
GetRunAppNT = ""
End If
lRet = CloseHandle(hProcess)
End Functionform里的函数Function GetEN(hWnd As Long) As String
Dim c As New CApp
GetEN = c.GetRunApp(hWnd)
End Function