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

linux打开桌面软件(wps)、获取已打开的文件名(wps)

程序测试环境是:slackware系统,属于linux系统,有桌面。系统镜像是:slackware64-15.0-install-dvd.iso。c++代码实现。

编译命令是:

gcc -o main main.cpp -lstdc++ 

 如下是测试代码demo,您可根据注释摘取自己需要的代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <dirent.h>
#include <string>
#include <sys/wait.h>
#include <ranges>// 打开默认软件
void open_wps(){pid_t pid = fork();if(pid == 0){const char* wpsCommand[] = {"vlc", nullptr};//wps(word)、et(excel)、wpspdf(pdf)、wpp(ppt)if(execvp(wpsCommand[0], (char* const*)wpsCommand) == -1){perror("failed to exec WPS");exit(EXIT_FAILURE);}else if(pid > 0){std::cout << "in main process" << std::endl;}else{perror("fork failed");exit(EXIT_FAILURE);}}
}// 根据文件打开软件
void open_wps(const char* wpsType, const std::string& filePath) {// Check if the file existsif (access(filePath.c_str(), F_OK) == -1) {std::cerr << "Error: File does not exist." << std::endl;return;}// Fork a child processpid_t pid = fork();if (pid == -1) {std::cerr << "Error: Failed to fork." << std::endl;return;}if (pid == 0) {// Child processchar *argv[] = {(char *)wpsType, (char *)filePath.c_str(), nullptr};if (execvp(argv[0], argv) == -1) {std::cerr << "Error: Failed to execute command." << std::endl;_exit(EXIT_FAILURE);}} else {// Parent process}
}// 查找wps进程id
std::vector<int> find_wps_processes(){std::vector<int> wpsPids;DIR* dir{nullptr};struct dirent* ent;if((dir = opendir("/proc")) != nullptr){while((ent = readdir(dir)) != nullptr){if(isdigit(ent->d_name[0])){                std::string pidDir = "/proc/" + std::string(ent->d_name) + "/cmdline";                std::ifstream cmdlineFile(pidDir);std::string cmdline;std::getline(cmdlineFile, cmdline, '\0');std::cout << "pid info: " << pidDir << std::endl;std::cout << "cmdline: " << cmdline << std::endl;if(cmdline.find("wps") != std::string::npos || cmdline.find("wpp") != std::string::npos){wpsPids.push_back(std::stoi(ent->d_name));}}}closedir(dir);}else{perror("could not open /proc directory");}return wpsPids;
}std::string trim(const std::string& str) {size_t first = str.find_first_not_of(" \t\n\r\f\v"); // 查找第一个非空白字符的位置if (std::string::npos == first) {return ""; // 如果字符串全是空白字符,返回空字符串}size_t last = str.find_last_not_of(" \t\n\r\f\v"); // 查找最后一个非空白字符的位置return str.substr(first, (last - first + 1)); // 截取子串
}// 打印已打开的文件信息
void print_opened_files(std::string type, int pid){std::string fdDir = "/proc/" + std::to_string(pid) + "/fd";DIR* dir;struct dirent* ent;if((dir =opendir(fdDir.c_str())) != nullptr){while ((ent = readdir(dir)) != nullptr){if(ent->d_name[0] != '.'){std::string linkTarget = fdDir + "/" + ent->d_name;char filePath[PATH_MAX] = {0};ssize_t len = readlink(linkTarget.c_str(), filePath, sizeof(filePath) - 1);if(len != 1 && std::string(filePath).find(".~") == std::string::npos){if((type == "wps" && std::string(filePath).find(".docx") != std::string::npos) ||(type == "et" && std::string(filePath).find(".xlsx") != std::string::npos) ||(type == "wpspdf" && std::string(filePath).find(".pdf") != std::string::npos) || (type == "wpp" && std::string(filePath).find(".ppt") != std::string::npos)){size_t pos = std::string(filePath).find_last_of("/");std::string fileName = std::string(filePath).substr(pos+1);std::cout << "pid: " << pid << " " << fileName << std::endl;}}}}closedir(dir);}else{perror("could not open directory");}
}// 打印wps进程信息
void print_wps_print(){std::vector<int> wpsPids = find_wps_processes();for(int pid:wpsPids){std::cout << "wps process id:" << pid << std::endl;std::string statusPath = "/proc/" + std::to_string(pid) + "/status";std::ifstream statusFile(statusPath);std::string line;while(std::getline(statusFile, line)){int pos = line.find("Name");if(pos != std::string::npos){std::string name = trim(line.substr(pos+5));std::cout << pid << ": " << name << std::endl;print_opened_files(name, pid);break;}            }}
}int main(){open_wps();sleep(2);std::string wordFilePath = "/home/gaowb/Documents/开发记录.docx";std::string wordFilePath2 = "/home/gaowb/Documents/开发记录2.docx";std::string excelFilePath = "/home/gaowb/Documents/C++西安计划安排-2.xlsx";std::string pdfFilePath = "/home/gaowb/Documents/slackware中文手册.pdf";// Open Word documentopen_wps("wps", wordFilePath);open_wps("wps", wordFilePath2);sleep(2);// Open Excel spreadsheetopen_wps("et", excelFilePath);sleep(2);// Open PDF documentopen_wps("wpspdf", pdfFilePath);sleep(2);print_wps_print();return 0;
}

代码需要说明的有如下几点:

1、wps在slackware 中安装好后,可以打开word、pdf、execl、ppt,他们分别对应的可执行文件是wps、wpspdf、et、wpp。

2、打开多个相同类型的文件,比如word,一般只有一个进程,以及一个软件界面,用多个tab页来显示多个文件。

3、代码逻辑有些随意,主要是用于测试。

相关文章:

  • rk3588s android12 自启动 C程序
  • 爱速搭百度低代码开发平台
  • 当前时机适合进入AIGC行业吗?——行业发展阶段与市场需求的分析
  • SPI总结
  • SSM私人诊所管理系统—计算机毕业设计源码36406
  • 搜索引擎简介
  • ChatGPT-o1用来进行数据分析,对比效果很惊人!
  • 力扣字典序问题详解
  • 焊接缺陷检测系统源码分享
  • C语言-线程
  • 李宏毅机器学习2022-HW8-Anomaly Detection
  • java的匿名内部类和lambda表达式
  • C语言VS实用调试技巧
  • 隧道灯光远程控制系统的设计与实现(论文+源码)_kaic
  • Spring Boot 进阶-Spring Boot 开发第一个Web接口
  • 《深入 React 技术栈》
  • Angular4 模板式表单用法以及验证
  • JDK9: 集成 Jshell 和 Maven 项目.
  • KMP算法及优化
  • Laravel5.4 Queues队列学习
  • Object.assign方法不能实现深复制
  • spring cloud gateway 源码解析(4)跨域问题处理
  • uva 10370 Above Average
  • vue2.0开发聊天程序(四) 完整体验一次Vue开发(下)
  • Vue小说阅读器(仿追书神器)
  • Vue组件定义
  • 闭包--闭包之tab栏切换(四)
  • 搭建gitbook 和 访问权限认证
  • 订阅Forge Viewer所有的事件
  • 翻译:Hystrix - How To Use
  • 判断客户端类型,Android,iOS,PC
  • 前端每日实战 2018 年 7 月份项目汇总(共 29 个项目)
  • 算法系列——算法入门之递归分而治之思想的实现
  • 微信公众号开发小记——5.python微信红包
  • 物联网链路协议
  • 写代码的正确姿势
  • MPAndroidChart 教程:Y轴 YAxis
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • ​LeetCode解法汇总2670. 找出不同元素数目差数组
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • #ubuntu# #git# repository git config --global --add safe.directory
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (32位汇编 五)mov/add/sub/and/or/xor/not
  • (C语言)输入自定义个数的整数,打印出最大值和最小值
  • (k8s)Kubernetes 从0到1容器编排之旅
  • (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (附源码)计算机毕业设计SSM疫情社区管理系统
  • (精确度,召回率,真阳性,假阳性)ACC、敏感性、特异性等 ROC指标
  • (七)MySQL是如何将LRU链表的使用性能优化到极致的?
  • (四)activit5.23.0修复跟踪高亮显示BUG
  • (一)、软硬件全开源智能手表,与手机互联,标配多表盘,功能丰富(ZSWatch-Zephyr)
  • (原)本想说脏话,奈何已放下
  • (转)程序员疫苗:代码注入