C++

Дебаг приложения на зависимые библиотеки

Приложение покажет в консоли библиотеки что подгружает приложение.

#include <iostream>
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <psapi.h>
#include <tchar.h>
#include <strsafe.h>

using namespace std;
#define BUFSIZE 512

void enterFileName(wstring& str);
bool fileExist(string& filename);
void checkLoadedDlls(string& fileName);
BOOL GetFileNameFromHandle(HANDLE hFile);

int main()
{
    char tmp[1024] = {};
    wstring fileName;
    enterFileName(fileName);
    WideCharToMultiByte(CP_ACP, 0, fileName.data(), fileName.length(),
                        tmp, 1024, 0, 0);
    string aFileName = tmp;
    if(fileExist(aFileName))
    {
        checkLoadedDlls(aFileName);
    }
    else
        cout << "File error\n";
    
    
    cout << "Exit\n";
    return 0;
}


void enterFileName(wstring& str)
{
    cout << "Enter *.exe filename:\n";
    wcin >> str;
}


bool fileExist(string& filename)
{
    OFSTRUCT os;
    return OpenFile(filename.data(), &os, OF_EXIST) > 0? true : false;
}

void checkLoadedDlls(string& fileName)
{
    STARTUPINFOA si = {};
    PROCESS_INFORMATION pi = {};
    if(CreateProcessA(fileName.data(), 0, 0, 0, true, DEBUG_ONLY_THIS_PROCESS, 0, 0, &si, &pi))
    {
        cout << "Process created\n";
        DEBUG_EVENT de;
        de.dwProcessId = pi.dwProcessId;
        de.dwThreadId = pi.dwThreadId;
        WaitForDebugEvent(&de, INFINITE);
        while(ContinueDebugEvent(pi.dwProcessId, pi.dwThreadId, DBG_CONTINUE))
        {
            switch(de.dwDebugEventCode)
            {
            case LOAD_DLL_DEBUG_EVENT:
            {
                GetFileNameFromHandle(de.u.LoadDll.hFile);
                break;
            }
            }
            WaitForDebugEvent(&de, INFINITE);
        }
    }
}

BOOL GetFileNameFromHandle(HANDLE hFile) 
{
    BOOL bSuccess = FALSE;
    TCHAR pszFilename[MAX_PATH+1];
    HANDLE hFileMap;
    
    // Get the file size.
    DWORD dwFileSizeHi = 0;
    DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi); 
    
    if( dwFileSizeLo == 0 && dwFileSizeHi == 0 )
    {
        cout << "Cannot map a file with a length of zero.\n";
        return FALSE;
    }
    
    // Create a file mapping object.
    hFileMap = CreateFileMapping(hFile, 
                    NULL, 
                    PAGE_READONLY,
                    0, 
                    1,
                    NULL);
    
    if (hFileMap) 
    {
        // Create a file mapping to get the file name.
        void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
    
        if (pMem) 
        {
            if (GetMappedFileName (GetCurrentProcess(), 
                                 pMem, 
                                 pszFilename,
                                 MAX_PATH)) 
            {
                wstring fileName = pszFilename;
                wstring::iterator begin = fileName.begin();
                wstring::iterator end = begin;
                for(int i = 0; i < 23; i++)
                    end++;
                fileName.erase(begin, end);
                wcout << fileName << endl;
            }
            bSuccess = TRUE;
            UnmapViewOfFile(pMem);
        } 
    
        CloseHandle(hFileMap);
    }
    //cout << pszFilename << endl;
    return(bSuccess);
}
Добавлено: 22 Сентября 2013 09:09:27 Добавил: Андрей Ковальчук Нравится 0
Добавить
Комментарии:
Нету комментариев для вывода...