6

I am writing a game. I am planning to store saves in the "saved games" directory.

How to find the location of the Saved Games folder programmatically?

It needs to work on non-English Windows. Hacks like %USERPROFILE%\Saved Games are not an option.

user5994461
  • 5,301
  • 1
  • 36
  • 57

1 Answers1

14

The Saved Games directory can be located with the SHGetKnownFolderPath() function, available since Windows Vista and Windows Server 2008.

Note that the FOLDERID_SavedGames argument is a C++ reference. Replace with &FOLDERID_SavedGames to call from C code.

Tested successfully on the first online MSVC compiler I could find:

https://rextester.com/l/cpp_online_compiler_visual

#define WINVER 0x0600
#define _WIN32_WINNT 0x0600

#include <stdio.h>
#include <shlobj.h>
#include <objbase.h>

#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "ole32.lib")

int main(void)
{
    PWSTR path = NULL;
    HRESULT r;

    r = SHGetKnownFolderPath(FOLDERID_SavedGames, KF_FLAG_CREATE, NULL, &path);
    if (path != NULL)
    {
        printf("%ls", path);
        CoTaskMemFree(path);
    }

    return 0;
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
user5994461
  • 5,301
  • 1
  • 36
  • 57
  • That online compiler is amazing. You can even create windows. I wonder what kind of desktop / service environment its running in.Ima probe its GDI capabilities now... – Chris Becke Feb 03 '19 at 17:38