如何获取正在运行的进程的命令行参数 [英] How to get command line arguments for a running process

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

问题描述

在我的程序中,当我使用mxmlc 的命令行编译命令.该错误与flex 中的嵌入字体名称未正确识别系统字体列表.

In my program, I have been receiving an error when I use a command-line compile command for mxmlc. The error is related to an embedded font name not being correctly identified by flex in the system fonts list.

然而,一时兴起,我决定将代码复制到 Flex Builder 并在那里编译.令我惊讶的是,它奏效了,并且找到了合适的字体使用我提供的相同系统名称 (PMingLiU).

However, on a whim, I decided to copy the code to Flex Builder and compile it there. To my surprise, it worked, and it found the proper font using the same system name I had given (PMingLiU).

我怀疑我的问题可能是语言环境的问题,而我的系统无法出于语言环境的考虑,正确识别字体名称.

I suspected my problem may be a locale one, and that my system cannot correctly identify the font name because of locale considerations.

我尝试将编译代码的语言环境设置为 en_US,设置为 no有用.所以我想问这里有没有人知道Flex Builder 是如何调用MXML 编译器的,和直接运行mxmlc 相比有什么区别?我们知道它没有直接使用 mxmlc.exe,因为我们尝试用我们自己的可执行文件替换 mxmlc 来捕获命令行参数.

I've tried setting the locale of the compile code to en_US, to no avail. So I would like to ask if anyone here knows how exactly Flex Builder invokes the MXML compiler and what differences there are compared to running mxmlc directly? We know it's not using the mxmlc.exe directly, since we tried replacing mxmlc with our own executable to capture the command line parameters.

如果重要,使用的操作系统是 Windows XP.

If it matters, the OS used is Windows XP.

推荐答案

虽然我没有你的问题的确切答案(Flex Builder 传递给 mxmlc.exe 的命令行参数),但我有一个元答案为你.您可以使用以下两种方法之一找到命令行.

Although I don't have the exact answer to your question (what command line arguments Flex Builder passes to mxmlc.exe), I do have a meta-answer for you. You can find the command line by using one of two methods.

第一个与平台无关,但需要您编译一个小的 C++ 程序.我以前在解决类似问题时使用过这种方法.您可以做的是创建一个包装应用程序,它只是将命令行输出到一个文件.构建此应用程序并将其作为 mxmlc.exe 的临时替代品放入,当 Flex Builder 执行它时,您将能够访问生成的文件cmdline.txt"以获取调用它的完整命令行:

The first is platform-agnostic but will require you to compile a small C++ program. I've used this approach before when solving similar problems. What you can do is create a wrapper application which simply outputs the command line to a file. Build this application and drop it in as a temporary replacement for your mxmlc.exe, and when Flex Builder executes it you'll be able to access the resulting file "cmdline.txt" to get the full command line that it was called with:

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char* argv[])
{
  ofstream cmdLine;
  cmdLine.open("cmdline.txt");

  for (int i = 0; i < argc; i++) {
    cmdLine << argv[i];
    if (i < argc)
      cmdLine << " ";
  }

  cmdLine.close();
  return 0;
}

如果您觉得在 Flex Builder 上玩这个肮脏的把戏是不对的,假设您在 Windows 上运行,还有另一种选择.您可以使用 WMI 遍历所有正在运行的进程并获取它们的命令行信息.Ruby 是我选择的语言,这需要您安装适用于 Windows 的 Ruby 解释器,您可以使用 One-单击适用于 Windows 的 Ruby 安装程序.

If you don't feel right about playing this dirty trick on Flex Builder, there is an alternative assuming you're running on Windows. You can use WMI to iterate over all of the running processes and grab their command line information. Ruby being my language of choice, this would require you to install the Ruby interpreter for Windows which you can do easily with the One-Click Ruby Installer for Windows.

安装后,只要 Flex Builder 开始构建,就立即运行此脚本:

After installing, just run this script as soon as Flex Builder kicks off your build:

require 'win32ole'
wmi = WIN32OLE.connect("winmgmts://")
processes = wmi.ExecQuery("select * from win32_process")

for process in processes do
    cmdLine = process.CommandLine
    puts "Command line: #{cmdLine}" if cmdLine =~ /mxmlc/
end

我添加了一个正则表达式来打印命令行中仅用于在命令行中以mxmlc"开头的进程(它应该可以满足您的需要).对于迭代每个进程的更通用的解决方案,只需删除包含以下内容的行末尾的 if 子句:

I've added in a regular expression to print out the command line only for processes which were started with "mxmlc" in the command line (which should work for your needs). For a more general solution of iterating over each process, just remove the if clause at the end of the line containing:

puts "Command line: #{cmdLine}" if cmdLine =~ /mxmlc/

这将使您免于使用 StartRemoteThread 执行任何低级魔术并在 PEB 结构中导航.

This will save you the headache of doing any low-level magic with StartRemoteThread and navigating through the PEB structures.

考虑到您的问题的性质,并且没有关于您的开发操作系统的更多信息,这是我所能做的最好的事情.如果这解决了您的问题,我可能建议您编辑您的帖子,以便面临类似问题的人可以找到此解决方案.像如何获取正在运行的进程的命令行参数"这样的标题可能更贴切.

That's about the best I could do considering the nature of your question and without more information regarding your development OS. If this solves your problem I might suggest that you edit your post so that people facing similar issues can find this solution. A title like "How to get command line arguments for a running process" might be more apt.

这篇关于如何获取正在运行的进程的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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