Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2010
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.90 KB | None | 0 0
  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include <commctrl.h>
  4. #include <stdio.h>
  5.  
  6. #define WINDOWCLASS "LISTEXPORTEDFUNCTIONS"
  7. #define TITLE "List Exported Functions"
  8.  
  9. #define IDC_EDIT 101
  10. #define IDC_BUTTON 102
  11. #define IDC_LISTVIEW 103
  12.  
  13. #define IDM_COPYNAME 201
  14. #define IDM_COPYADDRESS 202
  15. #define IDM_COPYFORWARDER 203
  16. #define IDM_COPYORDINAL 204
  17.  
  18. ATOM MyRegisterClass(HINSTANCE);
  19. BOOL InitInstance(HINSTANCE, int);
  20. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  21. void ResizeControls(HWND, HWND, HWND, HWND);
  22. void CreateColumns(HWND);
  23. void OnButtonClick(HWND, HWND, HWND);
  24. void InsertItems(HWND, HWND, LPTSTR);
  25. void OnRightClick(HWND, HWND, int);
  26. void OnCopyClick(HWND, int, int);
  27. DWORD GetForwarderAddress(LPTSTR);
  28.  
  29. int APIENTRY WinMain(HINSTANCE hInstance,
  30.                      HINSTANCE hPrevInstance,
  31.                      LPTSTR lpCmdLine,
  32.                      int nCmdShow)
  33. {
  34.   MSG msg;
  35.   INITCOMMONCONTROLSEX icex;
  36.  
  37.   icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  38.   icex.dwICC  = ICC_LISTVIEW_CLASSES;
  39.   InitCommonControlsEx(&icex);
  40.  
  41.   MyRegisterClass(hInstance);
  42.  
  43.   if(!InitInstance(hInstance, nCmdShow))
  44.     return EXIT_FAILURE;
  45.  
  46.   while(GetMessage(&msg, NULL, 0, 0))
  47.   {
  48.     TranslateMessage(&msg);
  49.     DispatchMessage(&msg);
  50.   }
  51.  
  52.   return (int)msg.wParam;
  53. }
  54.  
  55. ATOM MyRegisterClass(HINSTANCE hInstance)
  56. {
  57.   WNDCLASSEX wcex;
  58.  
  59.   wcex.cbSize        = sizeof(WNDCLASSEX);
  60.   wcex.style         = CS_HREDRAW | CS_VREDRAW;
  61.   wcex.lpfnWndProc   = WndProc;
  62.   wcex.cbClsExtra    = 0;
  63.   wcex.cbWndExtra    = 0;
  64.   wcex.hInstance     = hInstance;
  65.   wcex.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  66.   wcex.hCursor       = LoadCursor(NULL, IDC_ARROW);
  67.   wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  68.   wcex.lpszMenuName  = NULL;
  69.   wcex.lpszClassName = WINDOWCLASS;
  70.   wcex.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  71.  
  72.   return RegisterClassEx(&wcex);
  73. }
  74.  
  75. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  76. {
  77.   HWND hWnd;
  78.  
  79.   hWnd = CreateWindow(WINDOWCLASS, TITLE, WS_OVERLAPPEDWINDOW,
  80.       CW_USEDEFAULT, 0, 650, 400, NULL, NULL, hInstance, NULL);
  81.  
  82.   if(!hWnd)
  83.     return FALSE;
  84.  
  85.   ShowWindow(hWnd, nCmdShow);
  86.   UpdateWindow(hWnd);
  87.  
  88.   return TRUE;
  89. }
  90.  
  91. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  92. {
  93.   HINSTANCE hInst;
  94.  
  95.   static HWND hwndEdit;
  96.   static HWND hwndButton;
  97.   static HWND hwndListView;
  98.   static int iItem;
  99.  
  100.   switch(message)
  101.   {
  102.     case WM_COMMAND:
  103.       switch(LOWORD(wParam))
  104.       {
  105.         case IDC_BUTTON:
  106.           OnButtonClick(hWnd, hwndEdit, hwndListView);
  107.           break;
  108.        
  109.         case IDM_COPYNAME:
  110.           OnCopyClick(hwndListView, iItem, 0);
  111.           break;
  112.        
  113.         case IDM_COPYADDRESS:
  114.           OnCopyClick(hwndListView, iItem, 1);
  115.           break;
  116.        
  117.         case IDM_COPYFORWARDER:
  118.           OnCopyClick(hwndListView, iItem, 2);
  119.           break;
  120.        
  121.         case IDM_COPYORDINAL:
  122.           OnCopyClick(hwndListView, iItem, 3);
  123.           break;
  124.        
  125.         default:
  126.           return DefWindowProc(hWnd, message, wParam, lParam);
  127.       }
  128.       break;
  129.  
  130.     case WM_NOTIFY:
  131.       switch(((LPNMHDR)lParam)->code)
  132.       {
  133.       case NM_RCLICK:
  134.         LVHITTESTINFO lvhti;
  135.         LPNMITEMACTIVATE lpnmia;
  136.  
  137.         lpnmia = (LPNMITEMACTIVATE)lParam;
  138.         lvhti.pt = lpnmia->ptAction;
  139.  
  140.         if(ListView_SubItemHitTest(hwndListView, &lvhti) != -1)
  141.         {
  142.           iItem = lpnmia->iItem;
  143.           OnRightClick(hwndListView, hWnd, iItem);
  144.         }
  145.         break;
  146.  
  147.       default:
  148.         return DefWindowProc(hWnd, message, wParam, lParam);
  149.       }
  150.       break;
  151.  
  152.     case WM_CREATE:
  153.       hInst =  (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
  154.  
  155.       hwndEdit = CreateWindow("EDIT", NULL,
  156.           WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_READONLY | ES_LEFT,
  157.           0, 0, 0, 0, hWnd, (HMENU)IDC_EDIT, hInst, NULL);
  158.  
  159.       hwndButton = CreateWindow("BUTTON", "Load...", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
  160.           0, 0, 0, 0, hWnd, (HMENU)IDC_BUTTON, hInst, NULL);
  161.  
  162.       hwndListView = CreateWindow(WC_LISTVIEW, NULL, WS_CHILD | WS_VISIBLE | LVS_REPORT,
  163.           0, 0, 0, 0, hWnd, (HMENU)IDC_LISTVIEW, hInst, NULL);
  164.  
  165.       ListView_SetExtendedListViewStyle(hwndListView, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
  166.  
  167.       ResizeControls(hwndEdit, hwndButton, hwndListView, hWnd);
  168.  
  169.       CreateColumns(hwndListView);
  170.       break;
  171.    
  172.     case WM_SIZE:
  173.       ResizeControls(hwndEdit, hwndButton, hwndListView, hWnd);
  174.       break;
  175.      
  176.     case WM_DESTROY:
  177.       PostQuitMessage(0);
  178.       break;
  179.    
  180.     default:
  181.       return DefWindowProc(hWnd, message, wParam, lParam);
  182.   }
  183.  
  184.   return 0;
  185. }
  186.  
  187. void ResizeControls(HWND hwndEdit, HWND hwndButton, HWND hwndListView, HWND hWnd)
  188. {
  189.   RECT rcClient;
  190.  
  191.   GetClientRect(hWnd, &rcClient);
  192.  
  193.   MoveWindow(hwndEdit, 15, 15, rcClient.right - rcClient.left - (15 + 80 + 15 + 15), 20, TRUE);
  194.  
  195.   MoveWindow(hwndButton, rcClient.right - (15 + 80), 15, 80, 20, TRUE);
  196.  
  197.   MoveWindow(hwndListView, 15, 15 + 20 + 15, rcClient.right - rcClient.left - (15 + 15),
  198.       rcClient.bottom - rcClient.top - (15 + 20 + 15 + 15), TRUE);
  199. }
  200.  
  201. void CreateColumns(HWND hwndListView)
  202. {
  203.   LVCOLUMN lvc;
  204.   lvc.mask = LVCF_TEXT | LVCF_WIDTH;
  205.  
  206.   lvc.pszText = "Name";
  207.   lvc.cx = 225;
  208.  
  209.   ListView_InsertColumn(hwndListView, 0, &lvc);
  210.  
  211.   lvc.pszText = "Address";
  212.   lvc.cx = 75;
  213.  
  214.   ListView_InsertColumn(hwndListView, 1, &lvc);
  215.  
  216.   lvc.pszText = "Forwarder";
  217.   lvc.cx = 225;
  218.  
  219.   ListView_InsertColumn(hwndListView, 2, &lvc);
  220.  
  221.   lvc.pszText = "Ordinal";
  222.   lvc.cx = 55;
  223.  
  224.   ListView_InsertColumn(hwndListView, 3, &lvc);
  225. }
  226.  
  227. void OnButtonClick(HWND hWnd, HWND hwndEdit, HWND hwndListView)
  228. {
  229.   OPENFILENAME ofn;
  230.   char szFile[MAX_PATH];
  231.  
  232.   ZeroMemory(&ofn, sizeof(ofn));
  233.   ofn.lStructSize     = sizeof(ofn);
  234.   ofn.hwndOwner       = hWnd;
  235.   ofn.lpstrFile       = szFile;
  236.   ofn.lpstrFile[0]    = '\0';
  237.   ofn.nMaxFile        = sizeof(szFile);
  238.   ofn.lpstrFilter     = "Dynamic Link Library\0*.dll\0\0";
  239.   ofn.nFilterIndex    = 1;
  240.   ofn.lpstrFileTitle  = NULL;
  241.   ofn.nMaxFileTitle   = 0;
  242.   ofn.lpstrInitialDir = NULL;
  243.   ofn.Flags           = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  244.  
  245.   if(GetOpenFileName(&ofn))
  246.   {
  247.     Edit_SetText(hwndEdit, ofn.lpstrFile);
  248.     return InsertItems(hWnd, hwndListView, ofn.lpstrFile);
  249.   }
  250. }
  251.  
  252. void InsertItems(HWND hWnd, HWND hwndListView, LPTSTR lpstrFile)
  253. {
  254.   HMODULE hMod;
  255.   LVITEM lvI;
  256.  
  257.   PIMAGE_DOS_HEADER pdosHeader;
  258.   PIMAGE_NT_HEADERS pntHeaders;
  259.   PIMAGE_DATA_DIRECTORY pdataDirectory;
  260.   PIMAGE_EXPORT_DIRECTORY pexportDirectory;
  261.  
  262.   PWORD pwOrdinals;
  263.   PDWORD pdwFunctions;
  264.   PDWORD pdwNames;
  265.  
  266.   TCHAR szWorkBuff[20];
  267.  
  268.   ListView_DeleteAllItems(hwndListView);
  269.  
  270.   lvI.mask = LVIF_TEXT;
  271.  
  272.   hMod = LoadLibrary(lpstrFile);
  273.  
  274.   if(!hMod)
  275.   {
  276.     MessageBox(hWnd, "Could not load the dll.", "Error", MB_ICONERROR);
  277.     return;
  278.   }
  279.  
  280.   pdosHeader = (PIMAGE_DOS_HEADER)hMod;
  281.  
  282.   if(pdosHeader->e_magic != IMAGE_DOS_SIGNATURE)
  283.   {
  284.     MessageBox(hWnd, "This dll is not valid.", "Error", MB_ICONERROR);
  285.     return;
  286.   }
  287.  
  288.   pntHeaders = (PIMAGE_NT_HEADERS)((LONG)pdosHeader + pdosHeader->e_lfanew);
  289.  
  290.   if(pntHeaders->Signature != IMAGE_NT_SIGNATURE)
  291.   {
  292.     MessageBox(hWnd, "This dll is not valid.", "Error", MB_ICONERROR);
  293.     return;
  294.   }
  295.  
  296.   pdataDirectory = (PIMAGE_DATA_DIRECTORY)&pntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
  297.  
  298.   pexportDirectory = (PIMAGE_EXPORT_DIRECTORY)((DWORD)pdosHeader + pdataDirectory->VirtualAddress);
  299.  
  300.   pwOrdinals = (PWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfNameOrdinals);
  301.   pdwFunctions = (PDWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfFunctions);
  302.   pdwNames = (PDWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfNames);
  303.  
  304.   if(pdataDirectory->VirtualAddress == 0)
  305.   {
  306.     MessageBox(hWnd, "No exported functions in this dll.", "Error", MB_ICONERROR);
  307.     return;
  308.   }
  309.  
  310.   for(DWORD dw = 0; dw < pexportDirectory->NumberOfNames; dw++)
  311.   {
  312.     lvI.iItem = dw;
  313.     lvI.iSubItem = 0;
  314.     lvI.pszText = (LPTSTR)pdosHeader + pdwNames[dw];
  315.  
  316.     ListView_InsertItem(hwndListView, &lvI);
  317.  
  318.     if((DWORD)pdataDirectory->VirtualAddress < (DWORD)pdwFunctions[pwOrdinals[dw]] &&
  319.       (DWORD)pdwFunctions[pwOrdinals[dw]] < (DWORD)pdataDirectory->VirtualAddress + (DWORD)pdataDirectory->Size)
  320.     {
  321.       //it's a forwarder
  322.  
  323.       lvI.iSubItem = 2;
  324.             lvI.pszText = (LPTSTR)pdosHeader + pdwFunctions[pwOrdinals[dw]];
  325.  
  326.       ListView_SetItem(hwndListView, &lvI);
  327.  
  328.       _snprintf_s(szWorkBuff, _countof(szWorkBuff), _TRUNCATE, "%x", GetForwarderAddress((LPTSTR)pdosHeader + pdwFunctions[pwOrdinals[dw]]));
  329.      
  330.       lvI.iSubItem = 1;
  331.       lvI.pszText = szWorkBuff;
  332.      
  333.       ListView_SetItem(hwndListView, &lvI);
  334.     }
  335.     else
  336.     {
  337.       //it's not a forwarder
  338.  
  339.       _snprintf_s(szWorkBuff, _countof(szWorkBuff), _TRUNCATE, "%x", (DWORD)pdosHeader + pdwFunctions[pwOrdinals[dw]]);
  340.  
  341.       lvI.iSubItem = 1;
  342.             lvI.pszText = szWorkBuff;
  343.  
  344.       ListView_SetItem(hwndListView, &lvI);
  345.     }
  346.  
  347.     _snprintf_s(szWorkBuff, _countof(szWorkBuff), _TRUNCATE, "%u", pwOrdinals[dw] + pexportDirectory->Base);
  348.  
  349.     lvI.iSubItem = 3;
  350.         lvI.pszText = szWorkBuff;
  351.  
  352.     ListView_SetItem(hwndListView, &lvI);
  353.   }
  354.  
  355.   SetFocus(hwndListView);
  356. }
  357.  
  358. void OnRightClick(HWND hwndListView, HWND hWnd, int iItem)
  359. {
  360.   HMENU hMenu;
  361.   POINT pt;
  362.   TCHAR szBuffer[512];
  363.  
  364.   hMenu = CreatePopupMenu();
  365.  
  366.   GetCursorPos(&pt);
  367.  
  368.   ListView_GetItemText(hwndListView, iItem, 0, szBuffer, 512);
  369.  
  370.   AppendMenu(hMenu, MF_STRING | MF_DISABLED, NULL, szBuffer);
  371.   AppendMenu(hMenu, MF_SEPARATOR, NULL, NULL);
  372.   AppendMenu(hMenu, MF_STRING, IDM_COPYNAME, "Copy Name");
  373.   AppendMenu(hMenu, MF_STRING, IDM_COPYADDRESS, "Copy Address");
  374.   AppendMenu(hMenu, MF_STRING, IDM_COPYFORWARDER, "Copy Forwarder");
  375.   AppendMenu(hMenu, MF_STRING, IDM_COPYORDINAL, "Copy Ordinal");
  376.  
  377.   TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
  378. }
  379.  
  380. void OnCopyClick(HWND hwndListView, int iItem, int iSubItem)
  381. {
  382.   HGLOBAL hglbCopy;
  383.   LPTSTR lptstrCopy;
  384.   TCHAR szBuffer[512];
  385.  
  386.   ListView_GetItemText(hwndListView, iItem, iSubItem, szBuffer, 512);
  387.  
  388.   OpenClipboard(NULL);
  389.  
  390.   EmptyClipboard();
  391.  
  392.   hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (strlen(szBuffer)+1) * sizeof(TCHAR));
  393.  
  394.   lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);
  395.  
  396.   strcpy_s(lptstrCopy, strlen(szBuffer)+1, szBuffer);
  397.  
  398.   GlobalUnlock(hglbCopy);
  399.  
  400.   SetClipboardData(CF_TEXT, hglbCopy);
  401.  
  402.   CloseClipboard();
  403. }
  404.  
  405. DWORD GetForwarderAddress(LPTSTR lpszForwarder)
  406. {
  407.   TCHAR szDll[MAX_PATH];
  408.   HMODULE hMod;
  409.  
  410.   PIMAGE_DOS_HEADER pdosHeader;
  411.   PIMAGE_NT_HEADERS pntHeaders;
  412.   PIMAGE_DATA_DIRECTORY pdataDirectory;
  413.   PIMAGE_EXPORT_DIRECTORY pexportDirectory;
  414.  
  415.   PWORD pwOrdinals;
  416.   PDWORD pdwFunctions;
  417.   PDWORD pdwNames;
  418.  
  419.   int i;
  420.  
  421.   for(i = 0; (*lpszForwarder) && (*lpszForwarder != '.'); i++, lpszForwarder++)
  422.     szDll[i] = *lpszForwarder;
  423.  
  424.   szDll[i] = 0;
  425.   lpszForwarder++;
  426.  
  427.   strcat_s(szDll, _countof(szDll), ".dll");
  428.  
  429.   hMod = LoadLibrary(szDll);
  430.  
  431.   pdosHeader = (PIMAGE_DOS_HEADER)hMod;
  432.  
  433.   pntHeaders = (PIMAGE_NT_HEADERS)((LONG)pdosHeader + pdosHeader->e_lfanew);
  434.  
  435.   pdataDirectory = (PIMAGE_DATA_DIRECTORY)&pntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
  436.  
  437.   pexportDirectory = (PIMAGE_EXPORT_DIRECTORY)((DWORD)pdosHeader + pdataDirectory->VirtualAddress);
  438.  
  439.   pwOrdinals = (PWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfNameOrdinals);
  440.   pdwFunctions = (PDWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfFunctions);
  441.   pdwNames = (PDWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfNames);
  442.  
  443.   for(DWORD dw = 0; dw < pexportDirectory->NumberOfNames; dw++)
  444.   {
  445.     if(!lstrcmp((LPTSTR)pdosHeader + pdwNames[dw], lpszForwarder))
  446.       return (DWORD)pdosHeader + pdwFunctions[pwOrdinals[dw]];
  447.   }
  448.  
  449.   return 0;
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement