Otra Opcion:
Saludos
Código [Seleccionar]
Option Explicit
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwprocessid As Long) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'Abre un proceso para poder obtener el path ( Retorna el handle )
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
'Obtiene el nombre del proceso a partir de un handle
Private Declare Function GetModuleFileNameExA Lib "PSAPI.DLL" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As Long
' Cierra y libera el proceso abierto con OpenProcess
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Const PROCESS_VM_READ As Long = (&H10)
Private Const PROCESS_QUERY_INFORMATION As Long = (&H400)
Function ProcIDFromWnd(ByVal hwnd As Long) As Long
Dim idProc As Long:
'crea PID de un HWnd
GetWindowThreadProcessId hwnd, idProc
'retorno del PID
ProcIDFromWnd = idProc
End Function
Private Sub Command1_Click()
Shell "calc"
Dim Handle_Proceso As Long: Dim Buffer As String
Dim ret As Long: Dim Ruta As String
Dim Handle As Long: Handle = FindWindow("SciCalc", vbNullString)
'MsgBox ProcIDFromWnd(Handle)
Handle_Proceso = OpenProcess(PROCESS_QUERY_INFORMATION + PROCESS_VM_READ, 0, ProcIDFromWnd(Handle))
If Handle_Proceso <> 0 Then
Buffer = Space(255) ' Crea un buffer para almacenar el nombre y ruta
ret = GetModuleFileNameExA(Handle_Proceso, 0, Buffer, 255) ' Le pasa el Buffer al Api y el Handle
Ruta = Left(Buffer, ret) ' Le elimina los espacios nulos a la cadena devuelta
End If
ret = CloseHandle(Handle_Proceso) 'Cierra el proceso abierto
MsgBox Ruta 'Muestra la ruta del proceso
'cierro la calculadora
Call SendMessage(FindWindow("SciCalc", vbNullString), &H112, &HF060&, 0)
End
End Sub
Saludos