Dll创建及调用
来源:互联网 发布:以下是淘宝网禁售商品 编辑:程序博客网 时间:2024/05/17 22:02
隐式链接
I:DLL创建
a:创建一个Smart Device的Win32项目
b:选择WM6的SDK
c:选择Dll并导出
d:visual studio 自动生成的代码,这里加上了HelloWorld函数。
MyDll.cpp:
#include "stdafx.h"
#include "MyDll.h"
#include <windows.h>
#include <commctrl.h>
BOOL APIENTRY DllMain( HANDLE 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;
}
// This is an example of an exported variable
MYDLL_API int nMyDll=0;
// This is an example of an exported function.
MYDLL_API int fnMyDll(void)
{
return 42;
}
// This is the constructor of a class that has been exported.
// see MyDll.h for the class definition
CMyDll::CMyDll()
{
return;
}
MYDLL_API void HelloWorld(void)
{
MessageBox (NULL, TEXT ("Hello World"), TEXT ("HelloWorld"), MB_OK);
}
MyDll.h:
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the MYDLL_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// MYDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif
// This class is exported from the MyDll.dll
class MYDLL_API CMyDll {
public:
CMyDll(void);
// TODO: add your methods here.
};
extern MYDLL_API int nMyDll;
MYDLL_API int fnMyDll(void);
MYDLL_API void HelloWorld(void);
头文件 MyDll.h还将用于调用MyDll.dll的文件,MYDLL_API __declspec(dllexport)用于MyDll.cpp,其中MYDLL_EXPORTS 定义在:
这样当调用该Dll的文件没有定义MYDLL_EXPORTS,则使用#define MYDLL_API __declspec(dllimport).
e:编译后将生成MyDll.dll和MyDll.lib,其中MyDll.lib用于调用该Dll的文件编译时使用,可以用dumpbin -exports查看该Dll导出的函数:
II:DLL调用
a:创建一个控制台程序DllTest,并将上面生成的Mydll.lib和MyDll.h拷贝到该项目的目录下
b:在link的input中加入MyDll.lib:
c:将MyDll.dll拷贝包SDk模拟器的Program Files/DllTest目录下,否则运行时找不到MyDll.dll会报错无法运行
d:运行Debug,就可以看到效果了:
动态加载
1:新建一个Smart Device的Win32项目
2:创建一个Dll,不要导出符号
3:MyDll_Def.cpp:
// MyDll_Def.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
void HelloWorld(void)
{
MessageBox (NULL, TEXT ("Hello World"), TEXT ("Hello World"), MB_OK);
}
4:新建一个def文件,用于导出HelloWorld
LIBRARY "MyDll_Def"
EXPORTS
HelloWorld
5:创建一个DllTest_Def测试程序:
// DllTest_Def.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hInst;
hInst=LoadLibrary(TEXT("MyDll_Def.dll"));
if(NULL == hInst) {
MessageBox (NULL, TEXT ("Hello World"), TEXT ("Load MyDll_Def.dll error"), MB_OK);
return -1;
}
typedef void (*HELLOWORLDPROC)();
HELLOWORLDPROC HelloWorld=(HELLOWORLDPROC)GetProcAddress(hInst,TEXT("HelloWorld"));
if(NULL == HelloWorld) {
MessageBox (NULL, TEXT ("Hello World"), TEXT ("GetProcAddress HelloWorld error"), MB_OK);
return -1;
}
HelloWorld();
FreeLibrary(hInst);
return 0;
}
LoadLibrary用于动态加载Dll,GetProcAddress用于获取函数指针.
6:将MyDll.dll拷贝包SDk模拟器的Program Files/DllTest目录下,否则运行时找不到MyDll.dll会报错无法运行 7:运行Debug,就可以看到效果了:
- Dll创建及调用
- VC++ 创建及调用Dll
- VC中DLL的创建及调用
- 创建及调用QT的DLL
- dll创建及调用(VS2005)
- EVC创建DLL库及调用
- Qt DLL总结【二】-创建及调用QT的 DLL
- Qt DLL总结-创建及调用QT的 DLL
- Qt DLL总结【二】-创建及调用QT的 DLL
- Qt DLL总结-创建及调用QT的 DLL
- DLL文件在Delphi的创建及调用
- VC中DLL的创建及调用方法
- VC中DLL的创建及调用方法
- VC中DLL的创建及调用方法
- VC中DLL的创建及调用方法
- 在Dll中创建对话框并调用及AFX_MANAGE_STATE(AfxGetStaticModuleState())
- VC中DLL的创建及调用方法
- EVC创建DLL及调用的基本方法
- “不明真相”用词在于官而不在媒体
- 学习
- epoll精髓
- 怎样学好编程
- 选择
- Dll创建及调用
- 继续写博客的理由
- Mingw32 编译选项笔记
- jfreechart linux图片中文显示乱码解决方法
- 使用 Google 完全免费建站指南
- 深入理解JAVA事件机制
- sql的select查询列名未确定的问题
- 回调函数和函数指针
- Windows 消息机制