MSDN

Microsoft Docs

GetSystemRoot function

Retrieves the Windows system directory path (normally C:\Windows).

Syntax

UINT GetSystemRoot(
    LPWSTR lpBuffer,
    UINT   uSize
);

Parameters

ParameterDescription
lpBufferPointer to a buffer that receives the system directory string. The buffer must be of type LPWSTR (wide-character).
uSizeSize 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

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


© 2025 Microsoft