|
使用vc++创建dll,调用其它dll函数
1.File-->New-->rojects-->Win32 Dynamic-Link Library
2.录入工程名称,选择 An empty DLL project.
3.File-->New-->Files-->C/C++ Header File-->输入文件名称MyDll.h
4.内容如下
- #ifndef _MYDLL_H_
- #define _MYDLL_H_
-
- extern "C" _declspec (dllexport) int send(char *text,int com,int rate,int addr,int displayMode,int speed,int delayTime);
- #endif
复制代码 5.File-->New-->Files-->C++ Source File-->输入文件名称MyDll.cpp
6.内容如下
- #include "MyDll.h"
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
-
-
- int send(char *text,int com,int rate,int addr,int displayMode,int speed,int delayTime){
- char *dlladdr = "dll\\zy\\dlltpzp.dll";
- HMODULE hModule = ::LoadLibrary(dlladdr);
- if (NULL == hModule) {
- return 0;
- }
- //加载函数
- int(_stdcall *SendDatafun)(int, long, int, char *, int, int, int, int);
- SendDatafun = (int(_stdcall *)(int, long, int, char *, int, int, int, int))GetProcAddress(hModule, "SendDatafun");
- if (NULL == SendDatafun)
- {
- ::FreeLibrary(hModule);
- return -1;
- }
- UINT m_nDelayTime = 1;
- int nRetVal = SendDatafun(com, rate, addr, text,17, displayMode, speed, delayTime);
- if (0 > nRetVal)
- {
- return -2;
- }
- ::FreeLibrary(hModule);
- return 1;
- }
复制代码
7.工程右键build编译完成
8.此类是加载了一个目录在dll\\zy\\dlltpzp.dll的dll文件,并调用该dll中的SendDatafun方法
========================================================
VC++调用Dll中的函数
装载DLL
HMODULE LoadLibrary(
LPCTSTR lpFileName
);
获得所需函数
FARPROC GetProcAddress(
HMODULE hModule,
LPCSTR lpProcName
);
释放DLL
BOOL FreeLibrary(
HMODULE hModule
);
|
|