目录
Step#1 创建MFC应用程序,名称TestDll,基于对话框的,最后点击Finish
第一个错误 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#4添加代码之第二种方法【推荐使用此种,实际工程中应用的比较多】
错误及解决办法error LNK2019: unresolved external symbol _add referenced in function
Step#1. 新建C#项目,Windows Forms Application,取名CSharpTestDll
Step#3双击Add按钮添加事件响应函数,Subtract按钮也要双击
Step#4. 将被使用到的MyDll0.dll放置在"项目的Output Path"目录下。
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