当前位置: 首页 > news >正文

C++ 之 cli窗口交互程序测试DLL

C++ 之 cli窗口交互程序测试DLL

  • 在进行cmd窗口调用DLL时,根据用户不同的输入相应的执行不同的接口进行测试,支持多线程测试:

    • C++ 测试dll程序
    • C# 测试dll程序

1. C++实现

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>  // 用于Windows平台的键盘输入检测
#include <windows.h> // 用于Sleep函数// 示例接口函数
void startFunction() {printf("启动功能已调用!\n");// 这里可以添加实际的启动代码
}void stopFunction() {printf("停止功能已调用!\n");// 这里可以添加实际的停止代码
}void pauseFunction() {printf("暂停功能已调用!\n");// 这里可以添加实际的暂停代码
}void resumeFunction() {printf("恢复功能已调用!\n");// 这里可以添加实际的恢复代码
}void exitFunction(int *running) {printf("退出程序...\n");*running = 0;
}// 显示帮助信息
void showHelp() {printf("\n=== 键盘按键交互测试程序 ===\n");printf("按键映射:\n");printf("  S/s - 启动功能\n");printf("  T/t - 停止功能\n");printf("  P/p - 暂停功能\n");printf("  R/r - 恢复功能\n");printf("  H/h - 显示帮助\n");printf("  Q/q - 退出程序\n");printf("============================\n\n");
}int main() {int running = 1;showHelp();printf("程序已启动,请按相应键进行测试...\n");while (running) {// 检查是否有按键按下if (_kbhit()) {char key = _getch();// 处理按键switch (key) {case 'S':case 's':startFunction();break;case 'T':case 't':stopFunction();break;case 'P':case 'p':pauseFunction();break;case 'R':case 'r':resumeFunction();break;case 'H':case 'h':showHelp();break;case 'Q':case 'q':exitFunction(&running);break;default:printf("未定义的按键: %c (按H查看帮助)\n", key);break;}}// 短暂休眠以减少CPU占用Sleep(100);}printf("程序已退出。\n");return 0;
}

C#

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Collections.Generic;public class NativeMethods
{// 假设的C++ DLL接口 - 请根据实际DLL修改[DllImport("YourCppDll.dll", CallingConvention = CallingConvention.Cdecl)]public static extern void StartOperation(int operationId);[DllImport("YourCppDll.dll", CallingConvention = CallingConvention.Cdecl)]public static extern void StopOperation(int operationId);[DllImport("YourCppDll.dll", CallingConvention = CallingConvention.Cdecl)]public static extern int GetOperationStatus(int operationId);[DllImport("YourCppDll.dll", CallingConvention = CallingConvention.Cdecl)]public static extern void Initialize();[DllImport("YourCppDll.dll", CallingConvention = CallingConvention.Cdecl)]public static extern void Cleanup();
}class Program
{private static bool isRunning = true;private static Dictionary<ConsoleKey, int> keyMapping = new Dictionary<ConsoleKey, int>();static void Main(string[] args){// 初始化键位映射(可以根据需要修改)InitializeKeyMapping();Console.WriteLine("=== 实时线程控制程序 ===");Console.WriteLine("按键映射:");foreach (var mapping in keyMapping){Console.WriteLine($"{mapping.Key}: 控制操作{mapping.Value}");}Console.WriteLine("Space: 显示状态");Console.WriteLine("ESC: 退出程序");Console.WriteLine("==========================");// 初始化DLL(如果有需要的话)try{NativeMethods.Initialize();Console.WriteLine("DLL初始化成功");}catch (Exception ex){Console.WriteLine($"DLL初始化失败: {ex.Message}");}// 设置控制台以便实时读取按键Console.TreatControlCAsInput = true;Console.CursorVisible = false;// 主循环 - 实时检测键盘输入while (isRunning){if (Console.KeyAvailable){ProcessKeyPress();}// 短暂休眠以减少CPU使用率Thread.Sleep(10);}// 清理资源try{NativeMethods.Cleanup();Console.WriteLine("DLL清理完成");}catch (Exception ex){Console.WriteLine($"DLL清理错误: {ex.Message}");}Console.WriteLine("程序已退出");Console.CursorVisible = true;}static void InitializeKeyMapping(){// 定义按键与操作ID的映射关系keyMapping[ConsoleKey.D1] = 1;  // 按1键控制操作1keyMapping[ConsoleKey.D2] = 2;  // 按2键控制操作2keyMapping[ConsoleKey.D3] = 3;  // 按3键控制操作3keyMapping[ConsoleKey.D4] = 4;  // 按4键控制操作4// 可以继续添加更多映射...}static void ProcessKeyPress(){ConsoleKeyInfo keyInfo = Console.ReadKey(true);switch (keyInfo.Key){case ConsoleKey.Escape:isRunning = false;Console.WriteLine("\n退出程序...");break;case ConsoleKey.Spacebar:ShowAllStatus();break;default:if (keyMapping.ContainsKey(keyInfo.Key)){int operationId = keyMapping[keyInfo.Key];ToggleOperation(operationId);}else{Console.WriteLine($"\n未知按键: {keyInfo.Key}");}break;}}static void ToggleOperation(int operationId){try{int status = NativeMethods.GetOperationStatus(operationId);if (status == 0) // 假设0表示停止状态{NativeMethods.StartOperation(operationId);Console.WriteLine($"\n启动操作 {operationId}");}else{NativeMethods.StopOperation(operationId);Console.WriteLine($"\n停止操作 {operationId}");}}catch (Exception ex){Console.WriteLine($"\n操作 {operationId} 执行失败: {ex.Message}");}}static void ShowAllStatus(){Console.WriteLine("\n=== 当前状态 ===");foreach (var mapping in keyMapping){try{int status = NativeMethods.GetOperationStatus(mapping.Value);string statusText = status == 0 ? "停止" : "运行中";Console.WriteLine($"操作 {mapping.Value}: {statusText}");}catch (Exception ex){Console.WriteLine($"操作 {mapping.Value}: 状态获取失败 - {ex.Message}");}}Console.WriteLine("================");}// 如果需要处理控制台关闭事件[DllImport("Kernel32")]private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);private delegate bool EventHandler(CtrlType sig);private static EventHandler _handler;enum CtrlType{CTRL_C_EVENT = 0,CTRL_BREAK_EVENT = 1,CTRL_CLOSE_EVENT = 2,CTRL_LOGOFF_EVENT = 5,CTRL_SHUTDOWN_EVENT = 6}private static bool Handler(CtrlType sig){Console.WriteLine("正在关闭程序...");isRunning = false;Thread.Sleep(1000); // 给清理操作一些时间return false;}
}

文章转载自:

http://HluWj96e.Lmxfy.cn
http://A5wvHduz.Lmxfy.cn
http://uTAk7gut.Lmxfy.cn
http://GrECHlIo.Lmxfy.cn
http://Lj3mfAcd.Lmxfy.cn
http://gn7esDOJ.Lmxfy.cn
http://Hi3jJxLG.Lmxfy.cn
http://PJHA4x1I.Lmxfy.cn
http://9lkvvG6t.Lmxfy.cn
http://CjvI9DPU.Lmxfy.cn
http://ZwEP0iZJ.Lmxfy.cn
http://bsHqpDE6.Lmxfy.cn
http://BbsZFC2d.Lmxfy.cn
http://zC8n3JIV.Lmxfy.cn
http://AGBFAgFY.Lmxfy.cn
http://dFK0iuh1.Lmxfy.cn
http://VOlYLq7S.Lmxfy.cn
http://ruHYPaHQ.Lmxfy.cn
http://oxq42C8x.Lmxfy.cn
http://LbQYKle5.Lmxfy.cn
http://UQlzaRXo.Lmxfy.cn
http://4kbY11WC.Lmxfy.cn
http://YGnpqaw3.Lmxfy.cn
http://9jGT3E5d.Lmxfy.cn
http://bc9Zepke.Lmxfy.cn
http://Vyhm0j3U.Lmxfy.cn
http://IvZ3G9dU.Lmxfy.cn
http://2SUpR091.Lmxfy.cn
http://IEHeGxHV.Lmxfy.cn
http://7VOlws7S.Lmxfy.cn
http://www.dtcms.com/a/378368.html

相关文章:

  • openEuler系统远程管理方案:cpolar实现安全高效运维
  • Spring常用注解介绍
  • 《秋鳞小故事——编译器》
  • 【前端Vue】如何优雅地在vue中引入ace-editor编辑器
  • 架构深解:英伟达Rubin CPX如何通过专用预填充加速器与解耦架构重塑AI推理效率与成本
  • 线下小店悄然增长:两个关键模式与它们的运营启示
  • 开发安全利器:detect-secrets 敏感信息扫描工具实战指南
  • 中间件架构设计与实践:构建高性能分布式系统的核心基石
  • 错误于make.names(vnames, unique = TRUE): invalid multibyte string 9 使用 R 语言进行数据处理时
  • 前端基础标签
  • 深度学习基本模块:ConvTranspose2D 二维转置卷积层
  • 多模态数据治理新范式:衡石Agentic BI如何统一结构化与非结构化数据?
  • Gopeed下载器本地部署指南:cpolar实现远程任务管理
  • App 苹果 上架全流程解析 iOS 应用发布步骤、App Store 上架流程
  • unity UGUI 鼠标画线
  • ALBEF(Align Before Fuse)
  • redis 集群——redis cluster(去中心化)
  • k8s部署kafka三节点集群
  • 11.ImGui-加载字体和中文
  • 大模型推理革命
  • 项目-sqlite类的实现
  • 物联网领域中PHP框架的最佳选择有哪些?
  • ARM1.(ARM体系结构)
  • Linux开机启动设置全攻略
  • 解决Pytest参数化测试中文显示乱码问题:两种高效方法
  • PHP弱类型比较在CTF比赛中的深入分析与实战应用
  • 科大讯飞一面
  • html块标签和内联标签的通俗理解
  • 【C++】STL--Vector使用极其模拟实现
  • QT子线程与GUI线程安全交互