【问题标题】:How to find the "Saved Games" folder programmatically in C/C++?如何在 C/C++ 中以编程方式查找“保存的游戏”文件夹?
【发布时间】:2019-06-27 04:43:33
【问题描述】:

我正在写一个游戏。我打算将存档保存在“已保存的游戏”目录中。

如何以编程方式查找 Saved Games 文件夹的位置?

它需要在非英语 Windows 上工作。像%USERPROFILE%\Saved Games 这样的黑客不是一种选择。

【问题讨论】:

  • 从保存位置读取它。使用 Windows API 来确定位置。

标签: c++ c windows winapi game-development


【解决方案1】:

可以使用SHGetKnownFolderPath() 函数定位Saved Games 目录,该函数从Windows Vista 和Windows Server 2008 开始可用。

请注意,FOLDERID_SavedGames 参数是 C++ 引用。替换为 &FOLDERID_SavedGames 以从 C 代码调用。

在我能找到的第一个在线 MSVC 编译器上测试成功:

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

【讨论】:

  • 那个在线编译器太棒了。您甚至可以创建窗口。我想知道它运行在什么样的桌面/服务环境中。Ima 现在正在探索它的 GDI 功能......
猜你喜欢
  • 1970-01-01
  • 2014-11-10
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多