简体   繁体   中英

How to read from env in Windows in c++

I want to get system folder in windows, by reading SystemRoot. How can I do it? many thanks!

If you want to read the environment variables, use getenv or GetEnvironmentVariable .

However, if you want to find the %SYSTEMROOT% directory consider using GetWindowsFolder


For other special folders, you can use SHGetKnownFolderPath or SHGetFolderPath

There's a windows API you should use instead: GetWindowsDirectory

But if you do want to read from the environment you can use GetEnvironmentVariable , or from the C runtime with getenv or even get the environment pointer from the unofficial third main argument int main(argc, argv, envp) which is supported by the VC runtime.

This should be fairly easy with GetEnvironmentVariable():

DWORD WINAPI GetEnvironmentVariable(
  __in_opt   LPCTSTR lpName,
  __out_opt  LPTSTR lpBuffer,
  __in       DWORD nSize
);

See MSDN on GetEnvironmentVariable() for more infos and some examples (the function is used in Example 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM