Purpose:
Read the argv array and create a space-separated single command line string for C/C++ programs.
This is opposite to what CommandLineToArgvW does.
LPWSTR ArgvWToCommandLine(int* argc, LPWSTR argv[])
{
int lpCmdLineLen = 0;
LPWSTR lpCmdLine = NULL;
LPWSTR lpCmdLineIter = NULL;
LPWSTR* argvIter = NULL;
LPWSTR argvIterPtr = NULL;
if (0 < *argc)
{
// Calculate string length for the new command line
// +1 for space delimeters and for the terminating null character.
for (argvIter = argv; *argvIter != argv[*argc]; argvIter++)
{
argvIterPtr = *argvIter;
while (*argvIterPtr++)
{
lpCmdLineLen++;
}
lpCmdLineLen++;
}
/*SIMPLER VERSION
for (argIndex = 0; argIndex < *argc; argIndex++)
{
lpCmdLineLen += strlen(argv[argIndex]) + 1;
}*/
lpCmdLine = (LPWSTR)LocalAlloc(
LMEM_ZEROINIT,
lpCmdLineLen * sizeof(char));
if (lpCmdLine)
{
lpCmdLineIter = lpCmdLine;
argvIter = argv;
for (int argIndex = 0; argIndex < *argc; argIndex++)
{
while (*lpCmdLineIter)
{
lpCmdLineIter++;
}
argvIterPtr = *argvIter;
while (*argvIterPtr)
{
*lpCmdLineIter++ = *argvIterPtr++;
}
if (argIndex + 1 < *argc)
{
*lpCmdLineIter++ = TEXT(' ');
}
else
{
*lpCmdLineIter = TEXT('\0');
}
argvIter++;
}
}
else
{
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
}
}
else
{
SetLastError(ERROR_ARITHMETIC_OVERFLOW);
}
return (lpCmdLine);
}
No comments:
Post a Comment