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 DEBUG

void logMessage(const std::string& message, uint32_t debugFlags, const std::string& logFileName) {
#ifdef DEBUG
    std::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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值