Your code seeks to create a file in the current directory, but an exception occurs at GetCurrentDirectory(). To understand the issue, let's delve into the parameters used:
Additionally, retrieving the executable path using GetCurrentDirectory() is not reliable. Utilize the GetModuleFileName function instead:
TCHAR buffer[MAX_PATH] = { 0 }; GetModuleFileName(NULL, buffer, MAX_PATH);
For a more comprehensive approach, consider the following function that extracts the directory without the file name:
#include <windows.h> #include <string> #include <iostream> std::wstring ExePath() { TCHAR buffer[MAX_PATH] = { 0 }; GetModuleFileName(NULL, buffer, MAX_PATH); std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\/"); return std::wstring(buffer).substr(0, pos); }
The above is the detailed content of Why is GetCurrentDirectory() throwing an exception when trying to create a file?. For more information, please follow other related articles on the PHP Chinese website!