Convert argv back to single line

I am writing a (Win32 Console) program that terminates another process; It takes parameters, as in the following example:

runas.exe user notepad foo.txt

That is: it runasanalyzes userand then starts the notebook, passing the remaining parameters.

My problem is that it argvbreaks down into individual parameters, but CreateProcessAsUserone parameter is required lpszCommandLine.

Building this command line is probably not as simple as simply combining argvbackwards with spaces. Any pointers?

This is just an example. My first argument is not really a username and may contain spaces. This makes GetCommandLineit difficult to manually analyze the result .

Similarly, naive concatenation argvwill not work, because it must deal with the case where the original arguments were quoted and may contain spaces in them.

+5
source share
4 answers

Manual recombination is difficult:

You could try re-combining them, I think this will work, but be sure to follow the same command line as the screens . This may be more than the simple solution you are looking for.

, - , , . : --folderpath "c:\test\", - "c:\test\\".

MFC:

CWinApp theApp.m_lpCmdLine. , __argc __argv CommandLineToArgvW.

Win32 ( GUI):

WinMain. .

, __argc __argv CommandLineToArgvW.

wmain:

, Win32 API GetCommandLine. , .exe. / exe. , .

+7

GetCommandLine.

"WinMain" "main"? .

+2

API Win32, : GetCommandLine

+1

If you have a line allocated with enough space, use strcat for each item in the list. Yes, it is as easy as combining them with spaces.

Edit: Of course, you will need to enclose any elements containing spaces in quotation marks.

0
source

All Articles