简体   繁体   English

如何在C ++中的Windows中从环境中读取

[英]How to read from env in Windows in c++

I want to get system folder in windows, by reading SystemRoot. 我想通过阅读SystemRoot在Windows中获取系统文件夹。 How can I do it? 我该怎么做? many thanks! 非常感谢!

If you want to read the environment variables, use getenv or GetEnvironmentVariable . 如果要读取环境变量,请使用getenvGetEnvironmentVariable

However, if you want to find the %SYSTEMROOT% directory consider using GetWindowsFolder 但是,如果要查找%SYSTEMROOT%目录,请考虑使用GetWindowsFolder


For other special folders, you can use SHGetKnownFolderPath or SHGetFolderPath 对于其他特殊文件夹,可以使用SHGetKnownFolderPathSHGetFolderPath

There's a windows API you should use instead: GetWindowsDirectory 您应该改用Windows API: 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. 但是,如果您确实想从环境中读取数据,则可以使用GetEnvironmentVariable ,或者从C运行时使用getenv ,甚至可以从VC运行时支持的非官方的第三个主要参数int main(argc, argv, envp)中获取环境指针。 。

This should be fairly easy with GetEnvironmentVariable(): 使用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) 有关更多信息和一些示例,请参见GetEnvironmentVariable()上的MSDN 示例2中使用了该函数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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