我应该如何让配置文件的绝对路径与windows.exe一起发布?

问题描述 投票:0回答:1

我正在用C++制作一个windows应用程序。有一个API需要一个配置文件,也需要该配置文件的绝对路径。( https:/github.comValveSoftwareopenvrwikiAction-manifest。 ). 如果我了解发布可执行文件的预期做法,我就会更容易推理出这个问题。

我是否应该将MyApp.exe打包在一个名为MyApp的文件夹中,MyApp.exe在根目录下,所有的资源config都在它旁边?这是否意味着,当运行时,所有从可执行文件中引用的相对路径都应该是相对于MyApp文件夹的?如何获得所有相对路径相对的文件夹的绝对路径? (我可以通过简单地将绝对路径与配置文件的相对路径连接起来来获得配置文件的完整绝对路径,我应该可以控制...)

编辑:澄清一下,API 要求文件路径是绝对的. 请看链接。"必须提供文件的完整路径,不接受相对路径。"我不是在寻找c++的工作方法,让我不 需要 绝对文件路径。我需要找到一种方法来获取绝对文件路径,因为它是API的一个约束条件。

c++ windows resources config openvr
1个回答
2
投票

下面是你在Windows上的操作方法。

#include <Windows.h>
#include <iostream>

int main(){
    /*If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process (.exe file).*/
    HMODULE selfmodule = GetModuleHandleA(0);

    char absolutepath[MAX_PATH] = {0};

    uint32_t length = GetModuleFileNameA(selfmodule,absolutepath,sizoef(absolutepath));

    //lets assume our directory is C:/Users/Self/Documents/MyApp/MyApp.exe
    //let's backtrack to the /
    char* path = absolutepath+length;
    while(*path != '/'){
        *path = 0;
        --path;
    }



    //Now we are at C:/Users/Self/Documents/MyApp/
    //From here we can concat the Resources directory

    strcat(absolutepath,"Resources/somefile.txt");

    std::cout << absolutepath;
    //C:/Users/Self/Documents/MyApp/Resources/somefile.txt

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.