How to use the first two parameters of CreateProcess?

Source: Internet
Author: User

How to use the first two parameters of CreateProcess?

CreateProcess is an API function used to create a process in Windows. The first parameter is lpApplicationName, which literally indicates the name of an executable file, and the second parameter is lpCommandLine, literally, it should be a command line that is passed to the program when the process is started. In fact, it is not as simple as you think. The description of the two parameters of this API on MSDN is as long as two screens. He Nai's instructions are in English only. Many programmers, including me, are not good at English, and they have a headache at a glance. As a result, I simply don't want to see it. I use it based on my own feelings, and the results are always caused by various small problems, which are quite uncomfortable. Finally, I read the document and summarized it as a "NOTE" and two "usage ".

Note: lpCommandLine is LPTSTR rather than LPCTSTR, so this parameter cannot be a String constant and must be a writable string array.

If you want to execute C: \ Program Files \ WinRAR \ WinRAR.exe and input the command line "Hello I am James", there are two methods (in this example, the VC project character set is Unicode ):


1. Both lpApplicationName and lpCommandLine are used:

Wchar_t wszCmd [] = L "Hello I am James"; CreateProcess (L "C: \ Program Files \ WinRAR \ WinRAR.exe", wszCmd ,...);

Note that wszCmd [], that is, a space is required in the content of lpCommandLine. Otherwise, it will be connected with lpApplicationName and become "C: \ Program Files \ WinRAR \ WinRAR.exe Hello I am James ".


2. Use Only lpCommandLine:

Wchar_t wszPath [] = L "\" C :\\ Program Files \ WinRAR \ WinRAR.exe \ "Hello I am James"; CreateProcess (NULL, wszPath ,...);

Why must I add double quotation marks to C: \ Program Files \ WinRAR \ WinRAR.exe in the string? To avoid Ambiguity Caused by spaces in the path, for example, C: \ Program Files \ WinRAR \ WinRAR.exe can be interpreted as C: \ Program is an executable file Files \ WinRAR \ WinRAR.exe is a command line parameter, if there is no space in the path, no double quotation marks are required. It is recommended that you add double quotation marks each time to avoid errors for the sake of standardization.


MSDN reference: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425 (v = vs.85). aspx


This article is published by CharlesSimonyi on the csdn blog: http://blog.csdn.net/charlessimonyi/article/details/43957429.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.