Roman Roth
Roman Roth

Reputation: 31

C++ GetModuleFileName using Boost

I have the following code that needs to be ported from windows to boost:

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

How would I go about implementing the GetModuleFileName function using Boost?

Any help appreciated!

Upvotes: 1

Views: 648

Answers (2)

n. m. could be an AI
n. m. could be an AI

Reputation: 119877

boost::dll::shared_library class has a method location which returns the full path to the library.

For the whole program, there is boost::dll::program_location global function.

In addition, it is possible to find the executable or library location by symbol address and by source location:

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

The latter can only be used by a module to find its own location.

Upvotes: 1

Botje
Botje

Reputation: 30830

You can use Boost.Dll like so:

shared_library lib(DC_DLL_FILENAME);
filesystem::path full_path = lib.location();

If you're trying to get the path to the currently running code, that is boost::dll::this_line_location().

Upvotes: 0

Related Questions