GetSystemRoot function
Retrieves the Windows system directory path (normally C:\Windows
).
Syntax
UINT GetSystemRoot(
LPWSTR lpBuffer,
UINT uSize
);
Parameters
Parameter | Description |
---|---|
lpBuffer | Pointer to a buffer that receives the system directory string. The buffer must be of type LPWSTR (wide-character). |
uSize | Size of the buffer, in characters (including the terminating null character). |
Return value
If the function succeeds, the return value is the length of the string copied to lpBuffer
, in characters, not including the terminating null character. If the buffer is too small, the return value is the required size, in characters, for the buffer, and the contents of lpBuffer
are undefined.
Remarks
- The path returned does not end with a backslash.
- Use
GetSystemDirectory
to retrieve the path of the system files directory (C:\Windows\System32
). - For most applications, querying the
WINDIR
environment variable yields the same result.
Example
#include <windows.h>
#include <stdio.h>
int wmain(void)
{
wchar_t path[MAX_PATH];
UINT len = GetSystemRoot(path, ARRAYSIZE(path));
if (len == 0 || len >= ARRAYSIZE(path))
{
wprintf(L"Failed to get system root.\\n");
return 1;
}
wprintf(L"System root: %s\\n", path);
return 0;
}
Requirements
- Header:
Windows.h
- Library:
Kernel32.lib
- Minimum supported client: Windows XP
© 2025 Microsoft