开发者

Using Windows Environment Variable in Native Code

开发者 https://www.devze.com 2023-02-06 06:51 出处:网络
// I have a permission to create a file to Program Files const std::wstring sFileName = L\"%ProgramFiles%\\\\aaa.txt\";
// I have a permission to create a file to Program Files
const std::wstring sFileName = L"%ProgramFiles%\\aaa.txt";
HANDLE h 开发者_高级运维= CreateFileW(
  sFileName.c_str(),
  GENERIC_READ,
  FILE_SHARE_DELETE,
  0,
  CREATE_ALWAYS,
  0,
  0);
// INVALID_HANDLE_VALUE with ERROR_PATH_NOT_FOUND

I expected it makes C:\Program Files\aaa.txt or C:\Program Files (x86)\aaa.txt depend on the Os' environment. But it didn't work.

What's wrong? How do I use environment variable in native code?


You need to explicitly expand the environment variables. One way is using the Win32 API's ExpandEnvironmentStrings or ExpandEnvironmentStringsForUser


try using the GetEnvironmentVariable function

0

精彩评论

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