Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    Ricavare il handle di un programma

    Ciao vengo subito al sodo, come faccio a conoscere il handle di un programma se conosco il nome del programma, non il titolo della finestra ma il nome del programma tipo Winamp.exe

    grazie

  2. #2
    Utente di HTML.it
    Registrato dal
    Jul 2001
    Messaggi
    798
    Dovresti controllare i processi attivi, le dichiarazioni per le API da utilizzare sono le seguenti.

    codice:
    Public Declare Function Process32First Lib "kernel32" ( _
       ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    
    Public Declare Function Process32Next Lib "kernel32" ( _
       ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    
    Public Declare Function CloseHandle Lib "Kernel32.dll" _
       (ByVal Handle As Long) As Long
    
    Public Declare Function OpenProcess Lib "Kernel32.dll" _
      (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
          ByVal dwProcId As Long) As Long
    
    Public Declare Function EnumProcesses Lib "psapi.dll" _
       (ByRef lpidProcess As Long, ByVal cb As Long, _
          ByRef cbNeeded As Long) As Long
    
    Public Declare Function GetModuleFileNameExA Lib "psapi.dll" _
       (ByVal hProcess As Long, ByVal hModule As Long, _
          ByVal ModuleName As String, ByVal nSize As Long) As Long
    
    Public Declare Function EnumProcessModules Lib "psapi.dll" _
       (ByVal hProcess As Long, ByRef lphModule As Long, _
          ByVal cb As Long, ByRef cbNeeded As Long) As Long
    
    Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
       ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    
    Public Declare Function GetVersionExA Lib "kernel32" _
       (lpVersionInformation As OSVERSIONINFO) As Integer
    
    Public Type PROCESSENTRY32
       dwSize As Long
       cntUsage As Long
       th32ProcessID As Long           ' This process
       th32DefaultHeapID As Long
       th32ModuleID As Long            ' Associated exe
       cntThreads As Long
       th32ParentProcessID As Long     ' This process's parent process
       pcPriClassBase As Long          ' Base priority of process threads
       dwFlags As Long
       szExeFile As String * 260       ' MAX_PATH
    End Type
    
    Public Type OSVERSIONINFO
       dwOSVersionInfoSize As Long
       dwMajorVersion As Long
       dwMinorVersion As Long
       dwBuildNumber As Long
       dwPlatformId As Long           '1 = Windows 95.
                                      '2 = Windows NT
       szCSDVersion As String * 128
    End Type
    
    Public Const PROCESS_QUERY_INFORMATION = 1024
    Public Const PROCESS_VM_READ = 16
    Public Const MAX_PATH = 260
    Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
    Public Const SYNCHRONIZE = &H100000
    'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
    Public Const PROCESS_ALL_ACCESS = &H1F0FFF
    Public Const TH32CS_SNAPPROCESS = &H2&
    Public Const hNull = 0
    
    Function StrZToStr(s As String) As String
       StrZToStr = Left$(s, Len(s) - 1)
    End Function
    
    Public Function getVersion() As Long
       Dim osinfo As OSVERSIONINFO
       Dim retvalue As Integer
       osinfo.dwOSVersionInfoSize = 148
       osinfo.szCSDVersion = Space$(128)
       retvalue = GetVersionExA(osinfo)
       getVersion = osinfo.dwPlatformId
    End Function

    Questo il codice x ciclare sui processi attivi, quando trovi quello che ti interessa ti puoi fermare, chiudi il processo e ti salvi l'handle.

    codice:
        Select Case getVersion()
            Case 1 'Windows 95/98
                Dim f As Long, sName As String
                Dim hSnap As Long, proc As PROCESSENTRY32
                
                hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
                If hSnap = hNull Then Exit Sub
                proc.dwSize = Len(proc)
                ' Iterate through the processes
                f = Process32First(hSnap, proc)
                Do While f
                    sName = StrZToStr(proc.szExeFile)
                    Debug.Print sName
                    f = Process32Next(hSnap, proc)
                Loop
            Case 2 'Windows NT/2000/XP
                Dim cb As Long
                Dim cbNeeded As Long
                Dim NumElements As Long
                Dim ProcessIDs() As Long
                Dim cbNeeded2 As Long
                Dim NumElements2 As Long
                Dim Modules(1 To 200) As Long
                Dim lRet As Long
                Dim ModuleName As String
                Dim nSize As Long
                Dim hProcess As Long
                Dim I As Long
    
                'Get the array containing the process id's for each process object
                cb = 8
                cbNeeded = 96
                Do While cb <= cbNeeded
                    cb = cb * 2
                    ReDim ProcessIDs(cb / 4) As Long
                    lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
                Loop
                NumElements = cbNeeded / 4
                For I = 1 To NumElements
                    'Get a handle to the Process
                    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
                                        Or PROCESS_ALL_ACCESS, 0, ProcessIDs(I))
                    'Got a Process handle
                    If hProcess <> 0 Then
                        'Get an array of the module handles for the specified
                        'process
                        lRet = EnumProcessModules(hProcess, Modules(1), 200, cbNeeded2)
                        'If the Module Array is retrieved, Get the ModuleFileName
                        If lRet <> 0 Then
                           ModuleName = Space(MAX_PATH)
                           nSize = 500
                           lRet = GetModuleFileNameExA(hProcess, Modules(1), ModuleName, nSize)
                           Debug.Print Left$(ModuleName, lRet)
                        End If
                    End If
                    'Close the handle to the process
                    lRet = CloseHandle(hProcess)
                Next
        End Select
    Ans.

  3. #3
    Ciao sono ancora io il fatto è che non sono riuscito a capire come funzionava il codice mi ci sono scervellato un po' ma non l'ho capito, l'ho provato un po' e lui mi ritornava nella lista di debug i nome dei processi attivi sul mio computer ma non sono riuscito a capire dove trovare handle del programma che mi interessa.

    scusa il disturbo

    grazie

  4. #4
    Utente di HTML.it
    Registrato dal
    Jul 2001
    Messaggi
    798
    ma se è commentato e scritto ???

    'Get a handle to the Process
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
    Or PROCESS_ALL_ACCESS, 0, ProcessIDs(I))
    Ans.

  5. #5
    Quello lo avevo trovato solo che forse io sono troppo stupido ma non riesco a capire dove devo scrivere il nome del programma che voglio che venga chiuso (devo scrivere solo il nome.exe o anche tutto l'indirizzo c:\ecc\ecc\nome.exe?)

  6. #6
    Utente di HTML.it
    Registrato dal
    Jul 2001
    Messaggi
    798
    da nessuna parte. quel codice ti serve x recuperati l'handle del programma, poi a parte fai quel che devi fare.
    Ans.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.