我想知道一个EXE文件现在正在调用哪个文件?如何取得相应信息?用VB如何实现?请各位帮帮忙

解决方案 »

  1.   

    在EXE执行当中要监视其调用哪些文件?
    不懂就要顶
      

  2.   

    回复人: BCB_FANS(四大名捕之追杀令) ( ) 信誉:100  2002-11-26 22:42:35Z  得分:0 
     
     
      
    调用NTDLL.DLL里边的函数NtQuerySystemInformation()查询第16号信息(句柄)和NtQueryObject()以及NtQueryInformationFile()应该可以达到你的要求。以前有很多文章讨论进程的端口使用问题,就是使用上面这两个函数来实现的。至于这两个函数的具体使用方法,可以参考《NT/2000 Navite API Reference》这本书。在http://bingle.sitenova.net/上面有这本书的电子版本。
      
     
      

  3.   

    看看mModules(1 To 200)有没有问题。它必须写成(1 To 200)但我只用了一个,不知为什么
    定义这么大,是不是放别的模块哪?
    Option ExplicitPublic Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) 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 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 Type PROCESSENTRY32
         dwSize  As Long
         cntUsage  As Long
         th32ProcessID  As Long
         th32DefaultHeapID  As Long
         th32ModuleID  As Long
         cntThreads  As Long
         th32ParentProcessID  As Long
         pcPriClassBase  As Long
         dwFlags  As Long
         szExeFile  As String * 260
    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
    Public Const PROCESS_ALL_ACCESS = &H1F0FFF
    Public Const TH32CS_SNAPPROCESS = &H2&
    Public Const hNull = 0
     
    Public Function GetPath(ByVal hWnd As Long) As String
        Dim hProcess As Long
        Dim ProcID As Long
        Dim mModules(1 To 200) As Long
        Dim cbNeed As Long
        Dim ModuleName As String
        Dim nSize As Long
        Dim lRet As Long    GetWindowThreadProcessId hWnd, ProcID
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, ProcID)
        lRet = EnumProcessModules(hProcess, mModules(1), 200, cbNeed)
        If lRet <> 0 Then
            ModuleName = Space(MAX_PATH)
            nSize = 500
            lRet = GetModuleFileNameExA(hProcess, mModules(1), ModuleName, nSize)
            GetPath = Left(ModuleName, lRet)
        End If
        lRet = CloseHandle(hProcess)
    End Function
    '调用如下:
    'Text1.Text=GetPath(hWnd)<hWnd>是你要查的句柄
      

  4.   

    dongge2000(秋日私语):
      请问hWnd是句柄,我如何去取得句柄呢?
      如果我想给它传个EXE程序名行吗?
      

  5.   

    不行,
    Option ExplicitPublic Declare Function WindowFromPoint Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
    Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPublic Type POINTAPI
        X As Long
        Y As Long
    End TypePublic Type DGfor3
        ponX As Long
        ponY As Long
        MDC As Long
    End TypePublic Function MouseDC() As DGfor3
        On Error Resume Next
        Dim Cur As POINTAPI
        GetCursorPos Cur
        MouseDC.MDC = WindowFromPoint(Cur.X, Cur.Y)
        MouseDC.ponX = Cur.X
        MouseDC.ponY = Cur.Y
    End Function
      

  6.   

    也可用EnumWindows函数。
    EnumWindows VB声明 
    Declare Function EnumWindows& Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) 
    说明 
    枚举窗口列表中的所有父窗口(顶级和被所有窗口) 
    返回值 
    Long,非零表示成功,零表示失败 
    参数表 
    参数 类型及说明 
    lpEnumFunc Long,指向为每个子窗口都调用的一个函数的指针。用AddressOf运算符获得函数在标准模式下的地址 
    lParam Long,在枚举期间,传递给dwcbkd32.ocx定制控件之EnumWindows事件的值。这个值的含义是由程序员规定的 
    注解 
    我的理解——在随vb5同时提供的api32.txt文件中,找不到这个函数
     
      

  7.   


    ' BAS Moduel Code
    '
    '------------------------------------------------------------------------------
    Option Explicit Private Const LVIF_INDENT As Long = &H10
    Private Const LVIF_TEXT As Long = &H1
    Private Const LVM_FIRST As Long = &H1000
    Private Const LVM_SETITEM As Long = (LVM_FIRST + 6)Private Type LVITEM
       mask As Long
       iItem As Long
       iSubItem As Long
       state As Long
       stateMask As Long
       pszText As String
       cchTextMax As Long
       iImage As Long
       lParam As Long
       iIndent As Long
    End TypePublic Declare Function EnumWindows Lib "user32" _
      (ByVal lpEnumFunc As Long, _
       ByVal lParam As Long) As Long
       
    Public Declare Function EnumChildWindows Lib "user32" _
      (ByVal hWndParent As Long, _
       ByVal lpEnumFunc As Long, _
       ByVal lParam As Long) As LongPrivate Declare Function GetWindowTextLength Lib "user32" _
        Alias "GetWindowTextLengthA" _
       (ByVal hwnd As Long) As Long
        
    Private Declare Function GetWindowText Lib "user32" _
        Alias "GetWindowTextA" _
       (ByVal hwnd As Long, _
        ByVal lpString As String, _
        ByVal cch As Long) As Long
        
    Private Declare Function GetClassName Lib "user32" _
        Alias "GetClassNameA" _
       (ByVal hwnd As Long, _
        ByVal lpClassName As String, _
        ByVal nMaxCount As Long) As LongPrivate Declare Function IsWindowVisible Lib "user32" _
       (ByVal hwnd As Long) As Long
       
    Private Declare Function GetParent Lib "user32" _
       (ByVal hwnd As Long) As LongPrivate Declare Function SendMessage Lib "user32" _
       Alias "SendMessageA" _
      (ByVal hwnd As Long, _
       ByVal wMsg As Long, _
       ByVal wParam As Long, _
       lParam As Any) As Long
    Public Function EnumWindowProc(ByVal hwnd As Long, _
                                   ByVal lParam As Long) As Long
       
      'working vars
       Dim nSize As Long
       Dim sTitle As String
       Dim sClass As String
       
       Dim sIDType As String
       Dim itmX As ListItem
       Dim nodX As Node
       
      'eliminate windows that are not top-level.
       If GetParent(hwnd) = 0& And _
          IsWindowVisible(hwnd) Then
          
         'get the window title / class name
          sTitle = GetWindowIdentification(hwnd, sIDType, sClass)     'add to the listview
          Set itmX = Form1.ListView1.ListItems.Add(Text:=sTitle, Key:=CStr(hwnd) & "h")
          itmX.SmallIcon = Form1.ImageList1.ListImages("parent").Key
          itmX.SubItems(1) = CStr(hwnd)
          itmX.SubItems(2) = sIDType
          itmX.SubItems(3) = sClass
          
       End If
       
      'To continue enumeration, return True
      'To stop enumeration return False (0).
      'When 1 is returned, enumeration continues
      'until there are no more windows left.
       EnumWindowProc = 1
       
    End Function
    Private Function GetWindowIdentification(ByVal hwnd As Long, _
                                             sIDType As String, _
                                             sClass As String) As String   Dim nSize As Long
       Dim sTitle As String  'get the size of the string required
      'to hold the window title
       nSize = GetWindowTextLength(hwnd)
       
      'if the return is 0, there is no title
       If nSize > 0 Then
       
          sTitle = Space$(nSize + 1)
          Call GetWindowText(hwnd, sTitle, nSize + 1)
          sIDType = "title"
          
          sClass = Space$(64)
          Call GetClassName(hwnd, sClass, 64)
       
       Else
       
         'no title, so get the class name instead
          sTitle = Space$(64)
          Call GetClassName(hwnd, sTitle, 64)
          sClass = sTitle
          sIDType = "class"
       
       End If
       
       GetWindowIdentification = TrimNull(sTitle)End Function
    Public Function EnumChildProc(ByVal hwnd As Long, _
                                  ByVal lParam As Long) As Long
       
      'working vars
       Dim sTitle As String
       Dim sClass As String
       Dim sIDType As String
       Dim itmX As ListItem  'get the window title / class name
       sTitle = GetWindowIdentification(hwnd, sIDType, sClass)  'add to the listview
       Set itmX = Form2.ListView1.ListItems.Add(Text:=sTitle)
       itmX.SmallIcon = Form2.ImageList1.ListImages("child").Key
       itmX.SubItems(1) = CStr(hwnd)
       itmX.SubItems(2) = sIDType
       itmX.SubItems(3) = sClass
          
       Listview_IndentItem Form2.ListView1.hwnd, CLng(itmX.Index), 1
       
       EnumChildProc = 1
       
    End Function
    Private Function TrimNull(startstr As String) As String  Dim pos As Integer  pos = InStr(startstr, Chr$(0))
      
      If pos Then
          TrimNull = Left$(startstr, pos - 1)
          Exit Function
      End If
      
     'if this far, there was
     'no Chr$(0), so return the string
      TrimNull = startstr
      
    End Function
    Private Sub Listview_IndentItem(hwnd As Long, _
                                    nItem As Long, _
                                    nIndent As Long)   Dim LV As LVITEM  'if nIndent indicates that indentation
      'is requested nItem is the item to indent
       If nIndent > 0 Then
          
          With LV
            .mask = LVIF_INDENT
            .iItem = nItem - 1 'have to subtract 1
            .iIndent = nIndent
          End With
          
          Call SendMessage(hwnd, LVM_SETITEM, 0&, LV)
          
       End If
           
    End Sub
    '--end block--'
    '------------------------------------------------------------------------------
    '
    ' Form Code
    '
    '------------------------------------------------------------------------------
    Option ExplicitPrivate Sub Command1_Click()   ListView1.ListItems.Clear
       Call EnumWindows(AddressOf EnumWindowProc, &H0)End Sub
    Private Sub Form_Load()   Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2End Sub
    Private Sub ListView1_DblClick()   Dim hwndSelected As Long
       
       hwndSelected = Val(ListView1.SelectedItem.Key)
       
       Load Form2
       Call Form2.EnumSelectedWindow(ListView1.SelectedItem.Text, hwndSelected)
       
    End Sub
    '--end block--'
     
     
     Form2 Code 
      
    Add the following code to Form2: --------------------------------------------------------------------------------
     
    Option ExplicitPublic Sub EnumSelectedWindow(sItem As String, hwnd As Long)
     
       ListView1.ListItems.Clear
       ListView1.ListItems.Add Text:=sItem, SmallIcon:="parent"
       
       Call EnumChildWindows(hwnd, AddressOf EnumChildProc, &H0)
       
       Me.Show vbModal
       
    End Sub
    Private Sub Form_Load()   Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2
       
    End Sub