Find Function in exports

本文介绍了一个C++程序,该程序用于在指定目录下查找含有特定导出函数的库文件。程序通过递归遍历目录,并利用dumpbin工具来检查每个符合类型的文件(如.dll, .lib等),最终定位到包含目标函数的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// skip the fucking head "顶"

// ""

Find Function In Exports

/* 0*/    #include <tchar.h>
/* 1*/    #include <windows.h>
/* 2*/    #include <string>
/* 3*/    #include <vector>
/* 4*/    #include <iostream>
/* 5*/    #include <fstream>
/* 6*/   
/* 7*/   
/* 8*/    template<typename F>
/* 9*/    void IterDirectory(F& f)
/*10*/    {
/*11*/        WIN32_FIND_DATA findData;
/*12*/        HANDLE handle = FindFirstFile(f.mDirectory.c_str(), &findData);
/*13*/        if (handle != INVALID_HANDLE_VALUE) {
/*14*/            do {
/*15*/                f(findData.cFileName);
/*16*/            } while (FindNextFile(handle, &findData) != 0);
/*17*/        }
/*18*/   
/*19*/        DWORD dwError = GetLastError();
/*20*/        if (dwError != ERROR_NO_MORE_FILES)
/*21*/            std::wcout << std::wstring(L"FindNextFile error: ").c_str() << dwError << std::endl;
/*22*/        FindClose(handle);
/*23*/    }
/*24*/   
/*25*/    class Find
/*26*/    {
/*27*/    public:
/*28*/        Find(const wchar_t* directory, const wchar_t* matchString)
/*29*/            : mDirectory(directory)
/*30*/            , mMatchString(matchString)
/*31*/            , mSpace(70, L' ')
/*32*/        {}
/*33*/   
/*34*/        void operator()(const wchar_t* name)
/*35*/        {
/*36*/            std::wcout << L'/r' << mSpace << L'/r' << name;
/*37*/            if (dumpToFile(name))
/*38*/                findInFile(name);
/*39*/            std::wcout << L'/r' << mSpace;
/*40*/        }
/*41*/   
/*42*/        std::wstring mDirectory;
/*43*/   
/*44*/    private:
/*45*/        bool dumpToFile(const wchar_t* name)
/*46*/        {
/*47*/            std::wstring fileName(name);
/*48*/   
/*49*/            if (fileName.size() < 4)
/*50*/                return false;
/*51*/            std::wstring type = fileName.substr(fileName.size() - 4);
/*52*/            if (type != L".lib" && type != L".dll" && type != L".arx" && type != L".dbx" && type != L".exe")
/*53*/                return false;
/*54*/   
/*55*/            STARTUPINFO si;
/*56*/            PROCESS_INFORMATION pi;
/*57*/   
/*58*/            ZeroMemory( &si, sizeof(si) );
/*59*/            si.cb = sizeof(si);
/*60*/            ZeroMemory( &pi, sizeof(pi) );
/*61*/   
/*62*/            std::wstring command = L"dumpbin.exe -EXPORTS /"" + mDirectory + fileName + L"/" /OUT:C://a.txt";
/*63*/   
/*64*/            if(!CreateProcess(NULL, (wchar_t*)command.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
/*65*/                std::wcout << L"CreateProcess failed " << GetLastError() << std::endl;
/*66*/                return false;
/*67*/            }
/*68*/   
/*69*/            WaitForSingleObject( pi.hProcess, INFINITE );
/*70*/            CloseHandle( pi.hProcess );
/*71*/            CloseHandle( pi.hThread );
/*72*/            return true;
/*73*/        }
/*74*/   
/*75*/        bool findInFile(const wchar_t* name)
/*76*/        {
/*77*/            std::wifstream input(L"c://a.txt");
/*78*/            std::wstring world;
/*79*/            while (input >> world) {
/*80*/                if(world.find(mMatchString) != std::wstring::npos) {
/*81*/                    std::wcout << L'/r' << mSpace << L'/r' << name
/*82*/                               << L'/t' << world  << std::endl;
/*83*/                    return true;
/*84*/                }
/*85*/            }
/*86*/            return false;
/*87*/        }
/*88*/   
/*89*/        std::wstring mMatchString;
/*90*/        std::wstring mSpace;
/*91*/    };
/*92*/   
/*93*/    int _tmain(int argc, _TCHAR* argv[])
/*94*/    {
/*95*/        wchar_t envVar[512];
/*96*/        if (argc > 1 && GetEnvironmentVariable(L"FF_PATH", envVar, 512) < 512)
/*97*/            IterDirectory(Find(envVar, argv[1]));
/*98*/        return 0;
/*99*/    }

Add Line Number

/* 0*/    #include <tchar.h>
/* 1*/    #include <fstream>
/* 2*/   
/* 3*/    int main()
/* 4*/    {
/* 5*/        std::wifstream ifstream("source.cpp");
/* 6*/        std::wofstream ofstream("addLineNumber.cpp");
/* 7*/        std::wifstream::char_type line[1024];
/* 8*/        int i = 0;
/* 9*/        while (ifstream.getline(line, 1024)) {
/*10*/            ofstream << _T("/*");
/*11*/            ofstream.width(2);
/*12*/            ofstream << i++ << _T("*/    ") << line << _T('/n');
/*13*/        }
/*14*/    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值