⑴ 求VB取句柄对应全路径文件名模块
这个“句柄”指的是实例句柄hInstance吧...
用这个API:
Declare Function GetMoleFileName Lib "kernel32" Alias "GetMoleFileNameA" (ByVal hMole As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
返回值Long,如执行成功,返回复制到lpFileName的实际字符数量;失败时会返回0,并设置LastError
参数
hMole 一个模块的句柄。可以是一个DLL模块,或者是一个应用程序的实例句柄
lpFileName 指定一个字串缓冲区,要在其中容纳文件的用NULL字符中止的路径名,hMole模块就是从这个文件装载进来的
nSize 装载到缓冲区lpFileName的最大字符数量
例如:
Dim msg As String * 1024
msg = ""
GetMoleFileName App.hInstance, msg, 1024
MsgBox msg
则得到当前运行的路径+文件名.~
----------------------------------------------------------------------------------------
终于完成了- -```
用到这几个API:
GetWindowThreadProcessId()
-- 根据hWnd获取进程ID(PID)
OpenProcess()
--打开进程..返回值是进程句柄.
CloseHandle()
EnumProcessMoles()
--枚举进程模块(第一个模块就是第一个模块就是创建此进程的主exe程序
GetMoleFileNameExA()
取得模块的文件名.~
API函数及常量声明如下(去MSDN中找...API浏览器里好多都没有的......):
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function EnumProcessMoles Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphMole As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetMoleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hMole As Long, ByVal MoleName As String, ByVal nSize As Long) As Long
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const SYNCHRONIZE = &H100000
Private Const PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED + SYNCHRONIZE + &HFFF
下面这个函数的返回值就是根据hWnd取得的文件名:
Private Function getName(ByVal hHWND As Long) As String
Dim pID As Long
Dim hProcess As Long
Dim Moles(200) As Long
Dim nSize As Integer
Dim lRet As Long
Dim MoleName As String
Dim cb As Long
GetWindowThreadProcessId hHWND, pID
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, pID)
lRet = EnumProcessMoles(hProcess, Moles(1), 200, cb)
If lRet <> 0 Then
MoleName = Space(255)
nSize = 255
lRet = GetMoleFileNameExA(hProcess, Moles(1), MoleName, nSize)
End If
CloseHandle hProcess
End Function
..........Q:531623560