⑴ 求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