C++ DLL DEMO
头文件dlltest.h
#pragma once
#include "pch.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <bitset>extern "C" __declspec(dllexport) void debugService(uint32_t debugFlags, const std::string& logFileName);
dlltest.cpp
#include "pch.h"
#include "dlltest.h"#define DEBUGvoid logMessage(const std::string& message, uint32_t debugFlags, const std::string& logFileName) {
#ifdef DEBUGstd::ostream* outStream;std::ofstream logFile;if (logFileName.empty()) {outStream = &std::cout;}else {logFile.open(logFileName, std::ios_base::app);if (!logFile.is_open()) {std::cerr << "无法打开日志文件。" << std::endl;return;}outStream = &logFile;}if (debugFlags & 0x04) {*outStream << message << std::endl;}else {for (char c : message) {*outStream << " 0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(c);}*outStream << std::endl;}if (logFile.is_open()) {logFile.close();}
#endif
}__declspec(dllexport) void debugService(uint32_t debugFlags, const std::string& logFileName) {std::string inputData = "123456789";std::string outputData = "987654321";logMessage("输入数据: " + inputData, debugFlags, logFileName);logMessage("输出数据: " + outputData, debugFlags, logFileName);
}
main
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <bitset>
#include "../Dlltest/dlltest.h"int main() {uint32_t debugFlags = 0x00000004; // 设置调试标志std::string logFileName = ""; // 空字符串表示标准输出debugService(debugFlags, logFileName);return 0;
}
输入数据: 123456789
输出数据: 987654321