.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
HINSTANCE hDLL = ::LoadLibrary(wcPath);
if (hDLL)
{
typedef int (*PTSFUNCTION)(IDevice*, IDisplay*);
PTSFUNCTION fun = (PTSFUNCTION)GetProcAddress(hDLL, "PTSMain");
if (fun)
{
if (mDisplay == nullptr)
{
mDisplay = new Display();
}
if (mDevice == nullptr)
{
mDevice = new Device();
}
std::cout << (*fun)(mDevice, mDisplay) << std::endl;
}
else
{
FXD << GetLastError();
return false;
}
}
|