VC遍历dll导出函数

来源:互联网 发布:linux pv命令 编辑:程序博客网 时间:2024/06/04 18:47

参考文章:http://www.ccrun.com/article.asp?i=653&d=b2m5o1

代码:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. // ViewDllFunc.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include<windows.h>  
  6. #include<stdio.h>  
  7. #include<Imagehlp.h>  
  8. #pragma comment(lib,"Imagehlp.lib")  
  9.   
  10. bool GetDLLFileExports(char *szFileName, UINT *nNoOfExports, char **&pszFunctions);  
  11.   
  12. int main(int argc, char* argv[])  
  13. {  
  14.     UINT unNoOfExports=0;  
  15.     char **lppBuffer;  
  16.   
  17.     GetDLLFileExports("dllA.dll", &unNoOfExports, lppBuffer);  
  18.   
  19.     char func[256];  
  20.     memset(func,0,sizeof(func));  
  21.     for(UINT i=0; i<unNoOfExports; i++)  
  22.     {  
  23.        // Memo1->Lines->Add(lppBuffer[i]);  
  24.         sprintf(func,"%d %s",i,lppBuffer[i]);  
  25.         printf("%s \n",func);  
  26.     }  
  27.   
  28.     for(i=0; i<unNoOfExports; i++)  
  29.         delete []lppBuffer[i];  
  30.   
  31.     delete []lppBuffer;  
  32.   
  33.     printf("Hello World!\n");  
  34.     getchar();  
  35.     return 0;  
  36. }  
  37.   
  38.   
  39. bool GetDLLFileExports(char *szFileName, UINT *nNoOfExports, char **&pszFunctions)  
  40. {  
  41.     HANDLE hFile;  
  42.     HANDLE hFileMapping;  
  43.     LPVOID lpFileBase;  
  44.     PIMAGE_DOS_HEADER pImg_DOS_Header;  
  45.     PIMAGE_NT_HEADERS pImg_NT_Header;  
  46.     PIMAGE_EXPORT_DIRECTORY pImg_Export_Dir;  
  47.   
  48.     hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);  
  49.     if(hFile == INVALID_HANDLE_VALUE)  
  50.         return false;  
  51.   
  52.     hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);  
  53.     if(hFileMapping == 0)  
  54.     {  
  55.         CloseHandle(hFile);  
  56.         return false;  
  57.     }  
  58.   
  59.     lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0);  
  60.     if(lpFileBase == 0)  
  61.     {  
  62.         CloseHandle(hFileMapping);  
  63.         CloseHandle(hFile);  
  64.         return false;  
  65.     }  
  66.   
  67.     pImg_DOS_Header = (PIMAGE_DOS_HEADER)lpFileBase;  
  68.     pImg_NT_Header = (PIMAGE_NT_HEADERS)(  
  69.             (LONG)pImg_DOS_Header + (LONG)pImg_DOS_Header->e_lfanew);  
  70.   
  71.     if(IsBadReadPtr(pImg_NT_Header, sizeof(IMAGE_NT_HEADERS))  
  72.             || pImg_NT_Header->Signature != IMAGE_NT_SIGNATURE)  
  73.     {  
  74.     UnmapViewOfFile(lpFileBase);  
  75.     CloseHandle(hFileMapping);  
  76.     CloseHandle(hFile);  
  77.     return false;  
  78.     }  
  79.   
  80.     pImg_Export_Dir = (PIMAGE_EXPORT_DIRECTORY)pImg_NT_Header->OptionalHeader  
  81.             .DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;  
  82.     if(!pImg_Export_Dir)  
  83.     {  
  84.         UnmapViewOfFile(lpFileBase);  
  85.         CloseHandle(hFileMapping);  
  86.         CloseHandle(hFile);  
  87.         return false;  
  88.     }  
  89.     // 63 63 72 75 6E 2E 63 6F 6D  
  90.     pImg_Export_Dir= (PIMAGE_EXPORT_DIRECTORY)ImageRvaToVa(pImg_NT_Header,  
  91.             pImg_DOS_Header, (DWORD)pImg_Export_Dir, 0);  
  92.   
  93.     DWORD **ppdwNames = (DWORD **)pImg_Export_Dir->AddressOfNames;  
  94.   
  95.     ppdwNames = (PDWORD*)ImageRvaToVa(pImg_NT_Header,  
  96.             pImg_DOS_Header, (DWORD)ppdwNames, 0);  
  97.     if(!ppdwNames)  
  98.     {  
  99.         UnmapViewOfFile(lpFileBase);  
  100.         CloseHandle(hFileMapping);  
  101.         CloseHandle(hFile);  
  102.         return false;  
  103.     }  
  104.   
  105.     *nNoOfExports = pImg_Export_Dir->NumberOfNames;  
  106.     pszFunctions = new char*[*nNoOfExports];  
  107.   
  108.     for(UINT i=0; i < *nNoOfExports; i++)  
  109.     {  
  110.         char *szFunc=(PSTR)ImageRvaToVa(pImg_NT_Header, pImg_DOS_Header, (DWORD)*ppdwNames, 0);  
  111.   
  112.         pszFunctions[i] = new char[strlen(szFunc)+1];  
  113.         strcpy(pszFunctions[i],szFunc);  
  114.   
  115.         ppdwNames++;  
  116.     }  
  117.     UnmapViewOfFile(lpFileBase);  
  118.     CloseHandle(hFileMapping);  
  119.     CloseHandle(hFile);  
  120.     return true;  
  121. }  


---------------------------------------------------------------------------------------------------------------

0 0