.h 文件
#if defined(WIN32)
#ifdef PTS_API_EXPORTS
#define PTS_API __declspec(dllexport)
#else
#define PTS_API __declspec(dllimport)
#endif
#else
#define PTS_API
#endif
extern "C" int PTS_API PTSMain(IDevice * device, IDisplay * display);
.cpp 文件
extern "C" int PTS_API PTSMain(IDevice * device, IDisplay * display)
{
VehicleAttr vehicleAttr;
IVehicle* vehicle = new Vehicle(vehicleAttr, device, display);
display->LogData("Startup", "success");
display->SetVehicle(vehicle);
std::cout << "Hello World!\n";
return 0;
}
调用方式
#ifdef WIN32
#include <Windows.h>
#endif // WIN32
HINSTANCE hDLL = ::LoadLibrary(wcPath);
if (hDLL)
{
//int PTS_API PTSMain(const VehicleAttr & vehicleAttr, IDevice * device, IDisplay * display)
//typedef int (WINAPI* PTSFUNCTION)(const VehicleAttr&, IDevice*, IDisplay*);
typedef int (*PTSFUNCTION)(IDevice*, IDisplay*);
//这里是获取调用dll函数的地址
PTSFUNCTION fun = (PTSFUNCTION)GetProcAddress(hDLL, "PTSMain");
if (fun)
{
if (mDisplay == nullptr)
{
mDisplay = new Display();
}
if (mDevice == nullptr)
{
mDevice = new Device();
}
//这里是调用dll中的函数 函数返回的是 int 类型数值
//std::cout << (*fun)(mVehicleAttr, mDevice, mDisplay) << std::endl;
std::cout << (*fun)(mDevice, mDisplay) << std::endl;
}
else
{
//如果调用错误就打印错误信息
FXD << GetLastError();
return false;
}
}