首页 > 解决方案 > C++ GetModuleFileName 使用 Boost

问题描述

我有以下代码需要从 Windows 移植来提升:

BOOL Class::fn_GetModulePath(WCHAR szPath[MAX_PATH])
{
    BOOL bReturn = FALSE;

    dll::library_handle hDll = dll::load_shared_library((const char*)DC_DLL_FILENAME);
    //HMODULE hDll = LoadLibrary(DC_DLL_FILENAME);
    
    if (hDll)
    {
        // This function needs replacing
        DWORD dwResult = GetModuleFileName(hDll,szPath,MAX_PATH);

        dll::close_shared_library(hDll);
        //FreeLibrary(hDll);

        if (dwResult)
        {
            int iLen = (int) wcslen(szPath);

            if (iLen)
            {
                for (int i = iLen; i >= 0; i--)
                {
                    if(szPath[i] == '\\')
                    {
                        szPath[i+1] = 0;
                        break;
                    }
                }
            }

            bReturn = TRUE;
        }
    }

    return bReturn;
}

我将如何GetModuleFileName使用 Boost 来实现该功能?

任何帮助表示赞赏!

标签: c++boost

解决方案


boost::dll::shared_library类有一个location返回库的完整路径的方法。

对于整个程序,有boost::dll::program_location全局功能。

此外,可以通过符号地址和源位置找到可执行文件或库位置:

boost::dll::symbol_location
boost::dll::this_line_location

后者只能被模块用来查找自己的位置。


推荐阅读