如何将.dll链接到MFC对话框应用程序中的主程序 [英] How to link the .dll to my main program in MFC dialogue base application

查看:93
本文介绍了如何将.dll链接到MFC对话框应用程序中的主程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MFC中创建了名为CalcDLL的dll。我已经为该dll项目添加了add类,因为我已经编写了数学运算的代码,如加,减,乘,除。

我用基于对话的MFC应用程序创建了新项目之后。在调用时我添加了添加代码。

用于在项目菜单中将CalcDLL.dll添加到该主项目 - > properties-> configuration properties-> linker-> input->其他依赖项我添加了dll路径。但它显示

错误1错误LNK1107:文件无效或损坏:无法读取0x328

如何解决此错误任何人都可以向我解释,我只需要使用与.dll没有添加.lib



我尝试过:



在主项目中:

void CCalciMainDlg :: OnBnClickedAdd()

{



CAdd a;

Num1 = GetDlgItemInt(IDC_EDIT_NUM1);

Num2 = GetDlgItemInt(IDC_EDIT_NUM2);

int opt = 1;

输出= a.operation(Num1,Num2,opt);



SetDlgItemInt(IDC_EDIT_OUTPUT,输出);

}



在Dll项目中:CalcDLL

ADD类:Add.cpp



int CAdd :: operation(int nNum1,int nNum2,int opt)

{

int result;

switch(opt)

{

案例1:

结果= nNum1 + nNum2;

休息;

案例2:

result = nNum1 - nNum2;

休息;

案例3:

result = nNum1 * nNum2;

break;

案例4:

结果= nNum1 / nNum2;

休息;

}

返回结果;

}

I have created the dll in MFC named as CalcDLL. I have added the add class to that dll project in that i have written the code for mathematical operations like add,subtract,multiplication,division.
After I have created the new project with dialogue based MFC application. In that for calling i have added the code for addition.
For adding CalcDLL.dll to that main project in project menu -> properties->configuration properties->linker->input->additional dependencies i have added the dll path. But it is showing
Error 1 error LNK1107: invalid or corrupt file: cannot read at 0x328
How to resolve this error can anyone explain to me and i need to use only with .dll not with adding .lib

What I have tried:

In main project:
void CCalciMainDlg::OnBnClickedAdd()
{

CAdd a;
Num1 = GetDlgItemInt(IDC_EDIT_NUM1);
Num2 = GetDlgItemInt(IDC_EDIT_NUM2);
int opt = 1;
Output = a.operation(Num1, Num2,opt);

SetDlgItemInt(IDC_EDIT_OUTPUT, Output);
}

In Dll project:CalcDLL
ADD Class:Add.cpp

int CAdd::operation(int nNum1, int nNum2,int opt)
{
int result;
switch (opt)
{
case 1:
result = nNum1 + nNum2;
break;
case 2:
result = nNum1 - nNum2;
break;
case 3:
result = nNum1 * nNum2;
break;
case 4:
result = nNum1 / nNum2;
break;
}
return result;
}

推荐答案

您需要将.lib文件添加到链接器选项,而不是dll。链接器使用.LIB文件来满足对DLL函数的引用。应用程序启动时,DLL本身将在运行时加载。
You need to add the .lib file to your linker options, not the dll. The .LIB file is used by the linker to satisfy the references to your DLL functions. The DLL itself will be loaded at run time when the application starts.


您必须将 lib 文件添加到项目设置或使用 pragma 指令在您的一个源文件中(评论(C-C ++) [ ^ ]):

You have to add the lib file to your projects settings or use a pragma directive in one of your source files (comment (C-C++)[^]):
#pragma comment(lib, "[path\]libname")

创建DLL项目时,会创建相应的 lib 文件,并将其与DLL一起放在项目构建输出目录中。



如果您不想使用 lib 文件,可以在运行时使用LoadLibrary功能(Windows) [ ^ ]。但是你必须为导出的函数声明原型并使用 GetProcAddress函数(Windows) [ ^ ]。

When creating a DLL project, the corresponding lib file is created and placed together with the DLL in the project build output directory.

If you don't want to use a lib file, you can load the DLL during runtime with the LoadLibrary function (Windows)[^]. But then you have to declare prototypes for the exported functions and get the function pointers with the GetProcAddress function (Windows)[^].


这篇关于如何将.dll链接到MFC对话框应用程序中的主程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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