views:

55

answers:

3

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

+1  A: 

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.

Rup
+1 for good answer and the Microsoft '3rd argument to main' extension - it's always nice to learn something new.
Joe Gauterin
It's not just Microsoft - pretty much everything does have envp. GNU Make relies on it even and that's supposed to just work out-of-the-box everywhere. The only system I've ever seen without it was Interix 2 (3+ has it though) and there's an exception in GNU Make for AmigaOS too I think.
Rup
A: 

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)

Strayer
+3  A: 

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

Joe Gauterin