Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <windowsx.h>
- #include <commctrl.h>
- #include <stdio.h>
- #define WINDOWCLASS "LISTEXPORTEDFUNCTIONS"
- #define TITLE "List Exported Functions"
- #define IDC_EDIT 101
- #define IDC_BUTTON 102
- #define IDC_LISTVIEW 103
- #define IDM_COPYNAME 201
- #define IDM_COPYADDRESS 202
- #define IDM_COPYFORWARDER 203
- #define IDM_COPYORDINAL 204
- ATOM MyRegisterClass(HINSTANCE);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- void ResizeControls(HWND, HWND, HWND, HWND);
- void CreateColumns(HWND);
- void OnButtonClick(HWND, HWND, HWND);
- void InsertItems(HWND, HWND, LPTSTR);
- void OnRightClick(HWND, HWND, int);
- void OnCopyClick(HWND, int, int);
- DWORD GetForwarderAddress(LPTSTR);
- int APIENTRY WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- MSG msg;
- INITCOMMONCONTROLSEX icex;
- icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
- icex.dwICC = ICC_LISTVIEW_CLASSES;
- InitCommonControlsEx(&icex);
- MyRegisterClass(hInstance);
- if(!InitInstance(hInstance, nCmdShow))
- return EXIT_FAILURE;
- while(GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return (int)msg.wParam;
- }
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = WINDOWCLASS;
- wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- return RegisterClassEx(&wcex);
- }
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- hWnd = CreateWindow(WINDOWCLASS, TITLE, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, 650, 400, NULL, NULL, hInstance, NULL);
- if(!hWnd)
- return FALSE;
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- HINSTANCE hInst;
- static HWND hwndEdit;
- static HWND hwndButton;
- static HWND hwndListView;
- static int iItem;
- switch(message)
- {
- case WM_COMMAND:
- switch(LOWORD(wParam))
- {
- case IDC_BUTTON:
- OnButtonClick(hWnd, hwndEdit, hwndListView);
- break;
- case IDM_COPYNAME:
- OnCopyClick(hwndListView, iItem, 0);
- break;
- case IDM_COPYADDRESS:
- OnCopyClick(hwndListView, iItem, 1);
- break;
- case IDM_COPYFORWARDER:
- OnCopyClick(hwndListView, iItem, 2);
- break;
- case IDM_COPYORDINAL:
- OnCopyClick(hwndListView, iItem, 3);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- case WM_NOTIFY:
- switch(((LPNMHDR)lParam)->code)
- {
- case NM_RCLICK:
- LVHITTESTINFO lvhti;
- LPNMITEMACTIVATE lpnmia;
- lpnmia = (LPNMITEMACTIVATE)lParam;
- lvhti.pt = lpnmia->ptAction;
- if(ListView_SubItemHitTest(hwndListView, &lvhti) != -1)
- {
- iItem = lpnmia->iItem;
- OnRightClick(hwndListView, hWnd, iItem);
- }
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- case WM_CREATE:
- hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
- hwndEdit = CreateWindow("EDIT", NULL,
- WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_READONLY | ES_LEFT,
- 0, 0, 0, 0, hWnd, (HMENU)IDC_EDIT, hInst, NULL);
- hwndButton = CreateWindow("BUTTON", "Load...", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
- 0, 0, 0, 0, hWnd, (HMENU)IDC_BUTTON, hInst, NULL);
- hwndListView = CreateWindow(WC_LISTVIEW, NULL, WS_CHILD | WS_VISIBLE | LVS_REPORT,
- 0, 0, 0, 0, hWnd, (HMENU)IDC_LISTVIEW, hInst, NULL);
- ListView_SetExtendedListViewStyle(hwndListView, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
- ResizeControls(hwndEdit, hwndButton, hwndListView, hWnd);
- CreateColumns(hwndListView);
- break;
- case WM_SIZE:
- ResizeControls(hwndEdit, hwndButton, hwndListView, hWnd);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- void ResizeControls(HWND hwndEdit, HWND hwndButton, HWND hwndListView, HWND hWnd)
- {
- RECT rcClient;
- GetClientRect(hWnd, &rcClient);
- MoveWindow(hwndEdit, 15, 15, rcClient.right - rcClient.left - (15 + 80 + 15 + 15), 20, TRUE);
- MoveWindow(hwndButton, rcClient.right - (15 + 80), 15, 80, 20, TRUE);
- MoveWindow(hwndListView, 15, 15 + 20 + 15, rcClient.right - rcClient.left - (15 + 15),
- rcClient.bottom - rcClient.top - (15 + 20 + 15 + 15), TRUE);
- }
- void CreateColumns(HWND hwndListView)
- {
- LVCOLUMN lvc;
- lvc.mask = LVCF_TEXT | LVCF_WIDTH;
- lvc.pszText = "Name";
- lvc.cx = 225;
- ListView_InsertColumn(hwndListView, 0, &lvc);
- lvc.pszText = "Address";
- lvc.cx = 75;
- ListView_InsertColumn(hwndListView, 1, &lvc);
- lvc.pszText = "Forwarder";
- lvc.cx = 225;
- ListView_InsertColumn(hwndListView, 2, &lvc);
- lvc.pszText = "Ordinal";
- lvc.cx = 55;
- ListView_InsertColumn(hwndListView, 3, &lvc);
- }
- void OnButtonClick(HWND hWnd, HWND hwndEdit, HWND hwndListView)
- {
- OPENFILENAME ofn;
- char szFile[MAX_PATH];
- ZeroMemory(&ofn, sizeof(ofn));
- ofn.lStructSize = sizeof(ofn);
- ofn.hwndOwner = hWnd;
- ofn.lpstrFile = szFile;
- ofn.lpstrFile[0] = '\0';
- ofn.nMaxFile = sizeof(szFile);
- ofn.lpstrFilter = "Dynamic Link Library\0*.dll\0\0";
- ofn.nFilterIndex = 1;
- ofn.lpstrFileTitle = NULL;
- ofn.nMaxFileTitle = 0;
- ofn.lpstrInitialDir = NULL;
- ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
- if(GetOpenFileName(&ofn))
- {
- Edit_SetText(hwndEdit, ofn.lpstrFile);
- return InsertItems(hWnd, hwndListView, ofn.lpstrFile);
- }
- }
- void InsertItems(HWND hWnd, HWND hwndListView, LPTSTR lpstrFile)
- {
- HMODULE hMod;
- LVITEM lvI;
- PIMAGE_DOS_HEADER pdosHeader;
- PIMAGE_NT_HEADERS pntHeaders;
- PIMAGE_DATA_DIRECTORY pdataDirectory;
- PIMAGE_EXPORT_DIRECTORY pexportDirectory;
- PWORD pwOrdinals;
- PDWORD pdwFunctions;
- PDWORD pdwNames;
- TCHAR szWorkBuff[20];
- ListView_DeleteAllItems(hwndListView);
- lvI.mask = LVIF_TEXT;
- hMod = LoadLibrary(lpstrFile);
- if(!hMod)
- {
- MessageBox(hWnd, "Could not load the dll.", "Error", MB_ICONERROR);
- return;
- }
- pdosHeader = (PIMAGE_DOS_HEADER)hMod;
- if(pdosHeader->e_magic != IMAGE_DOS_SIGNATURE)
- {
- MessageBox(hWnd, "This dll is not valid.", "Error", MB_ICONERROR);
- return;
- }
- pntHeaders = (PIMAGE_NT_HEADERS)((LONG)pdosHeader + pdosHeader->e_lfanew);
- if(pntHeaders->Signature != IMAGE_NT_SIGNATURE)
- {
- MessageBox(hWnd, "This dll is not valid.", "Error", MB_ICONERROR);
- return;
- }
- pdataDirectory = (PIMAGE_DATA_DIRECTORY)&pntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
- pexportDirectory = (PIMAGE_EXPORT_DIRECTORY)((DWORD)pdosHeader + pdataDirectory->VirtualAddress);
- pwOrdinals = (PWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfNameOrdinals);
- pdwFunctions = (PDWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfFunctions);
- pdwNames = (PDWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfNames);
- if(pdataDirectory->VirtualAddress == 0)
- {
- MessageBox(hWnd, "No exported functions in this dll.", "Error", MB_ICONERROR);
- return;
- }
- for(DWORD dw = 0; dw < pexportDirectory->NumberOfNames; dw++)
- {
- lvI.iItem = dw;
- lvI.iSubItem = 0;
- lvI.pszText = (LPTSTR)pdosHeader + pdwNames[dw];
- ListView_InsertItem(hwndListView, &lvI);
- if((DWORD)pdataDirectory->VirtualAddress < (DWORD)pdwFunctions[pwOrdinals[dw]] &&
- (DWORD)pdwFunctions[pwOrdinals[dw]] < (DWORD)pdataDirectory->VirtualAddress + (DWORD)pdataDirectory->Size)
- {
- //it's a forwarder
- lvI.iSubItem = 2;
- lvI.pszText = (LPTSTR)pdosHeader + pdwFunctions[pwOrdinals[dw]];
- ListView_SetItem(hwndListView, &lvI);
- _snprintf_s(szWorkBuff, _countof(szWorkBuff), _TRUNCATE, "%x", GetForwarderAddress((LPTSTR)pdosHeader + pdwFunctions[pwOrdinals[dw]]));
- lvI.iSubItem = 1;
- lvI.pszText = szWorkBuff;
- ListView_SetItem(hwndListView, &lvI);
- }
- else
- {
- //it's not a forwarder
- _snprintf_s(szWorkBuff, _countof(szWorkBuff), _TRUNCATE, "%x", (DWORD)pdosHeader + pdwFunctions[pwOrdinals[dw]]);
- lvI.iSubItem = 1;
- lvI.pszText = szWorkBuff;
- ListView_SetItem(hwndListView, &lvI);
- }
- _snprintf_s(szWorkBuff, _countof(szWorkBuff), _TRUNCATE, "%u", pwOrdinals[dw] + pexportDirectory->Base);
- lvI.iSubItem = 3;
- lvI.pszText = szWorkBuff;
- ListView_SetItem(hwndListView, &lvI);
- }
- SetFocus(hwndListView);
- }
- void OnRightClick(HWND hwndListView, HWND hWnd, int iItem)
- {
- HMENU hMenu;
- POINT pt;
- TCHAR szBuffer[512];
- hMenu = CreatePopupMenu();
- GetCursorPos(&pt);
- ListView_GetItemText(hwndListView, iItem, 0, szBuffer, 512);
- AppendMenu(hMenu, MF_STRING | MF_DISABLED, NULL, szBuffer);
- AppendMenu(hMenu, MF_SEPARATOR, NULL, NULL);
- AppendMenu(hMenu, MF_STRING, IDM_COPYNAME, "Copy Name");
- AppendMenu(hMenu, MF_STRING, IDM_COPYADDRESS, "Copy Address");
- AppendMenu(hMenu, MF_STRING, IDM_COPYFORWARDER, "Copy Forwarder");
- AppendMenu(hMenu, MF_STRING, IDM_COPYORDINAL, "Copy Ordinal");
- TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, hWnd, NULL);
- }
- void OnCopyClick(HWND hwndListView, int iItem, int iSubItem)
- {
- HGLOBAL hglbCopy;
- LPTSTR lptstrCopy;
- TCHAR szBuffer[512];
- ListView_GetItemText(hwndListView, iItem, iSubItem, szBuffer, 512);
- OpenClipboard(NULL);
- EmptyClipboard();
- hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (strlen(szBuffer)+1) * sizeof(TCHAR));
- lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);
- strcpy_s(lptstrCopy, strlen(szBuffer)+1, szBuffer);
- GlobalUnlock(hglbCopy);
- SetClipboardData(CF_TEXT, hglbCopy);
- CloseClipboard();
- }
- DWORD GetForwarderAddress(LPTSTR lpszForwarder)
- {
- TCHAR szDll[MAX_PATH];
- HMODULE hMod;
- PIMAGE_DOS_HEADER pdosHeader;
- PIMAGE_NT_HEADERS pntHeaders;
- PIMAGE_DATA_DIRECTORY pdataDirectory;
- PIMAGE_EXPORT_DIRECTORY pexportDirectory;
- PWORD pwOrdinals;
- PDWORD pdwFunctions;
- PDWORD pdwNames;
- int i;
- for(i = 0; (*lpszForwarder) && (*lpszForwarder != '.'); i++, lpszForwarder++)
- szDll[i] = *lpszForwarder;
- szDll[i] = 0;
- lpszForwarder++;
- strcat_s(szDll, _countof(szDll), ".dll");
- hMod = LoadLibrary(szDll);
- pdosHeader = (PIMAGE_DOS_HEADER)hMod;
- pntHeaders = (PIMAGE_NT_HEADERS)((LONG)pdosHeader + pdosHeader->e_lfanew);
- pdataDirectory = (PIMAGE_DATA_DIRECTORY)&pntHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
- pexportDirectory = (PIMAGE_EXPORT_DIRECTORY)((DWORD)pdosHeader + pdataDirectory->VirtualAddress);
- pwOrdinals = (PWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfNameOrdinals);
- pdwFunctions = (PDWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfFunctions);
- pdwNames = (PDWORD)((DWORD)pdosHeader + pexportDirectory->AddressOfNames);
- for(DWORD dw = 0; dw < pexportDirectory->NumberOfNames; dw++)
- {
- if(!lstrcmp((LPTSTR)pdosHeader + pdwNames[dw], lpszForwarder))
- return (DWORD)pdosHeader + pdwFunctions[pwOrdinals[dw]];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement