c++ - 在Linux上的C++中执行Shell命令xdotool

标签 c++ linux exec xdotool

#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



这是输出:

command is xdotool
argument is mousemove
x is 1500
y is 1500

mousemove: Unknown command: 1500
Run 'mousemove help' if you want a command list

最佳答案

组装args时,需要添加“xdotool”作为第一个参数。

man page获得execvp:

The first argument, by convention, should point to the filename associated with the file being executed.



所以应该是char *args[] = {"xdotool", argument, x, y, nullptr};

关于c++ - 在Linux上的C++中执行Shell命令xdotool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61281332/

相关文章:

c++ - 如何使用 POCO 和 C++ 监听浏览器请求

linux - bash代码有问题

linux - 在 Linux 中将文本转换为 pdf 的简单解决方案

flash - Flash编译器不允许覆盖

java - 来 self 们 Java 程序的外部程序

c++ - wxWidgets 试图填满整个区域

c++ - 如何使用 Freetype 沿基线对齐字形?

C++ 对象的 C++ vector 到 mex 中的 mxArray

linux - Hadoop在两台不同名称的机器上配置多节点集群

php - 我可以在 PHP 函数 exec() 中使用 linux 内置实用程序 compgen 吗?