Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the "Saved Games" folder programmatically in C/C++?

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.

like image 607
user5994461 Avatar asked Feb 03 '19 01:02

user5994461


1 Answers

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;
}
like image 96
user5994461 Avatar answered Oct 19 '22 20:10

user5994461