Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function OpenProcess Lib "KERNEL32.DLL" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId 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 Function GetModuleBaseName Lib "PSAPI.DLL" Alias "GetModuleBaseNameA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId 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 Handle As Long) As Long
Private Const MAX_PATH = 260
Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_VM_READ = 16
Private Sub Command1_Click()
Dim hWnd As Long
Dim Path As String, Name As String
hWnd = FindWindow(vbNullString, "Untitled - Notepad")
If hWnd = 0 Then
MsgBox "Notepad is not opened!"
Exit Sub
End If
Path = ExePath(hWnd)
Debug.Print Path, "|||"
End Sub
Public Function ExePath(ByVal hWnd As Long) As String
Dim ThreadID As Long, ProcessID As Long
Dim lngReturn As Long
Dim strEXEPath As String
Dim lngSize As Long
Dim hProcess As Long
Dim hMod(0 To 1023) As Long
Dim cbNeeded As Long
ThreadID = GetWindowThreadProcessId(hWnd, ProcessID)
lngSize = MAX_PATH
'Get a handle to the Process
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, ProcessID)
lngReturn = EnumProcessModules(hProcess, hMod(0), 1024, cbNeeded)
strEXEPath = String$(lngSize, 0)
lngReturn = GetModuleFileNameExA(hProcess, hMod(0), strEXEPath, lngSize)
strEXEPath = Left(strEXEPath, lngReturn)
lngReturn = CloseHandle(hProcess)
ExePath = strEXEPath
End Function
Public Function ExePath(ByVal hWnd As Long) As String
Dim szName As String
Dim ThreadID As Long
Dim ProcessID As Long
Dim hProcess As Long
Dim lenName As Integer
Dim DevicePath As String
Dim p As Integer
ThreadID = GetWindowThreadProcessId(hWnd, ProcessID)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, ProcessID)
szName = String(MAX_PATH, Chr(0))
lenName = GetProcessImageFileName(hProcess, szName, MAX_PATH)
p = InStr(1, szName, Chr(0))
DevicePath = Left$(szName, p - 1)
ExePath = ConvertDevicePathToStandardPath(DevicePath)
End Function
Private Function ConvertDevicePathToStandardPath(ByVal DevFilePath As String) As String
Dim ret As Long
Dim k As Integer
Dim strSave As String
Dim sl As String
Dim DevPath As String
Dim l As Long
Dim p As Integer
strSave = String(255, Chr$(0))
ret = GetLogicalDriveStrings(255, strSave)
For k = 1 To 100
If Left$(strSave, InStr(1, strSave, Chr$(0))) = Chr$(0) Then Exit For
sl = Left$(strSave, InStr(1, strSave, Chr$(0)) - 2)
DevPath = String(255, Chr$(0))
l = QueryDosDevice(sl, DevPath, 255)
DevPath = Left$(DevPath, InStr(1, DevPath, Chr$(0)) - 1)
p = InStr(1, DevFilePath, DevPath)
If p <> 0 Then
ConvertDevicePathToStandardPath = sl & Right(DevFilePath, Len(DevFilePath) - Len(DevPath))
Exit Function
End If
strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
Next k
End Function
Option Explicit
Private Const ProcessImageFileName = 27
Private Const STATUS_INFO_LENGTH_MISMATCH = &HC0000004
Private Const PROCESS_QUERY_INFORMATION = &H400&
Private Const PROCESS_VM_READ = &H10&
Private Const HEAP_ZERO_MEMORY = &H8&
Private Type UNICODE_STRING
Length As Integer
MaximumLength As Integer
Buffer As Long
End Type
Private Declare Function GetProcessHeap Lib "kernel32.dll" () As Long
Private Declare Function HeapAlloc Lib "kernel32.dll" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function HeapFree Lib "kernel32.dll" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal lpMem 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 NtQueryInformationProcess Lib "ntdll.dll" (ByVal ProcessHandle As Long, ByVal ProcessInformationClass As Long, ByVal ProcessInformation As Long, ByVal ProcessInformationLength As Long, ByRef ReturnLength As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Sub RtlMoveMemory Lib "kernel32.dll" (ByVal lpDest As Long, ByVal lpSource As Long, ByVal cbCopy As Long)
Public Function GetProcessNameByPid(ByVal pid As Long) As String
Dim uni As UNICODE_STRING
Dim Buffer As Long
Dim hProcess As Long
Dim FileName As String
Dim cbNeeded As Long
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, pid)
If hProcess = 0 Then
Exit Function
End If
' Get the buffer size needed for the call.
If NtQueryInformationProcess(hProcess, ProcessImageFileName, VarPtr(uni), 8, cbNeeded) = STATUS_INFO_LENGTH_MISMATCH Then
' Allocate the required buffer from the heap.
Buffer = HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, cbNeeded)
If NtQueryInformationProcess(hProcess, ProcessImageFileName, Buffer, cbNeeded, cbNeeded) = 0 Then
' UNICODE_STRING
RtlMoveMemory VarPtr(uni), ByVal Buffer&, Len(uni)
FileName = String$(uni.Length / 2, vbNullChar)
RtlMoveMemory StrPtr(FileName), ByVal uni.Buffer, uni.Length
GetProcessNameByPid = FileName
End If
HeapFree GetProcessHeap, 0, Buffer
End If
CloseHandle hProcess
End Function
Private Sub Command1_Click()
Debug.Print GetProcessNameByPid(1092)
End Sub
'ProcessSecurity.vb
'egl1044
Imports System.Runtime.InteropServices
Imports System.Security.AccessControl
Imports System.Security.Principal
Public NotInheritable Class ProcessSecurity
Inherits NativeObjectSecurity
Public Sub New(ByVal handle As SafeHandle)
MyBase.New(False, Security.AccessControl.ResourceType.KernelObject, handle, _
AccessControlSections.Access Or _
AccessControlSections.Group Or _
AccessControlSections.Owner)
End Sub
Public Overrides Function AccessRuleFactory(ByVal identityReference As IdentityReference, ByVal accessMask As Integer, ByVal isInherited As Boolean, ByVal inheritanceFlags As System.Security.AccessControl.InheritanceFlags, ByVal propagationFlags As System.Security.AccessControl.PropagationFlags, ByVal type As System.Security.AccessControl.AccessControlType) As System.Security.AccessControl.AccessRule
Return New ProcessAccessRule(identityReference, accessMask, type)
End Function
Public Overrides Function AuditRuleFactory(ByVal identityReference As IdentityReference, ByVal accessMask As Integer, ByVal isInherited As Boolean, ByVal inheritanceFlags As System.Security.AccessControl.InheritanceFlags, ByVal propagationFlags As System.Security.AccessControl.PropagationFlags, ByVal flags As System.Security.AccessControl.AuditFlags) As System.Security.AccessControl.AuditRule
Return Nothing
End Function
Public Overloads Sub AddAccessRule(ByVal rule As ProcessAccessRule)
MyBase.AddAccessRule(rule)
End Sub
Public Overloads Function RemoveAccessRule(ByVal rule As ProcessAccessRule) As Boolean
Return MyBase.RemoveAccessRule(rule)
End Function
Public Overloads Sub Persist(ByVal handle As SafeHandle)
WriteLock()
Try
Persist(handle, _
AccessControlSections.Access Or _
AccessControlSections.Group Or _
AccessControlSections.Owner)
Finally
WriteUnlock()
End Try
End Sub
Public Overrides ReadOnly Property AccessRightType As System.Type
Get
Return GetType(Integer)
End Get
End Property
Public Overrides ReadOnly Property AccessRuleType As System.Type
Get
Return GetType(ProcessAccessRule)
End Get
End Property
Public Overrides ReadOnly Property AuditRuleType As System.Type
Get
Return Nothing
End Get
End Property
End Class
Public NotInheritable Class ProcessAccessRule
Inherits AccessRule
Public Sub New(ByVal identity As IdentityReference, ByVal accessRights As ProcessRights, ByVal accessType As AccessControlType)
MyBase.New(identity, accessRights, False, InheritanceFlags.None, PropagationFlags.None, accessType)
End Sub
End Class
<Flags()>
Public Enum ProcessRights
'ProcessAllAccess = 0
ProcessCreateProcess = &H80
ProcessCreateThread = &H2
ProcessDupHandle = &H40
ProcessQueryInformation = &H400
ProcessQueryLimitedInformation = &H1000
ProcessSetInfromation = &H200
ProcessSetQuota = &H100
ProcessSuspendResume = &H800
ProcessTerminate = &H1
ProcessVmOperation = &H8
ProcessVmRead = &H10
ProcessVmWrite = &H20
ProcessSynchronize = &H100000
End Enum
[DllImport("user32.dll")]
public static extern IntPtr GetWindowThreadProcessId(I
void GetProcessPathFromWindowHa
{
uint pid = 0;
Win32.GetWindowThreadProce
Process p = Process.GetProcessById((in
return p.MainModule.FileName;
}