Guidance
指路人
g.yi.org
Guidance Forums / Rapid-Q Basic / Modulename not returned by GetModuleFileNameEx

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Modulename not returned by GetModuleFileNameEx
#4417
Posted by: 2004-06-11 07:26:51
I'm trying to find the name of a process so that I can kill it and start another program.
However GetModuleFileNameEx does not return the name of the process/Modulename as VB or Access does

Can anybody help?
$TYPECHECK ON
$OPTIMIZE ON
$INCLUDE "RAPIDQ.INC"
'$INCLUDE "WIN32API.INC"

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

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

Type PROCESS_MEMORY_COUNTERS
        cb As Long
        PageFaultCount As Long
        PeakWorkingSetSize As Long
        WorkingSetSize As Long
        QuotaPeakPagedPoolUsage As Long
        QuotaPagedPoolUsage As Long
        QuotaPeakNonPagedPoolUsage As Long
        QuotaNonPagedPoolUsage As Long
        PagefileUsage As Long
        PeakPagefileUsage As Long
End Type

      Const PROCESS_QUERY_INFORMATION = 1024
      Const PROCESS_VM_READ = 16
      Const MAX_PATH = 260
      Const STANDARD_RIGHTS_REQUIRED = &HF0000
      Const SYNCHRONIZE = &H100000
      'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
      Const PROCESS_ALL_ACCESS = &H1F0FFF
      Const TH32CS_SNAPPROCESS = &H2&
      Const hNull = 0

'      Const False = 0
'      Const True = 1

DECLARE SUB Button1Click (Sender AS QBUTTON)

Declare Function Process32First Lib "kernel32" ALIAS "Process32First"( _
        ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long

Declare Function Process32Next Lib "kernel32" ALIAS "Process32Next"( _
         ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
        
Declare Function GetProcessMemoryInfo Lib "psapi.dll" alias "GetProcessMemoryInfo" ( _
         Byval process As Long, ppsmemCounters As PROCESS_MEMORY_COUNTERS, _
         Byval size As Long) As Integer

Declare Function CloseHandle Lib "Kernel32.dll" ALIAS "CloseHandle" _
         (ByVal Handle As Long) As Long

Declare Function OpenProcess Lib "Kernel32.dll" ALIAS "OpenProcess" _
        (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
            ByVal dwProcId As Long) As Long

Declare Function EnumProcesses Lib "psapi.dll" ALIAS "EnumProcesses" _
         (ByRef lpidProcess As Long, ByVal cb As Long, _
            ByRef cbNeeded As Long) As Long

Declare Function GetModuleFileNameEx Lib "psapi.dll" ALIAS "GetModuleFileNameExA" _
         (ByVal hProcess As Long, ByVal hModule As Long,  _
            ByVal lpFileName As String, ByVal nSize As Long) As Long

Declare Function EnumProcessModules Lib "psapi.dll" ALIAS "EnumProcessModules" _
         (ByVal hProcess As Long, ByRef lphModule As Long, _
            ByVal cb As Long, ByRef cbNeeded As Long) As Long

Declare Function CreateToolhelp32Snapshot Lib "kernel32" ALIAS "CreateToolhelp32Snapshot"( _
         ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long

Declare Function GetVersionExA Lib "kernel32" ALIAS "GetVersionExA" _
         (lpVersionInformation As OSVERSIONINFO) As Integer
        
Declare Function getVers() As Long

Declare Function fIsAppRunning(docstr As String) as variant

Declare Function StrZToStr(s As String) As String

Function fIsAppRunning(docstr As String) as long
dim akey$ as string
dim ret as long
dim i as long

      print "in fIsAppRunning, docstr" & docstr

      Select Case getVers()

      Case 1 'Windows 95/98
        print "in fIsAppRunning, case 1" & docstr

         Dim f As Long, sname As String
         Dim hSnap As Long, proc As PROCESSENTRY32
         hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
         If hSnap = hNull Then Exit Function
         proc.dwSize = Len(proc)
         ' Iterate through the processes
         f = Process32First(hSnap, proc)
         Do
          While f
           sname = StrZToStr(proc.szExeFile)
           Print sname
           f = Process32Next(hSnap, proc)
           wend
         Loop

      Case 2  'Windows NT or XP
       'print "in fIsAppRunning, case 2" & docstr

         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
       print "cb= ";cb
         'Do
         While cb <= cbNeeded
            cb = cb * 2
            ReDim ProcessIDs(cb / 4) As Long
            lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
         wend

         'Loop
         NumElements = cbNeeded / 4
         print "lRet=";lRet;" NumElements = ";NumElements

         For i = 1 To NumElements
            'Get a handle to the Process
            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
               Or PROCESS_VM_READ, 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
         print " Element no = ";i;" Got a Process handle=";hProcess;" iret=";lRet;" cbNeeded2=";cbNeeded2

                If lRet <> 0 Then
                PRINT "len of Modulename b4:";len(ModuleName);" name=";Left$(ModuleName, lRet)
  '                 ModuleName = Space$(MAX_PATH)
         print " MAX_PATH = ";MAX_PATH;" ModuleName=";ModuleName
                   nSize = 500
 '                  lRet = GetModuleFileNameExA(hProcess, Modules(1), _
                   lRet = GetModuleFileNameEx(hProcess, Modules(1), _
                                   ModuleName, nSize)
         print " Lret=";lRet;" hProcess=";hProcess;" Modules(1)=";Modules(1);" nSize=";nSize
         print "ModuleName=";ModuleName
                    Print Left$(ModuleName, lRet)
                    If InStr(1, Left$(ModuleName, lRet), docstr) > 0 Then
                        Print Left$(ModuleName, lRet)
                        fIsAppRunning = True
                    End If
         input "Press a key to view next process" ;akey$
         print"---------------------------------------"
                End If
            End If
          'Close the handle to the process
         lRet = CloseHandle(hProcess)
         Next

      End Select
     

End Function

Function StrZToStr(s As String) As String
         StrZToStr = Left$(s, Len(s) - 1)
End Function

Function getVers() As Long
         Dim osinfo As OSVERSIONINFO
         Dim retvalue As Integer
         osinfo.dwOSVersionInfoSize = 148
         osinfo.szCSDVersion = Space$(128)
         retvalue = GetVersionExA(osinfo)
         getVers = osinfo.dwPlatformId
  'print "in getvers, getVers" & osinfo.dwPlatformId
End Function
    
CREATE Form AS QFORM
    Caption = "Form1"
    Width = 320
    Height = 240
    Center
    CREATE Button1 AS QBUTTON
        Caption = "Button1"
        Left = 77
        Top = 43
        OnClick = Button1Click
    END CREATE
END CREATE
Form.ShowModal

SUB Button1Click (Sender AS QBUTTON)
   '-- Enter your code here
Dim varRet As Variant
Dim doc As String
' varRet = Shell """C:\mm\doctrine\bin\dstation.exe""" ' mm package will refire this back up
' varRet = Shell """C:\mm\doctrine\bin\adel_javaw.exe"""

doc = "mm_javaw.exe"
print "in main, getvers=";varRet
varRet = fIsAppRunning(doc)
If varRet = True Then
  Print doc & " Is running"
Else
  Print doc & " Is not running"
' varRet = Shell """C:\mm\hdstart.bat"""
End If

END SUB

Message2. Re: Modulename not returned by GetModuleFileNameEx
#4432
Posted by: genglish 2004-06-12 08:22:51
Getting Closer - found something in this forum that applies:
Changed:
Public Declare Function GetModuleFileNameEx Lib "PSAPI.DLL" Alias "GetModuleFileNameExA" _
(ByVal hProcess As Long, _
 ByVal hModule As Long, _
 ByVal lpFileName As String, _
 ByVal nSize As Long ) As Long

to:
Public Declare Function GetModuleFileNameEx Lib "PSAPI.DLL" Alias "GetModuleFileNameExA" _
(ByVal hProcess As Long, _
 ByVal hModule As Long, _
 ByVal lpFileName As long, _
 ByVal nSize As Long ) As Long
               &
Changed:
lRet = GetModuleFileNameEx(hProcess, Modules(1), ModuleName, nSize)
to:
lRet = GetModuleFileNameEx(hProcess, Modules(1), VARPTR(ModuleName), nSize)

gives me:Lret=32hProcess=148 Modules(1)=16777216ModuleName=C:\WINDOWS\system32\services.exe exe
Does anybody have any ideas on how to return the PID(processID) from 16777216?

Message3. Re: Modulename not returned by GetModuleFileNameEx
#4458
Posted by: JohnK 2004-06-15 02:58:03
Be sure to check you are using GetModuleFileNameEx and not GetModuleFileNameExA! Case is important. Also, if you pass a UDT to these api's you should make sure the pointer is correct. I have noticed that if I pass a UDT the API doesn't run, You must then make a QMEMORYSTREAM
.WriteUDT()
Then pass the pointer (QMEMORYSTREAM.pointer) to the API. This is confusing in the DOCs, but is explained in it. I think you can pass the UDT only if your UDT is very simple.

-JohnK
I'm basic too
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Thu 2024-4-18  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0