首页 > 代码库 > Windows编程 - 遍历程序使用的动态链接库(dll) 代码(C++)

Windows编程 - 遍历程序使用的动态链接库(dll) 代码(C++)

遍历程序使用的动态链接库(dll) 代码(C++)


本文地址: http://blog.csdn.net/caroline_wendy


遍历程序使用的动态链接库(dll), 首先需要遍历所有进程, 匹配进程名称与进程ID, 然后根据进程名称, 输出所有使用的库(dll).

示例中Image.exe是预先启动的程序. 代码包含遍历进程的代码, 和输出动态链接库(dll)的代码.


代码:

/*
 * main.cpp
 *
 *  Created on: 2014.06.08
 *      Author: Spike
 */

/*vs 2012*/

#include <iostream>
#include <iomanip>
#include <string>
#include <map>


#include <windows.h>
#include <TlHelp32.h>

using namespace std;

bool traverseProcesses (std::map<std::string, int>& _nameID) 
{
	PROCESSENTRY32 pe32;
	pe32.dwSize = sizeof(pe32);

	HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if(hProcessSnap == INVALID_HANDLE_VALUE) {
		std::cout << "CreateToolhelp32Snapshot Error!" << std::endl;;
		return false;
	}

	BOOL bResult =Process32First(hProcessSnap, &pe32);

	int num(0);

	while(bResult) 
	{
		std::string name = pe32.szExeFile;
		int id = pe32.th32ProcessID;

		//std::cout << "[" << ++num << "] : " <<"Process Name:" 
		//	<< name << "  " << "ProcessID:" << id<< std::endl;

		_nameID.insert(std::pair<string, int>(name, id)); //字典存储
		bResult = Process32Next(hProcessSnap,&pe32);
	}

	CloseHandle(hProcessSnap);

	return true;
}

bool traverseModels(const std::string _name) 
{
	DWORD dwId;

	/*printf("Please enter the name of process to traverse processmodels:");
	std::string name;
	cin >> name;*/

	std::map<std::string, int> nameID;

	if (!traverseProcesses(nameID)) { //变量进程
		cout << "Print Processes Error!" << endl;
	}

	dwId = nameID[_name];

	HANDLE hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwId);
	if(hModuleSnap == INVALID_HANDLE_VALUE){
		printf("CreateToolhelp32SnapshotError! \n");
		return false;
	}

	MODULEENTRY32 module32;
	module32.dwSize = sizeof(module32);
	BOOL bResult = Module32First(hModuleSnap, &module32);

	int num(0);

	while(bResult){
		std::wcout << "[" << num++ << "] : " <<"Module:" << std::left << std::setw(25) 
			<< module32.szModule << "  " << endl << "Path:" << module32.szExePath << std::endl;
		bResult = Module32Next(hModuleSnap, &module32);
	}

	CloseHandle(hModuleSnap);

	return true;

}

int main()
{
	const std::string program("Image.exe");

	if (!traverseModels(program)) {
		cout << "Traverse Models Error!" << endl;
	}

	return 0;
}

输出: