Windows編程:遍曆程式使用的動態連結程式庫(dll) 代碼(C++)

來源:互聯網
上載者:User

遍曆程式使用的動態連結程式庫(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:"//更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/        //  << 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;  }

輸出:

作者:csdn部落格 Spike_King

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.