Windows下動態封裝加載DLL

DLL導出函數時,使用GetProcAddress獲取函數地址,傳參不需要帶入namespace:

    int HixxxInterface::Init(const std::string& cfgpath, const std::string& lang)
    {
        std::string configPath = cfgpath.empty() ? "../../webengine/bin" : cfgpath;
        if (NULL == init_func_)//動態加載數據!!!
        {
            HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
            CHAR exe_path[MAX_PATH], dll_path[MAX_PATH], drive[MAX_PATH], dir[MAX_PATH], filename[MAX_PATH], ext[MAX_PATH];
            GetModuleFileNameA(hInstance, exe_path, MAX_PATH);
            _splitpath_s(exe_path, drive, dir, filename, ext); 
            //
            //組裝動態庫絕對路徑!!!
            std::string absolute_path = dir + configPath;
            _makepath_s(dll_path, drive, absolute_path.c_str(), "xxxx.dll", NULL);
            //
            DWORD dwAttrib = GetFileAttributesA(dll_path);
            if (INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY))
            {
                //文件存在!!!
                dllhandle_ = LoadLibraryA(dll_path);
                if (NULL != dllhandle_)
                {
                    auto addressInit = (Hixxx_Init)GetProcAddress(dllhandle_, "Hixxx_Init");
                    if (NULL != addressInit)
                    {
                        init_func_ = addressInit;
                    }
                    auto addressSetCallback = (Hixxx_SetCallback)GetProcAddress(dllhandle_, "Hixxx_SetCallback");
                    if (NULL != addressSetCallback)
                    {
                        setcallback_func_ = addressSetCallback;
                    }
                    auto addressEmbeded = (Hixxx_Embeded)GetProcAddress(dllhandle_, "Hixxx_Embeded");
                    if (NULL != addressEmbeded)
                    {
                        embeded_func_ = addressEmbeded;
                    }
                    auto addressResize = (Hixxx_Resize)GetProcAddress(dllhandle_, "Hixxx_Resize");
                    if (NULL != addressResize)
                    {
                        resize_func_ = addressResize;
                    }
                    auto addressLoad = (Hixxx_LoadUrl)GetProcAddress(dllhandle_, "Hixxx_LoadUrl");
                    if (NULL != addressLoad)
                    {
                        loadurl_func_ = addressLoad;
                    }
                    auto addressReset = (Hixxx_resetContent)GetProcAddress(dllhandle_, "Hixxx_resetContent");
                    if (NULL != addressReset)
                    {
                        resetcontent_func_ = addressReset;
                    }
                    auto addressUnInit = (Hixxx_UnInit)GetProcAddress(dllhandle_, "Hixxx_UnInit");
                    if (NULL != addressUnInit)
                    {
                        uninit_func_ = addressUnInit;
                    }
                }
            }
        }
        //
        if (NULL != init_func_)
        {
            return init_func_(lang);
        }
        //
        return 1;
    }

wstring和string互轉:

std::wstring stringToWstring(const std::string& str)
{
    LPCSTR pszSrc = str.c_str();
    int nLen = MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, NULL, 0);
    if (0 == nLen) {
        return std::wstring(L"");
    }

    wchar_t* pwszDst = new wchar_t[nLen];
    if (NULL == pwszDst) {
        return std::wstring(L"");
    }

    MultiByteToWideChar(CP_ACP, 0, pszSrc, -1, pwszDst, nLen);
    std::wstring wstr(pwszDst);
    delete[] pwszDst;
    pwszDst = NULL;

    return wstr;
}

std::string wstringToString(const std::wstring& wstr)
{
    LPCWSTR pwszSrc = wstr.c_str();
    int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
    if (nLen == 0) {
        return std::string("");
    }

    char* pszDst = new (std::nothrow) char[nLen];
    if (!pszDst) {
        return std::string("");
    }

    WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
    std::string str(pszDst);
    delete[] pszDst;
    pszDst = NULL;

    return str;
}

 

發佈了59 篇原創文章 · 獲贊 15 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章