PathRemoveFileSpecA使用的一个报错

背景:在使用PathRemoveFileSpecA没有报错提示,但是在编译的时候会报链接错误。

PathRemoveFileSpecA是一个windwosAPI函数,用于从完整路径最后移除文件名,仅保留路径部分。

moveFileSpec下还有几个类似的接口,应对不同的情况 

static std::string getModelBaseDir() {
	char dllPath[MAX_PATH] = { 0 };
	HMODULE hModule = nullptr;
	if (GetModuleHandleExA(
	    GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
	    reinterpret_cast<LPCSTR>(&getModelBaseDir),
    &hModule))
	{
	    if (GetModuleFileNameA(hModule, dllPath, MAX_PATH) > 0) {
	        // 去掉文件名部分,仅保留目录
	        if (PathRemoveFileSpecA(dllPath)) {
	            std::string basePath(dllPath);
	            basePath += "\\models\\wechat_qrcode\\";
	            return basePath;
	       }
	    }
	}

    return ".\\models\\wechat_qrcode\\";
}

  解决办法:除了include<Windows.h>和<Shlwapi.h>

  还需要把Shlwapi.lib手动加进去

  #pragma comment(lib, "Shlwapi.lib")

posted @ 2025-07-17 09:41  Wind_Swing_Dunn  阅读(19)  评论(0)    收藏  举报