C/C++/C#使用C/C++动态链接库DLL 错误现象及解决办法 图文详解

本文详细介绍了在C/C++和C#中使用动态链接库(DLL)的方法,包括创建、导入函数声明、配置包含目录和库文件,以及解决常见错误。通过实例演示了如何在MFC和C#应用程序中调用DLL中的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

MFC应用程序使用DLL

Step#1 创建MFC应用程序,名称TestDll,基于对话框的,最后点击Finish

Step#2 添加两个按钮Add和Subtract

Step#3双击Add按钮添加命令响应函数

Step#4添加代码

Step#5错误及解决办法

第一个错误 error C2664 cannot convert argument 1 from 'const char [7]' to 'const wchar_t *'

第二个错误 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.

第三个错误 error LNK2019: unresolved external symbol

第四个错误error LNK1104: cannot open file 'MyDll0.lib' 

第五个错误 error LNK2019: unresolved external symbol __imp__add referenced in function 

第六个错误 找不到MyDll0.dll,无法继续执行代码。重新安装程序可能会解决此问题

Step#6程序运行结果

Step#4添加代码之第二种方法【推荐使用此种,实际工程中应用的比较多】

TestDllDlg.cpp代码

错误及解决办法error LNK2019: unresolved external symbol _add referenced in function

汇总C/C++使用 DLL步骤

C#应用程序使用DLL

Step#1. 新建C#项目,Windows Forms Application,取名CSharpTestDll

Step#2 添加两个按钮Add和Subtract

​Step#3双击Add按钮添加事件响应函数,Subtract按钮也要双击

Step#4. 将被使用到的MyDll0.dll放置在"项目的Output Path"目录下。

Step#5添加代码

Form1.cs完整版代码如下

Step#6错误及解决办法

Step#7 Build成功&运行

辅助工具

Dumpbin

Dependency Walker


MFC应用程序使用DLL

Step#1 创建MFC应用程序,名称TestDll,基于对话框的,最后点击Finish

 

Step#2 添加两个按钮Add和Subtract

这里使用的dll为《创建动态链接库(dll)》中创建的MyDll0.dll

ID Caption 命令响应函数 函数功能
IDC_BTN_ADD Add OnBnClickedBtnAdd 调用MyDll0.dll的add函数
IDC_BTN_SUBTRACT Subtract OnBnClickedBtnSubtract 调用MyDll0.dll的subtract函数

Step#3双击Add按钮添加命令响应函数

Step#4添加代码

extern "C" _declspec(dllimport) int   _stdcall  add(int a, int b);
extern "C" _declspec(dllimport) int   _stdcall  subtract(int a, int b);

void CTestDllDlg::OnBnClickedBtnAdd()
{
    // TODO: Add your control notification handler code here
    CString str;
    str.Format("5+3=%d", add(5, 3));
    MessageBox(str);
}


void CTestDllDlg::OnBnClickedBtnSubtract()
{
    // TODO: Add your control notification handler code here
    CString str;
    str.Format("5-3=%d", subtract(5, 3));
    MessageBox(str);
}

Step#5错误及解决办法

第一个错误 error C2664 cannot convert argument 1 from 'const char [7]' to 'const wchar_t *'

错误说明:error C2664: 'void ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>::Format(UINT,...)' : cannot convert argument 1 from 'const char [7]' to 'const wchar_t *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

解决办法:VS2013默认使用UNICODE编码,将其修改为Not Set

Project -> Properties -> Configuration Properties -> General -> Character Set: Not Set

第二个错误 error MSB8031: Building an MFC project for a non-Unicode character set is deprecated.

错误说明:error MSB8031: Building an MFC project for a non-Unicode character set is deprecated. You must change the project property to Unicode or download an additional library. See http://go.microsoft.com/fwlink/p/?LinkId=286820 for more information.

解决办法:
VS2013把 multi-byte character set 支持移除了

去微软网站下载这个组件就行了,网址http://go.microsoft.com/fwlink/p/?LinkId=286820

Multibyte MFC Library for Visual Studio 2013

【注意】根据自己的VS版本来选择组件,笔者的是VS2013

参考文章https://blog.csdn.net/tahelin/article/details/30312881

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值