我刚学dll文件的使用,自己写了个程序,dll文件的定义如下:
//dll.h
#ifndef _DLL_H_
#define _DLL_H_
#include <windows.h>
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
DLLIMPORT void CALLBACK DllFoo(void);
#endif /* _DLL_H_ */
//dllmain.cpp
/* Replace "dll.h " with the name of your header */
#include "dll.h "
#include <windows.h>
#include <iostream>
int WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, PVOID pvReserved)
{
return TRUE;
}
DLLIMPORT void CALLBACK DllFoo(void)
{
std::cout < < "这是dll里的函数 ";
}
编译后得到mydll.dll 和 libmydll.a(我用的编译器是devc++,这里的*.a相当于*.lib).
在google里找了以后我用#include "dll.h "并在编译器工程设置里加入链接libmydll.a后使用DllFoo成功,但是用另一种方法,也就是不#include "dll.h ",并且不用链接文件,在程序中使用LoadLibrary()却出问题,程序如下:
//main.cpp
#include <windows.h>
#include <iostream>
using namespace std;
typedef void (*DllFoo)(void);
int main(int argc, char *argv[])
{
DllFoo _DllFoo;
HINSTANCE hInstLibrary = LoadLibrary( "mydll.dll ");
if (hInstLibrary == NULL)
{
cout < < "载入dll失败 " < <endl;
FreeLibrary(hInstLibrary);
system( "pause ");
return 1;
}
_DllFoo = (DllFoo)GetProcAddress(hInstLibrary, "DllFoo ");
if(_DllFoo == NULL)//运行后程序进入这里,显示“转换失败”后结束
{
cout < < "转换失败 " < <endl;
FreeLibrary(hInstLibrary);
system( "pause ");
return 1;
}
//以下程序没有运行
DllFoo();
cout < <endl;
FreeLibrary(hInstLibrary);
system( "PAUSE ");
return EXIT_SUCCESS;
}
dll问题
最新推荐文章于 2024-04-25 18:23:49 发布