关于C++里编写与调用DLL文件的问题

来源:百度知道 编辑:UC知道 时间:2024/06/06 17:24:35
最近我在学习用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;<

使用GetLastError()查看错误代码.
或者将:extern "C" void _declspec(dllexport) Fun();
改为extern "C" _declspec(dllexport) void Fun();

hDll=LoadLibrary(TEXT("DllTest.dll"));
在这句后面加一个assert(hdll)看看链接库是否加载进来了.