立即注册 登录
VB爱好者乐园(VBGood) 返回首页

新林的个人空间 http://blog.vbgood.com/?76641 [收藏] [复制] [分享] [RSS]

日志

获取记事本的完整路径,用的是进程pid[转贴]

已有 2037 次阅读2009-8-9 03:42 |个人分类:编程转载|

'**************************************************************************
'**模 块 名:工程1 - Form1
'**说 明:魔灵圣域   by icecept(郭卫)
'**创 建 人:icecept(魔灵)
'**日 期:2009-02-15 01:10:29
'**修 改 人:icecept(魔灵)
'**版 本:V1.0.0
'**E-mail   :icecept@163.com QQ:543375508
'**网 址:http://hi.baidu.com/icecept    http://hi.csdn.net/icecept
'*************************************************************************
Option Explicit
Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Private 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 CloseHandle Lib "kernel32.dll" (ByVal hObject 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
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Sub Command1_Click()
   On Error Resume Next
Dim s_hwnd As Long, i As Long, pid As Long
Dim s_path As String
Shell "notepad.exe", vbNormalFocus
s_hwnd& = FindWindow("notepad", vbNullString) 'Hwnd
i = GetWindowThreadProcessId(s_hwnd, pid) 'Pid
s_path = GetProcessPathByProcessID(pid)    '获取程序全路径
Text1.Text = s_path
End Sub
'获取应用程序的完整路径
Private Function GetProcessPathByProcessID(pid As Long) As String
    On Error GoTo ErrLine
    Dim cbNeeded As Long
    Dim szBuf(1 To 250) As Long
    Dim Ret As Long
    Dim szPathName As String
    Dim nSize As Long
    Dim hProcess As Long
    hProcess = OpenProcess(&H400 Or &H10, 0, pid)
    If hProcess <> 0 Then
       Ret = EnumProcessModules(hProcess, szBuf(1), 250, cbNeeded)
       If Ret <> 0 Then
         szPathName = Space(260)
         nSize = 500
         Ret = GetModuleFileNameExA(hProcess, szBuf(1), szPathName, nSize)
         GetProcessPathByProcessID = Left(szPathName, Ret)
       End If
    End If
    Ret = CloseHandle(hProcess)
    If GetProcessPathByProcessID = "" Then
       GetProcessPathByProcessID = "SYSTEM   "
    End If
ErrLine:
End Function

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

文字版|手机版|小黑屋|VBGood  

GMT+8, 2022-12-7 07:21

VB爱好者乐园(VBGood)
返回顶部