如何获得running-exe的命令行参数? [英] How can get running-exe's command line parameter?

查看:98
本文介绍了如何获得running-exe的命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜欢标题。



我只想找到一个有命令行参数匹配的exes。



提前致谢!

like the title.

I just want find a exes which have a command line parameter that matches.

Thanks in advance!

推荐答案

在Windows上,您可以使用WMIC命令获取此信息;

On Windows you can you the WMIC command to get this information;
C:\WMIC PROCESS get Caption,Commandline,Processid





通过从 C ++ 调用它,你可以解析输出并读取你想要的信息。



这样的东西可能适用于你;





And by calling that from C++ you can parse the output and read the information you want.

Something like this might work for you;

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <string>
#include <vector>
#include <istream>
#include <ostream>
#include <sstream>

#include <stdio.h>

using namespace std;

class process_info {
public:
    const string name;
    const int id;
    const string command_line;

    process_info(const string& name, const int id, const string command_line)
        : name(name), id(id), command_line(command_line) {
    }
};

int to_int(const string& value) {
    int i;
    istringstream ss(value);
    ss >> i;
    return i;
}

string trim(const string& str, const char trim_char) {
    string::size_type start = str.find_first_not_of(trim_char);
    string::size_type end = str.find_last_not_of(trim_char);

    if (start == end)
        return "";

    string temp = str.substr(0, end + 1);

    return temp.substr(start, temp.size());
}

string trim_whitespace(const string& str) {
    return trim(trim(trim(trim(str, ' '), '\t'), '\r'), '\n');
}

string run_command(const char* cmd) {
    FILE* pipe = _popen(cmd, "r");
    if (!pipe)
        return "ERROR";

    static const int buffer_size = 2048;
    char buffer[buffer_size];
    string result = "";
    while(!feof(pipe)) {
        if(fgets(buffer, buffer_size, pipe) != NULL)
            result += buffer;
    }
    _pclose(pipe);
    return result;
}

int main(int argc, char* argv[]) {
    const string result = run_command("WMIC PROCESS get Caption,Commandline,Processid");
    stringstream str(result);

    string line;
    vector<process_info> processes;

    if (getline(str, line, '\r')) {
        const string::size_type cl_pos = line.find("CommandLine", 0);
        const string::size_type pid_pos = line.find("ProcessId", 0);

        while(getline(str, line, '\r')) {
            if (line.find_first_not_of('\n') != string::npos) {
                if (line.size() > 0) {
                    const string name = trim_whitespace(line.substr(0, cl_pos));
                    const string command_line = trim_whitespace(line.substr(cl_pos, pid_pos-cl_pos));
                    const string pid = trim_whitespace(line.substr(pid_pos, line.size() - pid_pos));

                    if (pid.size() > 0) {
                        const process_info info(name, to_int(pid), command_line);
                        processes.push_back(info);
                    }
                }
            }
        }
    }

    cout << "Dumping list of processes: " << endl;
    for(string::size_type i = 0; i < processes.size(); ++i) {
        cout << "[" << processes[i].id << "] " << processes[i].name << "; command line={" << processes[i].command_line << "}" << endl;
    }

    return 0;
}





(对手动字符串解析道歉,我假设您将使用 boost 或类似的最终版本。)



希望这会有所帮助,

Fredrik Bornander



(apologies for the "manual" string parsing, I am assuming you'll use boost or something like that for your final version).

Hope this helps,
Fredrik Bornander

如果要查看正在运行的可执行文件的调用方式(包括其参数),可以使用任务管理器。我有Vista;其他版本可能有不同的细节。



右键单击Windows工具栏并选择任务管理器。转到进程选项卡。选择查看 - >选择列。在此对话框中,确保选中命令行,单击确定。您应该显示所有正在运行的应用程序的命令行。如果您的兴趣流程没有显示,请确保选中显示所有用户的流程复选框/按钮。
If you want to see how a running executable was called (including its parameters) you can use Task Manager. I have Vista; other versions might have different details.

Right click on the Windows tool bar and select "Task Manager". Go to the Processes tab. Select View -> Select Columns. On this dialog, make sure that "Command Line" is selected, click OK. You should have the command line of all running apps displayed. If your process of interest does not show up, make sure you have the "show processes from all users" checkbox/button selected.


这篇关于如何获得running-exe的命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆