GetModuleFileNameA()与GetCurrentDirectoryA()

本文详细介绍了在Windows环境下使用C/C++获取程序运行时路径与当前目录的方法,包括使用`GetModuleFileNameA`和`GetCurrentDirectoryA`函数,并通过实例展示了如何解析路径并获取工作目录。

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

头文件: #include <windows.h>

GetModuleFileNameA()
char moduleFileName[MAX_PATH];     
GetModuleFileNameA(0, moduleFileName, MAX_PATH);

取得的路径为:c:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects 
\Test\debug\Test.exe

 

    char moduleFileName[MAX_PATH];     
    GetModuleFileNameA(NULL, moduleFileName, MAX_PATH);     
    std::string workingFolder(moduleFileName);     
    size_t pos = workingFolder.find_last_of("\\");     
    workingFolder = workingFolder.substr(0, pos+1);     
    std::cout<<workingFolder<<std::endl;    

取得的路径为:c:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects 
\Test\debug\

 

GetCurrentDirectoryA()
char result[MAX_PATH];     
GetCurrentDirectoryA(MAX_PATH, result);

取得的路径为:c:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects 
\Test\Test 

转载于:https://www.cnblogs.com/lxt287994374/p/3863303.html

详细注释函数 CSymbols::CSymbols(const char* SymbolsPatch) { m_ZwQuerySystemInformation =(ZWQUERYSYSTEMINFORMATION)GetProcAddress(GetModuleHandle(L"ntdll.dll"), "ZwQuerySystemInformation"); m_SymbolsPatch = SymbolsPatch; Module_INFO Module; if (!GetKernelModuleInfo(&Module)) { MessageBox(0, L"GetKernelModuleInfo error!", L"", 0); } DWORD Options = SymGetOptions(); Options = Options | SYMOPT_DEBUG; SymSetOptions(Options); m_hProcess = GetCurrentProcess(); //BOOL bRet = SymInitialize(m_hProcess, 0, FALSE); //if (!bRet) //{ // MessageBox(0, L"SymInitialize error!", L"", 0); // return ; //} if (!InitSymHandler1()) { MessageBox(0, L"SymInitialize error!", L"", 0); return; } if (m_SymbolsPatch==0) { MessageBox(0,L"m_SymbolsPatch error",L"",0); return ; } if (!SymSetSearchPath(m_hProcess, m_SymbolsPatch)) { MessageBox(0,L"SymSetSearchPath error!",L"",0); return ; } HMODULE hDll = LoadLibraryEx(TEXT("ntoskrnl.exe"), NULL, DONT_RESOLVE_DLL_REFERENCES); char szFile[MAX_PATH], SymFile[MAX_PATH] = {""}; char SymFile1[MAX_PATH] = { "" }; //MODULEINFO ModInfo; GetModuleFileNameA(hDll, szFile, sizeof(szFile) / sizeof(szFile[0])); char currentDir[260]; GetCurrentDirectoryA(260, currentDir); //char szcurrFile[MAX_PATH]; //GetModuleFileNameA(NULL, szcurrFile, sizeof(szcurrFile) / sizeof(szcurrFile[0])); //HANDLE hcurr = GetModuleHandleA(szcurrFile); //char SymFile[MAX_PATH] = {""}; char SymFile1[MAX_PATH] = { "" }; if (!SymGetSymbolFile(m_hProcess, NULL, szFile, sfPdb, SymFile, MAX_PATH, SymFile1, MAX_PATH)) { int err = GetLastError(); char msg[260]; sprintf(msg, "SymGetSymbolFile error:%d", err); MessageBoxA(0, msg, "", 0); return; } MessageBoxA(0, "OK", "", 0); return; char FileName[MAX_PATH]; GetSystemDirectoryA(FileName, sizeof(FileName)); strcat_s(FileName, "\\"); strcat_s(FileName, Module.KernelName); HANDLE hFile = CreateFileA(FileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (hFile==INVALID_HANDLE_VALUE) { MessageBox(0,L"CreateFileA error!",L"",0); return ; } DWORD dwfilesize = GetFileSize(hFile, NULL); m_BaseOfDll = SymLoadModule64(m_hProcess, hFile, FileName, NULL, (DWORD64)Module.KernelBass, dwfilesize); CloseHandle(hFile); if (m_BaseOfDll == 0) { //printf("SymLoadModule64:%d\n", GetLastError()); MessageBox(0,L"SymLoadModule64 error!",L"",0); return ; } }
最新发布
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值