asas asas - 3 years ago 135
C++ Question

C++ [Windows] Path to the folder where the executable is located

I need to access some files with

fstream
in my C++ app on Windows. Those files are all located in subfolders of the folder where my exe file is located.


  • What is the easiest and more important: safest way to get the path to the folder of the current executable?


Answer Source

Use GetModuleHandle and GetModuleFileName to find out where your exe is running from.

HMODULE hModule = GetModuleHandleW(NULL);
WCHAR path[MAX_PATH];
GetModuleFileNameW(hModule, path, MAX_PATH);

Then strip the exe name from path.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download