枚举进程加载模块

news/2024/12/4 19:34:29/

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

在教程 vb.net 教程 6-3 进程加载的模块 中详细讲解了使用 Process类的modules属性,该属性可以获取进程加载的所有Dll文件,详细使用可以参看上述博文。

但是在实际使用中存在一个问题:对于有些程序,不能获得其进程全部的加载模块。

例如,获得QQExternal的加载模块,如果使用.Net只能获得5个dll。但是通过其它工具,可以看到实际包含了很多dll:

通过调用系统api可以很好地解决这个问题。
调用的api声明如下:
打开进程,以便执行后续操作:
    Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" (
           ByVal dwDesiredAccess As Integer,
           ByVal bInheritHandle As Integer,
           ByVal dwProcessId As Integer
           ) As IntPtr

枚举进程模块:
    Declare Function EnumProcessModulesEx Lib "PSAPI.DLL" (
           ByVal hProcess As IntPtr,
           ByVal lphModule() As Long,
           ByVal cb As Integer,
           ByRef cbNeeded As Integer,
           ByVal dwFilterFlag As Integer
           ) As Integer

获得模块文件路径:
    Declare Function GetModuleFileNameEx Lib "PSAPI.DLL" Alias "GetModuleFileNameExA" (
          ByVal hProcess As IntPtr,
          ByVal hModule As IntPtr,
          ByVal lpFileName As System.Text.StringBuilder,
          ByVal nSize As Integer) As Integer

关闭句柄:
 Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As Integer

根据以上API函数,获得模块的代码如下:
 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.LoadFor Each pro As Process In Process.GetProcessesDim lvPro As New ListViewItem(pro.ProcessName)lvPro.SubItems.Add(pro.Id)Me.ListView1.Items.Add(lvPro)NextEnd SubPrivate Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChangedIf ListView1.SelectedItems.Count <> 1 ThenExit SubEnd IfDim lvpro As New ListViewItem()lvpro = ListView1.SelectedItems(0)Dim proid As Integer = Integer.Parse(lvpro.SubItems(1).Text)Dim prohandle As Integer = Integer.Parse(lvpro.SubItems(1).Text)Call listModNet(proid)Call listModApi(proid)End SubPrivate Sub listModNet(ByVal proid As Integer)Dim pro As Process = Process.GetProcessById(proid)Dim lvMod As ListViewItemListView2.Items.Clear()TryTextBox2.Text = pro.Modules.CountFor Each proMod As ProcessModule In pro.ModuleslvMod = New ListViewItem(proMod.ModuleName)lvMod.SubItems.Add(proMod.BaseAddress.ToInt64)lvMod.SubItems.Add(proMod.FileVersionInfo.FileVersion)lvMod.SubItems.Add(proMod.ModuleMemorySize)lvMod.SubItems.Add(proMod.FileName)ListView2.Items.Add(lvMod)NextCatch ex As ExceptionTextBox2.Text = "err:" & ex.MessageEnd TryEnd SubPublic Const PROCESS_QUERY_INFORMATION = 1024Public Const PROCESS_VM_READ = 16Const LIST_MODULES_ALL = 3Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess As Integer,ByVal bInheritHandle As Integer,ByVal dwProcessId As Integer) As IntPtrDeclare Function EnumProcessModulesEx Lib "PSAPI.DLL" (ByVal hProcess As IntPtr,ByRef lphModule As IntPtr,ByVal cb As Integer,ByRef cbNeeded As Integer,ByVal dwFilterFlag As Integer) As IntegerDeclare Function EnumProcessModulesEx Lib "PSAPI.DLL" (ByVal hProcess As IntPtr,ByVal lphModule() As Long,ByVal cb As Integer,ByRef cbNeeded As Integer,ByVal dwFilterFlag As Integer) As IntegerDeclare Function GetModuleFileNameEx Lib "PSAPI.DLL" Alias "GetModuleFileNameExA" (ByVal hProcess As IntPtr,ByVal hModule As IntPtr,ByVal lpFileName As System.Text.StringBuilder,ByVal nSize As Integer) As IntegerDeclare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As IntegerPrivate Sub listModApi(ByVal proid As Integer)ListBox1.Items.Clear()Dim prohandle As IntPtrprohandle = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, proid)Dim maxMod As Integer = 1024Dim pmod() As LongReDim pmod(0)Dim cb As Integercb = System.Runtime.InteropServices.Marshal.SizeOf(pmod(0))Dim cbneeded As IntegerDim result As Integerresult = EnumProcessModulesEx(prohandle, pmod, 8, cbneeded, LIST_MODULES_ALL)cb = cbneeded / 8ReDim pmod(cb - 1)result = EnumProcessModulesEx(prohandle, pmod, cb * 8, cbneeded, LIST_MODULES_ALL)If result = 0 ThenTextBox1.Text = "err:" & Err.LastDllErrorElseTextBox1.Text = cbneeded / 8For i As Integer = 0 To pmod.Count - 1Dim modfilename As New System.Text.StringBuilder(255)result = GetModuleFileNameEx(prohandle, pmod(i), modfilename, 255)ListBox1.Items.Add(modfilename)NextEnd IfCloseHandle(prohandle)End Sub

对于部分进程,需要使用管理员权限才能打开查看。

运行时如下:

 可以看到.Net中获得5个模块,但是通过API可以发现,QQExternal的进程下有105个模块,相差比较大。但是原因尚不明确。

关于API的调用,请参看以下博文:

第27章 API的调用

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看vb.net 教程 目录


http://www.ppmy.cn/news/549905.html

相关文章

Content-Type: text/html;

** Content-Type: text/html; ** 它的意思是设置页面内容是html&#xff0c;编码格式是utf-8。 header()函数的作用是&#xff1a;发送一个原始 HTTP 标头[Http Header]到客户端。 标头 (header) 是服务器以 HTTP 协义传 HTML 资料到浏览器前所送出的字串&#xff0c;在标头…

Ubuntu下安装QQ(wineQQ)

谢天谢地&#xff0c;谢计算机大佬&#xff0c;在linux下搞出qq&#xff0c;没QQ&#xff0c;办公还真是不行&#xff0c;虽然有其它的传输方式&#xff0c;但总觉没有那么方便呀"。这真是个奇迹。 先安装wine&#xff0c;三条指令&#xff0c;注意&#xff1a;不是默认的…

去掉QQ迷你新闻的方法

1、打开QQ安装后的文件夹&#xff0c;单击菜单栏“工具”→文件夹选项&#xff0c;在打开的“文件夹选项”对话框中选择“查看”选项卡&#xff0c;在“高级设置”框中把“隐藏已知文件类型的扩展名”项前边的勾去掉。 2、在QQ安装后的文件夹中找到QQexternal.exe&#xff08;不…

Ubuntu 13.04下安装QQ2012

试了下在刚发布的Ubuntu 13.04下安装QQ2012&#xff0c;请根据自己的机器类型下载后按照下面的32位或64位安装说明安装。 下载网址&#xff1a;http://www.longene.org/download/ 直接下载地址&#xff1a;http://www.longene.org/download/WineQQ2012-20120719-Longene.de…

textContent和innerText属性的区别

实例1&#xff1a; <!DOCTYPE html> <html><head><meta charset"utf-8"><title></title></meta></head><body><div class"test" id"test">test<div style"display:none&q…

QQ的几个进程与程序文件

QQ的功能越来越多&#xff0c;加入的进程也越来越多&#xff0c;下面是几个常见的进程说明 QQ.exe 这个进程不用说了&#xff0c;直接略过QQExternal.exe QQ2008版本之前的QQExternal.exe是广告相关进程 QQ2011里面的QQExternal.exe是外部应用相关进程&#xff0c;QQ2…

QQExternal.exe介绍及禁止运行的方法

查看原文&#xff1a;http://www.hellonet8.com/801.html 今天中午章郎虫在使用电脑的时候&#xff0c;突然觉得电脑好卡。用任务管理器查看发现QQExternal.exe这个进程占用我这台老爷机30&#xff5e;50%cpu使用率和150M的内存。按照惯例&#xff0c;用Google搜索QQExternal.e…

【MATLAB第45期】基于MATLAB的深度学习SqueezeNet卷积神经网络混凝土裂纹图像识别预测模型

【MATLAB第45期】基于MATLAB的深度学习SqueezeNet卷积神经网络混凝土裂纹图像识别预测模型 引言 该文章展示如何微调名为SqueezeNet的预训练深度卷积网络&#xff0c;以执行裂纹图像分类预测。并使用一种称为Grad-CAM的技术来解释和分析分类输出。文章使用L.Zhang介绍的混凝土…