Microsoft Docs

GetSystemDirectory function

Retrieves the path of the system directory. The system directory contains system files such as dynamic-link libraries (DLLs) and executables that are essential to the OS.

Syntax

UINT WINAPI GetSystemDirectory(
    _Out_ LPTSTR lpBuffer,
    _In_  UINT   uSize
);

Parameters

ParameterTypeDescription
lpBufferLPTSTRPointer to a buffer that receives the system directory path.
uSizeUINTSize of the buffer, in TCHARs.

Return value

If the function succeeds, the return value is the length of the string copied to lpBuffer, not including the terminating null character.

If the buffer is too small, the return value is the required size, in characters, for the buffer, including the terminating null character. In this case, the contents of lpBuffer are undefined.

If the function fails, the return value is zero. Use GetLastError for extended error information.

Remarks

Example

#include <windows.h>
#include <stdio.h>

int main(void)
{
    TCHAR szPath[MAX_PATH];
    UINT len = GetSystemDirectory(szPath, MAX_PATH);
    if (len == 0 || len > MAX_PATH) {
        printf("Failed to get system directory. Error: %lu\n", GetLastError());
        return 1;
    }
    printf("System Directory: %s\n", szPath);
    return 0;
}
Output (example):
System Directory: C:\Windows\System32

See also