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

倾然丶 夕夏残阳落幕 提交于 2019-12-24 01:09:19

问题


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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/54499256/how-to-find-the-saved-games-folder-programmatically-in-c-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!