How does C++ call the method in the library to operate the hardware according to the DLL library provided by the manufacturer?
1. What is a DLL?
Dynamic Link Library (Dynamic Link Library) DLL files are also executable files like EXE files, but DLL is also called a library, because it encapsulates various types, functions, and the like, just like a library , stores a lot of things, mainly for calling.
2. Call method
There are two main calling methods: implicit (through lib files and header files) and explicit (only through DLL files).
3. Call method
Implicit call:
//将lib文件及头文件导入的项目中,并通过头文件调用库中的函数
配置参考链接:https://zhuanlan.zhihu.com/p/490440768
Show call:
HMODULE hMod = LoadLibraryA("POS_SDK.dll");//载入动态库
FreeLibrary(hMod);//卸载函数库
4. Practical operation
```cpp
#include <iostream>
#include <windows.h>
#include <string.h>
#include "test.h"
#define char TCHAR
#define _AFXDLL
using namespace std;
/*
根据厂家提供的开发文档调用对应函数接口,如何没有文档可以使用工具查看动态库中的函数功能。
*/
typedef long(*POS_Port_OpenA)(LPSTR szName, INT iPort, BOOL bFile, LPSTR szFilePath);
typedef long(*POS_Output_PrintFontStringA)(LONG iPrinterID, INT iFont, INT iThick, INT iWidth, INT iHeight,
INT iUnderLine, LPCSTR lpString);
using namespace std;
int testDell()
{
cout << "------------Dell库调用练习测试程序------------" << endl;
HMODULE hMod = LoadLibraryA("POS_SDK.dll");
if (hMod != NULL) {
POS_Port_OpenA posfunc;
posfunc = (POS_Port_OpenA)GetProcAddress(hMod, "POS_Port_OpenA");
if (posfunc != NULL) {
cout << "========找到对应的函数,即将开始调用函数===========" << endl;
char cmd[1000] = "COM8:115200, N, 8, 1";
long result = posfunc(cmd, 1000, FALSE, NULL);
cout << "获取的设备ID号码为:" << result << endl;
if (result < 0) {
cout << "串口打开失败,请检查串口号是否准确" << endl;
}
else
{
cout << "串口打开成功,可以正常操作凭条打印机" << endl;
POS_Output_PrintFontStringA stringfunc;
stringfunc = (POS_Output_PrintFontStringA)GetProcAddress(hMod, "POS_Output_PrintFontStringA");
if (stringfunc != NULL) {
cout << "成功:凭条打印机字符串打印接口查询接口找到了" << endl;
stringfunc(result, 0, 0, 0, 0, 0, "Hi, thank you for choosing our printer");
}
else
{
cout << "失败:凭条打印机字符串打印接口没有找到了" << endl;
}
}
}
else
{
cout << "未找到该函数,可能是函数名字错误请检查!" << endl;
}
FreeLibrary(hMod);
}
else
{
cout << "没有打开指定的DLL库,请检查!" << endl;
}
return 1;
}