自己做了个DLL,另外建了个应用程序调用这个DLL,执行后能加载DLL,但不能调用函数,生成DLL的那个程序部分如下:
// MyDLL.cpp : Defines the entry point for the DLL application.
//#include "stdafx.h"
//#include "MyDLL.def"
int _declspec(dllexport) Max(int a,int b)
{
if(a>=b)return a;
else
return b;
}
int _declspec(dllexport) Min(int a,int b)
{
if(a>=b)return b;
else
return a;
}BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}调用DLL的程序部分如下:
void CTestDlg::OnButton1()
{
int max_value=19;
int min_value=18;
int result;
HINSTANCE hLibrary;
typedef int (*_Max)(int a,int b);
typedef int (*_Min)(int a,int b);
_Max max;
_Min min;
hLibrary=LoadLibrary("MyDLL.dll");
if(hLibrary!=NULL)
MessageBox("The MyDLL.DLL has already been load.");max=(_Max)GetProcAddress(hLibrary,"Max");
//if (max!=(_Max)NULL)
//{
result=(*max)(max_value,min_value);
cout<<result<<endl;
//}
//else
MessageBox("the call of function failure!!!");
//int max_value= (*max)(15,20);
//int min_value= (* min)(15,20);
//MessageBox(""+max_value);
FreeLibrary(hLibrary);
}MyDLL.dll还有MyDLL.lib已经copy到该工程debug目录下
// MyDLL.cpp : Defines the entry point for the DLL application.
//#include "stdafx.h"
//#include "MyDLL.def"
int _declspec(dllexport) Max(int a,int b)
{
if(a>=b)return a;
else
return b;
}
int _declspec(dllexport) Min(int a,int b)
{
if(a>=b)return b;
else
return a;
}BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}调用DLL的程序部分如下:
void CTestDlg::OnButton1()
{
int max_value=19;
int min_value=18;
int result;
HINSTANCE hLibrary;
typedef int (*_Max)(int a,int b);
typedef int (*_Min)(int a,int b);
_Max max;
_Min min;
hLibrary=LoadLibrary("MyDLL.dll");
if(hLibrary!=NULL)
MessageBox("The MyDLL.DLL has already been load.");max=(_Max)GetProcAddress(hLibrary,"Max");
//if (max!=(_Max)NULL)
//{
result=(*max)(max_value,min_value);
cout<<result<<endl;
//}
//else
MessageBox("the call of function failure!!!");
//int max_value= (*max)(15,20);
//int min_value= (* min)(15,20);
//MessageBox(""+max_value);
FreeLibrary(hLibrary);
}MyDLL.dll还有MyDLL.lib已经copy到该工程debug目录下
解决方案 »
- 一个类对象用new 产生, 如何去释放呢?
- 求助:急:如何从串口中读出接收到的信号并判断该信号是否符合一定的要求,如:接收到的是字母等等
- SOCKET错误
- 请教:Unicode下combo box乱码
- 如何在C++里,把整形数强制转换为字符串型的
- 请问如何让子窗口在父窗口之外显示?
- 98用DX编制DVD播放程序,遇到用GetDvdInterface取得IID_IVideoWindow接口错误,应该如何解决?(DX9 已经成功安装了)
- 在VC中如何向文件读写float型数据
- 怎样在CEditView写的文本编辑器的当前编辑位置插如系统时间?
- 如何在 ActiveX中取得IE的句柄
- 如何获取当前窗口的句柄?
- VC6.0下使用GDI+碰到的问题
extern "C" DllExport int _stdcall Min(int a,int b)
试试看吧
“执行后能加载DLL”,不是加载DLL成功了么,真的吗?
extern "C" __declspec(dllexport) int _stdcall Min(int a,int b)
但要加入 MyDLL.h #include " MyDLL.h"
修改引入的MyDLL.h中
extern "C" __declspec(dllexport) int Max(int a,int b);
为
extern "C" __declspec(dllimport) int Max(int a,int b);
你的错误应该不是DLL的问题,可能是初始化了一个空指针,未赋值便读取,找一下NULL关键字试试