简体   繁体   English

调用CreateProcessAsUser时,是否需要将exe路径指定为lpCommandLine中的第一个参数?

[英]Do I need to specify an exe path as the 1st parameter in lpCommandLine when calling CreateProcessAsUser?

I can't seem to find a definitive answer to this. 我似乎找不到确切的答案。 My goal is to start a process using a user token. 我的目标是使用用户令牌启动一个过程。 Say, the process in question is started as such: 说,所涉及的过程是这样开始的:

"C:\My folder\My proc.exe" param=1

So when I specify lpCommandLine parameter for the CreateProcessAsUser API, do I need to specify executable path as the 1st parameter as such: 因此,当我为CreateProcessAsUser API指定lpCommandLine参数时,是否需要将可执行路径指定为第一个参数,例如:

LPCTSTR pStrExePath = L"C:\\My folder\\My proc.exe";

TCHAR buffCmdLine[MAX_PATH];
if(SUCCEEDED(::StringCchPrintf(buffCmdLine, MAX_PATH, 
    L"\"%s\" %s", pStrExePath, L"param=1")))

bResult = CreateProcessAsUser(
    hToken,            // client's access token
    pStrExePath,       // file to execute
    buffCmdLine,       // command line
    NULL,              // pointer to process SECURITY_ATTRIBUTES
    NULL,              // pointer to thread SECURITY_ATTRIBUTES
    FALSE,             // handles are not inheritable
    NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT,   // creation flags
    envBlock,          // pointer to new environment block 
    NULL,              // name of current directory 
    &si,               // pointer to STARTUPINFO structure
    &pi                // receives information about new process
);

Or can I omit the exe path and do this? 还是我可以省略exe路径并执行此操作?

LPCTSTR pStrExePath = L"C:\\My folder\\My proc.exe";

TCHAR buffCmdLine[MAX_PATH];
if(SUCCEEDED(::StringCchCopy(buffCmdLine, MAX_PATH, L"param=1")))

bResult = CreateProcessAsUser(
    hToken,            // client's access token
    pStrExePath,       // file to execute
    buffCmdLine,       // command line
    NULL,              // pointer to process SECURITY_ATTRIBUTES
    NULL,              // pointer to thread SECURITY_ATTRIBUTES
    FALSE,             // handles are not inheritable
    NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT,   // creation flags
    envBlock,          // pointer to new environment block 
    NULL,              // name of current directory 
    &si,               // pointer to STARTUPINFO structure
    &pi                // receives information about new process
);

They both seem to work. 他们似乎都工作。

Reading the documentation, both cases should work. 阅读文档,这两种情况都应该起作用。

From MSDN MSDN

If both lpApplicationName and lpCommandLine are non-NULL, *lpApplicationName specifies the module to execute, and *lpCommandLine specifies the command line . 如果lpApplicationName和lpCommandLine均为非NULL,则* lpApplicationName指定要执行的模块 ,而* lpCommandLine指定命令行 The new process can use GetCommandLine to retrieve the entire command line . 新进程可以使用GetCommandLine检索整个命令行 Console processes written in C can use the argc and argv arguments to parse the command line. 用C编写的控制台进程可以使用argc和argv参数来解析命令行。 Because argv[0] is the module name, C programmers generally repeat the module name as the first token in the command line . 由于argv [0]是模块名称,因此C程序员通常将模块名称作为命令行中的第一个标记重复

I agree that the documentation may be clearer saying that it accepts the argument part of the command line or the full command line in lpCommandLine when lpApplicationName is non-NULL. 我同意文档可能会更清晰地说,当lpApplicationName为非NULL时,它接受lpCommandLine命令行的参数部分或完整命令行。

UPDATE : The documentation is better in the case lpApplicationName is NULL 更新:在lpApplicationName为NULL的情况下,文档会更好

If lpApplicationName is NULL, the first white space–delimited token of the command line specifies the module name... 如果lpApplicationName为NULL,则命令行的第一个用空格分隔的标记指定模块名称...

UPDATE 2 : There is a nice documentation about these arguments Understanding CreateProcess and Command-line Arguments . 更新2 :关于这些参数的文档很好,可以理解CreateProcess和命令行参数 Reading this documentation, I understand that there is a difference between your two cases. 阅读本文档后,我了解到您的两种情况有所不同。 When you provide lpApplicationName and arguments in lpCommandLine the child process will parse the command line as it is in lpCommandLine . 当您在lpCommandLine提供lpApplicationName和参数时,子进程将像在lpCommandLine一样解析命令行。 So if you do not duplicate the exe path, argv[0] will not represent the exe path as usual but param=1. 因此,如果您不复制exe路径,则argv [0]不会照常表示exe路径,而是param = 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM