开发者

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

开发者 https://www.devze.com 2022-12-27 05:18 出处:网络
This question already has answers here: Get path of executable (25 answers) Closed 9 years ago. 开发者_如何学编程I need to access some files with fstream in my C++ app on Windows. Those f
This question already has answers here: Get path of executable (25 answers) Closed 9 years ago. 开发者_如何学编程

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?


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

WCHAR path[MAX_PATH];
GetModuleFileNameW(NULL, path, MAX_PATH);

Then strip the exe name from path.


GetThisPath.h

/// dest is expected to be MAX_PATH in length.
/// returns dest
///     TCHAR dest[MAX_PATH];
///     GetThisPath(dest, MAX_PATH);
TCHAR* GetThisPath(TCHAR* dest, size_t destSize);

GetThisPath.cpp

#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

TCHAR* GetThisPath(TCHAR* dest, size_t destSize)
{
    if (!dest) return NULL;
    if (MAX_PATH > destSize) return NULL;

    DWORD length = GetModuleFileName( NULL, dest, destSize );
    PathRemoveFileSpec(dest);
    return dest;
}

mainProgram.cpp

TCHAR dest[MAX_PATH];
GetThisPath(dest, MAX_PATH);

Update: PathRemoveFileSpec is deprecated in Windows 8. However the replacement, PathCchRemoveFileSpec, is available in Windows 8+ only. (Thanks to @askalee for the comment)

I think this code below might work, but I'm leaving the above code up there until the below code is vetted. I don't have a compiler set up to test this at the moment. If you have a chance to test this code, please post a comment saying if this below code worked and on what operating system you tested. Thanks!

GetThisPath.h

/// dest is expected to be MAX_PATH in length.
/// returns dest
///     TCHAR dest[MAX_PATH];
///     GetThisPath(dest, MAX_PATH);
TCHAR* GetThisPath(TCHAR* dest, size_t destSize);

GetThisPath.cpp

#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

TCHAR* GetThisPath(TCHAR* dest, size_t destSize)
{
    if (!dest) return NULL;

    DWORD length = GetModuleFileName( NULL, dest, destSize );
#if (NTDDI_VERSION >= NTDDI_WIN8)
    PathCchRemoveFileSpec(dest, destSize);
#else
    if (MAX_PATH > destSize) return NULL;
    PathRemoveFileSpec(dest);
#endif
    return dest;
}

mainProgram.cpp

TCHAR dest[MAX_PATH];
GetThisPath(dest, MAX_PATH);
  • NTDDI_WIN8 from this answer
  • Thanks @Warpspace for the suggested change!


By default, the directory that the exe is run from should be the starting location. So opening a file in a subfolder should be as easy as

fstream infile; 
infile.open(".\\subfolder\\filename.ext");

from within your program.

However, there is no real way to GUARANTEE this will always work unless you either use a framework that wraps the needed features (I'd look at boost), or using the Windows API directly such as GetModuleFileName (as sean e suggested)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号