关于C++里编写与调用DLL文件的问题 10
最近我在学习用C++语言来编写DLL文件,并尝试在C++程序里调用DLL文件里的东西.我用的编译器是VS2008.首先,我的DLL文件里,主要有以下内容://DllTes...
最近我在学习用C++语言来编写DLL文件,并尝试在C++程序里调用DLL文件里的东西.我用的编译器是VS2008.首先,我的DLL文件里,主要有以下内容:
//DllTest.h
extern "C" void _declspec(dllexport) Fun();
//DllTest.cpp
#include "stdafx.h"
#include"stdio.h"
#include"DllTest.h"
void Fun()
{
printf("Fun()\n");
}
//dllmain.cpp
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
接着是我的EXE工程:
//DllUseTest.cpp
#include "stdafx.h"
#include"stdio.h"
#include"stdlib.h"
#include"windows.h"
using namespace System;
int main(array<System::String ^> ^args)
{
HINSTANCE hDll;
hDll=LoadLibrary(TEXT("DllTest.dll"));
typedef void(*lpFun)();
lpFun Fun;
if(hDll!=NULL)
{
Fun=(lpFun)GetProcAddress(hDll,"Fun");//经测试,Fun最后为NULL!
if(Fun!=NULL)
{
Fun();
}
else
{
printf("Fun is null!\n");
}
}
else
{
printf("hDll is null!\n");
}
System::Console::ReadKey ();
return 0;
}
最后输出为Fun is null.为什么会这样呢?
hDll不为空,这测试过了.而且如果hDll为空,则最后输出结果应该是"hDll is null!" 展开
//DllTest.h
extern "C" void _declspec(dllexport) Fun();
//DllTest.cpp
#include "stdafx.h"
#include"stdio.h"
#include"DllTest.h"
void Fun()
{
printf("Fun()\n");
}
//dllmain.cpp
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
接着是我的EXE工程:
//DllUseTest.cpp
#include "stdafx.h"
#include"stdio.h"
#include"stdlib.h"
#include"windows.h"
using namespace System;
int main(array<System::String ^> ^args)
{
HINSTANCE hDll;
hDll=LoadLibrary(TEXT("DllTest.dll"));
typedef void(*lpFun)();
lpFun Fun;
if(hDll!=NULL)
{
Fun=(lpFun)GetProcAddress(hDll,"Fun");//经测试,Fun最后为NULL!
if(Fun!=NULL)
{
Fun();
}
else
{
printf("Fun is null!\n");
}
}
else
{
printf("hDll is null!\n");
}
System::Console::ReadKey ();
return 0;
}
最后输出为Fun is null.为什么会这样呢?
hDll不为空,这测试过了.而且如果hDll为空,则最后输出结果应该是"hDll is null!" 展开
2个回答
展开全部
使用GetLastError()查看错误代码.
或者将:extern "C" void _declspec(dllexport) Fun();
改为extern "C" _declspec(dllexport) void Fun();
或者将:extern "C" void _declspec(dllexport) Fun();
改为extern "C" _declspec(dllexport) void Fun();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询