延迟加载DLL [英] Delay load DLL

查看:137
本文介绍了延迟加载DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为简单起见,我将带有标头MathFuncsDll.h的DLL_TUTORIAL.dll都放在了根文件夹C:\中。

For simplicity, I've placed both DLL_TUTORIAL.dll with header MathFuncsDll.h in the root folder C:\ .

然后,创建了一个空项目,设置

Then, created the empty project, setting


配置属性->链接器->输入->延迟加载的Dll's

Configuration Properties->Linker->Input->Delay Loaded Dll's



C:\DLL_TUTORIAL.dll;%(DelayLoadDLLs )

C:\DLL_TUTORIAL.dll;%(DelayLoadDLLs)



配置属性-> VC ++目录->包括目录

Configuration Properties->VC++ Directories->Include Directories


C:\; $(IncludePath)

C:\;$(IncludePath)

编译器命令:


/ Zi / nologo / W3 / WX- / O2 / Oi / Oy- / GL / D _MBCS / Gm- / EHsc / MT / GS
/ Gy / fp:precise / Zc:wchar_t / Zc:forScope
/Fp\"Release\clean_rough_draft.pch / Fa Release\ / Fo Release\
/ Fd发布\vc100.pdb / Gd / analyze- / erro rReport:queue

/Zi /nologo /W3 /WX- /O2 /Oi /Oy- /GL /D "_MBCS" /Gm- /EHsc /MT /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Release\clean_rough_draft.pch" /Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /Gd /analyze- /errorReport:queue

该项目仅包含主文件。

main.cpp

#include <Windows.h>
#include <iostream>
#include "MathFuncsDll.h"

using namespace MathFuncs;
using namespace std;

int main()
{
    std::cout<< MyMathFuncs<int>::Add(5,10)<<endl;

    system("Pause");
    return 0;
}

Dll已在不同的解决方案中成功编译。

Dll has been compiled successfully in different solution.

MathFuncsDll.h

MathFuncsDll.h

namespace MathFuncs
{
    template <typename Type>  
    class MyMathFuncs   
    {
    public:
        static __declspec(dllexport) Type Add(Type a, Type b);

        static __declspec(dllexport) Type Subtract(Type a, Type b);

        static __declspec(dllexport) Type Multiply(Type a, Type b);

        static __declspec(dllexport) Type Divide(Type a, Type b);

    };


}

这些功能的定义:

#include "MathFuncsDll.h"

#include <stdexcept>

using namespace std;

namespace MathFuncs
{
    template <typename Type>
    Type MyMathFuncs<Type>::Add(Type a,Type b)
    { return a+b; }

    template <typename Type>
    Type MyMathFuncs<Type>::Subtract(Type a,Type b)
    { return a-b; }

    template <typename Type>
    Type MyMathFuncs<Type>::Multiply(Type a,Type b)
    { return a*b; }

    template <typename Type>
    Type MyMathFuncs<Type>::Divide(Type a,Type b)
    { 
        if(b == 0) throw new invalid_argument("Denominator cannot be zero!");
        return a/b; 
    }
}

运行该程序失败:


1> main.obj:错误LNK2001:未解析的外部符号 public:静态int __cdecl MathFuncs :: MyMathFuncs :: Add(int,int)(?Add @?$ MyMathFuncs @ H @ MathFuncs @@ SAHHH @ Z)
1> C:\Users\Tomek\Documents\Visual Studio 2010\Projects\clean_rough_draft\Release\clean_rough_draft.exe :致命错误LNK1120:1个未解决的外部变量

1>main.obj : error LNK2001: unresolved external symbol "public: static int __cdecl MathFuncs::MyMathFuncs::Add(int,int)" (?Add@?$MyMathFuncs@H@MathFuncs@@SAHHH@Z) 1>C:\Users\Tomek\Documents\Visual Studio 2010\Projects\clean_rough_draft\Release\clean_rough_draft.exe : fatal error LNK1120: 1 unresolved externals

您能指出我的错误吗?

推荐答案

该问题与延迟加载DLL无关。我在这里看到两个问题:

The problem has nothing to do with delayed loading of the DLL or not. I can see two problems here:


  1. 您正在导出模板函数。这种方法行不通,因为Visual C ++编译器不支持模板导出,但是已经从标准中删除了模板导出。为此,您有两种可能的解决方案:

  1. You are exporting templated functions. This wouldn't work that way because template exporting is not supported in Visual C++ compiler and however has already been dropped from the standard. For this to work you have two possible solutions:


  • 移动.h文件中方法的实现,因此不再需要DLL因为所有代码都在头文件中;

  • 使用将在客户端应用程序中使用的类型实例化模板。这是通过将具有确切类型的实例化代码放入cpp文件中,在标头中进行一些 extern模板声明等来完成的。您可以在Google中查询更多信息,只需搜索外部模板DLL或类似的文件即可。

  • Move the implementations of the methods in the .h file, thus no longer needing a DLL at all as all the code is in a header file;
  • Instantiate the templates with the types that you will use in your client application. This is done be putting instantiation code with exact types in your cpp file, doing some extern template declarations in the header, etc. You can look that up in Google for more information, just search for 'extern template DLL' or something similar.

您仅在创建DLL时导出方法,但永远不会导入它们(或者至少是我从代码中看到的)。您在每个方法前面都使用 __ declspec(dllexport),它告诉编译器将这些方法放入DLL。如果要从客户端应用程序使用这些方法,则必须从DLL导入它们。这是通过在每个方法的前面放置 __ declspec(dllimport)来完成的。由于不能将两个前缀都放在方法上,因此必须创建两个几乎相同的头文件,只是在该方法前缀上有所不同,或者基于这是DLL构建代码还是客户端应用程序使用一些宏替换。再次,您可以在Google中查找它的运行方式。

You only export the methods when creating the DLL, but never import them (or at least that's what I see from the code). You use __declspec(dllexport) in front of each method, which tells the compiler to put that methods in the DLL. When you want to use these methods from a client application, you have to import them from the DLL. This is done by placing __declspec(dllimport) in front of each method. Since you can not put both prefixes on the methods you either have to create two almost the same header files that just differ on that method prefix thing or use some macro substitution based on whether this is a DLL building code or a client application. Once again, you can look that up in Google to see how its done.

我希望这会有所帮助。

这篇关于延迟加载DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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