如何重命名DLL但仍允许EXE找到它? [英] How do I rename a DLL but still allow the EXE to find it?

查看:349
本文介绍了如何重命名DLL但仍允许EXE找到它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个内部生成的DLL,我们拥有相关的stub的静态LIB。



我们还有一个使用此DLL的EXE静态链接到DLL的LIB文件(即不是手动使用LoadLibrary)的简单方法。



当我们部署EXE时,我们希望将DLL文件名称



我们如何做到这一点,以便我们的EXE仍然自动发现DLL?



我已经尝试重命名DLL和LIB文件(在它们被构建为正常名称之后),然后将EXE项目设置更改为与重命名的LIB链接。这在运行时失败,因为我猜这个DLL的名字被烧录到LIB文件中,而不是简单地通过连接器将.lib替换为.dll来猜测。


$ b $一般来说,我们不想将此混淆应用于DLL的所有用途,因此我们希望保留当前的DLL项目输出文件。


$ b $我希望有一种方法可以编辑DLL的LIB文件,并用其他东西替换DLL文件的硬编码名称。在这种情况下,这可以完全在EXE项目中完成(也许作为预构建步骤)。






更新:我发现延迟加载不起作用,因为我的DLL包含导出的C ++类。
请参阅本文



有没有其他选择?

解决方案

这是一个很好的替代方法:延迟加载



构建应用程序时,将链接到原始DLL名称将原始dll设置为延迟加载)



然后根据客户的要求部署重命名的DLL。



您的EXE将尝试使用原始名称找到DLL,而不是重命名的版本,因此将失败,但是延迟加载您可以拦截此失败,并自己加载重命名的版本,然后让本机Windows加载器解决所有事情,就好像没有更改。



阅读文章链接器支持延迟加载的DLL 并参阅延迟钩子示例



您的延迟挂钩可能如下所示:

  FARPROC WINAPI delayHook(unsigned dliNotify,PDelayLoadInfo pdli)
{
switch(dliNotify)
{
case dliNotePreLoadLibrary:
if(strcmp(pdli-> szDll,origional.dll) == 0)
return(FARPROC)LoadLibrary(renamed.dll);
break;
default:
return NULL;
}

返回NULL;
}


We have a DLL which is produced in house, and for which we have the associated static LIB of stubs.

We also have an EXE which uses this DLL using the simple method of statically linking to the DLL's LIB file (ie, not manually using LoadLibrary).

When we deploy the EXE we'd like the DLL file name to be changed for obfuscation reasons (at customer's request).

How can we do this so that our EXE still finds the DLL automagically?

I have tried renaming the DLL and LIB files (after they were built to their normal name), then changing the EXE project settings to link with the renamed LIB. This fails at runtime, as I guess the name of the DLL is baked into the LIB file, and not simply guessed at by the linker replacing ".lib" with ".dll".

In general, we do not want to apply this obfuscation to all uses of the DLL, so we'd like to keep the current DLL project output files are they are.

I'm hoping that there will be a method whereby we can edit the DLL's LIB file, and replace the hardcoded name of the DLL file with something else. In which case this could be done entirely within the EXE project (perhaps as a pre-build step).


Update: I find that Delay Loading does not work, as my DLL contains exported C++ classes. See this article.

Are there any alternatives?

解决方案

Here is a nice alternative approach: delay loading.

When building your application, link it all as you would with the original DLL name (but set the origional dll to be delay loaded).

You then deploy the DLL renamed as per your customers request.

Your EXE will attempt to locate the DLL using the original name and not the renamed version so will fail, however with delay loading you can intercept this fail and load the renamed version yourself and then have the native windows loader resolve everything as if nothing changed.

Read the article Linker Support for Delay-Loaded DLLs and see the Delay Hook example.

Your delay hook might be something like below:

FARPROC WINAPI delayHook( unsigned dliNotify, PDelayLoadInfo pdli )
{
    switch( dliNotify )
    {
        case dliNotePreLoadLibrary:
            if( strcmp( pdli->szDll, "origional.dll" ) == 0 )
                return (FARPROC)LoadLibrary( "renamed.dll" );
            break;
        default:
            return NULL;
    }

    return NULL;
}

这篇关于如何重命名DLL但仍允许EXE找到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆