1

我正在使用 Leadtools 17.5。如果我将 Leadtools Dlls 静态链接到我的 64 位 C++ 应用程序,然后调用L_SetLicenseBuffer一切正常,返回值为零。但出于安全原因,最终产品不允许将这些 DLL 添加到System32文件夹中,也不允许更改系统路径,并且由于多个应用程序正在使用这些工具,我想将它们安装在一个公共文件夹中(C:\例如Program Files\Common Files\LeadTools\17.5)并使用AddDllDirectory将路径添加到 DLL 搜索路径。所以我决定在运行时动态加载 DLL。所以我为这样的函数创建了一个定义:

typedef L_INT (EXT_FUNCTION* TL_SetLicenseBuffer)(L_UCHAR* pLicenseBuffer, L_SSIZE_T nSize, L_TCHAR* pszDeveloperKey);
typedef L_BOOL (EXT_FUNCTION* TL_IsSupportLocked)(L_UINT uType);

然后创建了一个这样的函数指针:

TL_SetLicenseBuffer pfSetLicenseBuffer = NULL;
TL_IsSupportLocked pfIsSupportLocked = NULL;

然后将 DLL 所在的路径添加到 DLL 搜索路径:

AddDllDirectory(LEAD_DLL_PATH);
AddDllDirectory(LEAD_FILTER_PATH);

并将 DLL 的默认目录搜索路径设置为用户定义:

SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_USER_DIRS);

然后加载 DLL 并获取我需要的函数的地址:

HINSTANCE hKrn = LoadLibrary(L"ltkrnx.dll");
pfSetLicenseBuffer = (TL_SetLicenseBuffer)GetProcAddress(hKrn, "L_SetLicenseBuffer");
pfIsSupportLocked  = (TL_IsSupportLocked)GetProcAddress(hKrn, "L_IsSupportLocked");

现在,如果我使用与以前相同的参数的函数指针,函数将失败并返回 -13 以及任何后续调用,例如pfIsSupportLocked显示 nag 对话框:

retCode = pfSetLicenseBuffer(pLicenseData, LicSize, pKeyStr); // retCode is -13
pfIsSupportLocked(L_SUPPORT_DOCUMENT); // Shows nag dialog

有谁知道我该如何解决这个问题?

谢谢
山姆

4

2 回答 2

0

我根本无法使动态加载工作,但我能够使用延迟加载工作。
我必须做的是返回将提取的 .Lib 文件链接到我的应用程序,然后告诉编译器延迟加载关联的 DLL,这让我有机会为 __pfnDliNotifyHook2 和 __pfnDliFailureHook2 创建通知挂钩这样就可以使用LoadLibrary从正确的位置加载延迟加载的 Dll。
但这仅解决了一半的问题,因为其中一些 Dll 依赖于其他 DLL,并且当我使用完整路径加载所需的 DLL 时,它找不到辅助 DLL(与第一个 DLL 位于同一目录中)我正在加载),这将导致 LoadLibrary 失败。解决方案是跟踪这些依赖项并预加载它们。我包含了一些代码来解决以后可能遇到类似情况的任何人的问题。
PS我正在使用Embarcadero 的 C++ Builder,所以像Strings,TStringListException可能不是每个人都熟悉的一些对象,但这个概念也应该在 VC++ 中工作。

#include <map>

struct TDllDependency
{
    TStringList* Dependency;
    HMODULE hDll;

    __fastcall TDllDependency(void)
    {
        hDll = NULL;
        Dependency = new TStringList();
    }
    virtual __fastcall ~TDllDependency(void)
    {
        delete Dependency;
    }
};

class TDllModList : public std::map<System::String, TDllDependency>
{
public:
    void __fastcall CheckDependency(const System::String& aName);
};
//---------------------------------------------------------------------------
System::String __fastcall GetLtDllPath(void)
{
    wchar_t* pfPath = NULL;
    System::String dllPath;

    SHGetKnownFolderPath(FOLDERID_ProgramFilesCommon, KF_FLAG_DEFAULT, NULL, &pfPath);
    if (NULL != pfPath)
    {
        dllPath = IncludeTrailingBackslash(pfPath) + L"LeadTools\\17.5\\";
        ::CoTaskMemFree(pfPath);
    }
    return dllPath;
}
System::String mDllPath(GetLtDllPath());
TDllModList DllModList;
void __fastcall InitDllDepends()
{
    DllModList.clear();
#if defined(_WIN64)
    DllModList[L"ltimgefxx.dll"].Dependency->CommaText = L"ltdisx.dll,ltimgutlx.dll";
    DllModList[L"ltefxx.dll"].Dependency->CommaText = L"ltdisx.dll,ltimgutlx.dll";
    DllModList[L"ltimgcorx.dll"].Dependency->CommaText = L"ltdisx.dll,ltimgutlx.dll";
    DllModList[L"ltdlgimgefxx.dll"].Dependency->CommaText = L"ltdisx.dll,ltdlgkrnx.dll,ltdlgcomx.dll,ltdlgctrlx.dll,ltdlgutlx.dll,ltimgefxx.dll,ltimgsfxx.dll,ltimgcorx.dll,ltimgclrx.dll";
    DllModList[L"ltdlgutlx.dll"].Dependency->CommaText = L"ltdisx.dll,ltfilx.dll,ltdlgkrnx.dll,ltimgclrx.dll,ltimgcorx.dll,ltimgefxx.dll,ltimgsfxx.dll";
    DllModList[L"ltdlgctrlx.dll"].Dependency->CommaText = L"ltdlgutlx.dll,ltdlgkrnx.dll,ltdisx.dll,ltfilx.dll,ltimgefxx.dll";
    DllModList[L"ltdlgcomx.dll"].Dependency->CommaText = L"ltdlgkrnx.dll,ltdlgctrlx.dll,ltdlgutlx.dll";
#elif defined(__WIN32__)
    DllModList[L"ltimgefxu.dll"].Dependency->CommaText = L"ltdisu.dll,ltimgutlu.dll";
    DllModList[L"ltefxu.dll"].Dependency->CommaText = L"ltdisu.dll,ltimgutlu.dll";
    DllModList[L"ltimgcoru.dll"].Dependency->CommaText = L"ltdisu.dll,ltimgutlu.dll";
    DllModList[L"ltdlgimgefxu.dll"].Dependency->CommaText = L"ltdisu.dll,ltdlgkrnu.dll,ltdlgcomu.dll,ltdlgctrlu.dll,ltdlgutlu.dll,ltimgefxu.dll,ltimgsfxu.dll,ltimgcoru.dll,ltimgclru.dll";
    DllModList[L"ltdlgutlu.dll"].Dependency->CommaText = L"ltdisu.dll,ltfilu.dll,ltdlgkrnu.dll,ltimgclru.dll,ltimgcoru.dll,ltimgefxu.dll,ltimgsfxu.dll";
    DllModList[L"ltdlgctrlu.dll"].Dependency->CommaText = L"ltdlgutlu.dll,ltdlgkrnu.dll,ltdisu.dll,ltfilu.dll,ltimgefxu.dll";
    DllModList[L"ltdlgcomu.dll"].Dependency->CommaText = L"ltdlgkrnu.dll,ltdlgctrlu.dll,ltdlgutlu.dll";
#endif
};
HMODULE SafeLoadLeadDll(const System::String tName)
{
    System::String tPath;
    HMODULE retVal = NULL;

    DllModList.CheckDependency(tName);
    tPath = mDllPath + tName;
    if(FileExists(tPath))
        retVal = ::LoadLibrary(tPath.c_str());
    return retVal;
}
FARPROC WINAPI MyDliNotifyHook(unsigned dliNotify, PDelayLoadInfo pdli)
{
    FARPROC retVal = NULL;
    System::String tStr(pdli->szDll);

    tStr = tStr.LowerCase();
    if(dliNotePreLoadLibrary == dliNotify)
    {
        TDllModList::iterator i = DllModList.find(tStr);

        if(DllModList.end() == i)
        {
            retVal = (FARPROC)SafeLoadLeadDll(tStr);
            DllModList[tStr].hDll = (HMODULE)retVal;
        }
        else if(NULL == i->second.hDll)
        {
            i->second.hDll = SafeLoadLeadDll(tStr);
            retVal = (FARPROC)i->second.hDll;
        }
        else
            retVal = (FARPROC)i->second.hDll;
    }
    else if(dliFailLoadLib == dliNotify)
    {
        tStr = L"Compleatly falied to load " + tStr;
        ::OutputDebugString(tStr.c_str());
    }
    return retVal;
}

FARPROC WINAPI MyDliFailureHook(unsigned dliNotify, PDelayLoadInfo pdli)
{
    FARPROC retVal = NULL;
    if(dliNotePreLoadLibrary == dliNotify)
    {
        System::String tMsg = pdli->szDll;

        tMsg = L"Failed to load \"" + tMsg + L"\".\n" + SysErrorMessage(::GetLastError());
        throw Exception(tMsg);
    }
    return retVal;
}

extern "C" PfnDliHook __pfnDliNotifyHook2 = MyDliNotifyHook;
extern "C" PfnDliHook __pfnDliFailureHook2 = MyDliFailureHook;
void __fastcall TDllModList::CheckDependency(const System::String& aName)
{
    TDllModList::iterator i = find(aName);

    if(end() != i)
    {
        int len = i->second.Dependency->Count;
        int j;
        System::String tPath;

        for(j = 0; j < len; j++)
        {
            if(end() == find(i->second.Dependency->Strings[j]))
            {
                CheckDependency(i->second.Dependency->Strings[j]);
                tPath = mDllPath + i->second.Dependency->Strings[j];
                (*this)[i->second.Dependency->Strings[j]].hDll = ::LoadLibrary(tPath.c_str());
            }
        }
    }
}
//---------------------------------------------------------------------------

当然InitDllDepends();应该在开始时调用以WinMain正确设置。

于 2016-11-08T17:52:47.583 回答
0

您需要做的第一件事是检查调试器输出并确保您期望加载的 DLL 是通过验证路径加载的那个。您的搜索路径中可能有多个版本的 LTKRNX.DLL。我在这里测试了你的代码,它返回了成功:

typedef L_INT (EXT_FUNCTION* TL_SetLicenseBuffer)(L_UCHAR* pLicenseBuffer, L_SSIZE_T nSize, L_TCHAR* pszDeveloperKey);
typedef L_BOOL (EXT_FUNCTION* TL_IsSupportLocked)(L_UINT uType);

HINSTANCE hKrn = LoadLibrary(L"ltkrnx.dll");
TL_SetLicenseBuffer pfSetLicenseBuffer = NULL;
TL_IsSupportLocked pfIsSupportLocked = NULL;

pfSetLicenseBuffer = (TL_SetLicenseBuffer)GetProcAddress(hKrn, "L_SetLicenseBuffer");
pfIsSupportLocked  = (TL_IsSupportLocked)GetProcAddress(hKrn, "L_IsSupportLocked");

L_INT retCode = pfSetLicenseBuffer(szLICAnsi, _countof(szLICAnsi), pKeyStr);
if(retCode == SUCCESS)
  bRet = pfIsSupportLocked(L_SUPPORT_DOCUMENT);
else
  printf("Problem!");

PaulMcKenzie 还建议另一种方法来验证您对 LoadLibrary 的调用是否正常工作。如果您仍然无法弄清楚,您可以通过 support@leadtools.com 联系我们的技术支持以帮助您解决此问题

于 2016-10-31T13:56:06.200 回答