[英]Execute shell command xdotool in C++ on linux
#include <unistd.h>
#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
int main(void)
{
// Trying to execute:
// xdotool mousemove 1500 1500
// char command[] = "/usr/bin/xdotool\0";
char command[] = "xdotool\0";
char argument[] = "mousemove\0";
int src_x = 1500;
int src_y = 1500;
char x[5];
memset(x, 0x00, 5);
sprintf(x, "%d", src_x);
char y[5];
memset(y, 0x00, 5);
sprintf(y, "%d", src_y);
cout << "command is " << command << "\n";
cout << "argument is " << argument << "\n";
cout << "x is " << x << "\n";
cout << "y is " << y << "\n\n";
char *args[] = {argument, x, y, nullptr};
return execvp(command, args);
/* char *args[] = {x, y, nullptr};
char *com[] = {argument, nullptr};
return execvpe(command, com, args);
*/
}
這段代碼有什么問題?
該命令運行,但似乎將每個參數解釋為另一個執行。
不確定,但我相信它正在運行:
xdotool mousemove
xdotool 1500
xdotool 1500
這是 output:
命令是 xdotool
參數是鼠標移動
x 是 1500
y 是 1500mousemove:未知命令:1500
如果需要命令列表,請運行“mousemove help”
組裝args
時,您需要添加“xdotool”作為第一個參數。
從execvp
的手冊頁:
按照慣例,第一個參數應該指向與正在執行的文件關聯的文件名。
所以應該是char *args[] = {"xdotool", argument, x, y, nullptr};
.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.