Skip to content

List System Directories

Four small C functions that return the current, temp, Windows, and system directories, each using a static buffer ring to stay safe across repeated calls.

Published Aug 16, 1998

A quick reference for the four directory-lookup APIs every Win32 program eventually needs: GetCurrentDirectory, GetTempPath, GetWindowsDirectory, and GetSystemDirectory. Each wrapper copies the result into one slot of a 64-deep static buffer ring so the returned pointer stays valid across many interleaved calls - the same pattern used throughout the older NetworkDLS code. Drop these into any project that wants short-lived string handles to the standard locations without managing its own buffers.

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

extern char * Get_CurrentDirectory();
extern char * Get_TempDirectory();
extern char * Get_WindowsDirectory();
extern char * Get_SystemDirectory();

int main()
{
    printf("%s%s\n","The Current Directory is: ",Get_CurrentDirectory());
    printf("%s%s\n"," The System Directory is: ",Get_SystemDirectory());
    printf("%s%s\n","The Windows Directory is: ",Get_WindowsDirectory());
    printf("%s%s\n","   The Temp Directory is: ",Get_TempDirectory());
    return 0;
}

//////////////////////////////////////////////////////////////////////

extern char * Get_CurrentDirectory ()
{
    static char StrFunction [64][257];
    static int StrCnt;
    static char T [257];
    GetCurrentDirectory(256,T);
    if (++StrCnt==64) StrCnt = 0;
    sprintf(StrFunction[StrCnt],"%s",T);
    return StrFunction[StrCnt];
}

/////////////////////////////////////////////////////////////////////

extern char * Get_TempDirectory ()
{
    static char StrFunction [64][257];
    static int StrCnt;
    static char T [257];
    GetTempPath (256,T);
    if (++StrCnt==64) StrCnt = 0;
    sprintf(StrFunction[StrCnt],"%s",T);
    return StrFunction[StrCnt];
}

/////////////////////////////////////////////////////////////////////

extern char * Get_WindowsDirectory ()
{
    static char StrFunction [64][257];
    static int StrCnt;
    static char T [257];
    GetWindowsDirectory (T,256);
    if (++StrCnt==64) StrCnt = 0;
    sprintf(StrFunction[StrCnt],"%s",T);
    return StrFunction[StrCnt];
}

/////////////////////////////////////////////////////////////////////

extern char * Get_SystemDirectory ()
{
    static char StrFunction [64][257];
    static int StrCnt;
    static char T [257];
    GetSystemDirectory (T,256);
    if (++StrCnt==64) StrCnt = 0;
    sprintf(StrFunction[StrCnt],"%s",T);
    return StrFunction[StrCnt];
}

On modern systems prefer the wide-character variants (GetWindowsDirectoryW etc.) or the SHARP/SHGetFolderPath family if you need per-user locations like Documents.

An unhandled error has occurred. Reload 🗙