Hi all ..

Can someone enlighten me why on this earth is this code giving out the
error.

const char *Path = "\"C:\\winnt\\system32\\cscript.exe\"";
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.dwFlags = STARTF_RUNFULLSCREEN;
ZeroMemory( &pi, sizeof(pi) );

// Start the child process.
if( !CreateProcess(NULL,
TEXT(Path),
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi ))
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}

But when i pass the same path, hardcoded into the function,
createprocess, it compiles fine. What am i doing wrong ??

Thanks for your time !!
Prakash437

Re: Error C2664: 'CreateProcessA' : cannot convert parameter 2 from by Jochen

Jochen
Tue Apr 19 05:15:00 CDT 2005

Hi prakash437!

> Can someone enlighten me why on this earth is this code giving out the
> error.
>
> const char *Path = "\"C:\\winnt\\system32\\cscript.exe\"";
> ZeroMemory( &si, sizeof(si) );
> si.cb = sizeof(si);
> si.dwFlags = STARTF_RUNFULLSCREEN;
> ZeroMemory( &pi, sizeof(pi) );
>
> // Start the child process.
> if( !CreateProcess(NULL,
> TEXT(Path),
> NULL,
> NULL,
> FALSE,
> CREATE_NEW_CONSOLE,
> NULL,
> NULL,
> &si,
> &pi ))
> {
> printf( "CreateProcess failed (%d).\n", GetLastError() );
> return;
> }
>
> But when i pass the same path, hardcoded into the function,
> createprocess, it compiles fine. What am i doing wrong ??

You can say: This is a bad definition of the "CreateProcess" function...

If you take a look at the signature of the CreateProcessA/W-function you
will see that the second parameter is an LPTSTR (*no* LPCTSTR)!
Therefor you cannot pass a "const TCHAR" to CreateProcess as second
parameter.
This might even work in ANSI-builds, but in UNICODE-builds it will
return FALSE.

<quote>
The Unicode version of this function, CreateProcessW, will fail if this
parameter is a const string.
</quote>

And here is the reason why this is the case:
<quote>
The system adds a null character to the command line string to separate
the file name from the arguments. This divides the original string into
two strings for internal processing.
</quote>

See: CreateProcess
http://msdn.microsoft.com/library/en-us/dllproc/base/createprocess.asp

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Re: Error C2664: 'CreateProcessA' : cannot convert parameter 2 from 'const char *' to 'char *' by prakash437

prakash437
Tue Apr 19 07:04:55 CDT 2005

Thanks for the reply .. It helps ..

Prakash


Re: Error C2664: 'CreateProcessA' : cannot convert parameter 2 from 'const char *' to 'char *' by Gene

Gene
Tue Apr 19 16:14:17 CDT 2005

<prakash437@gmail.com> wrote in message
news:1113905039.523521.242040@o13g2000cwo.googlegroups.com...
> Hi all ..
..
> But when i pass the same path, hardcoded into the function,
> createprocess, it compiles fine. What am i doing wrong ??

The answer to your second question lies in the decision of the c++ standard
to depart from purity for the sake of compatibility with C. Standard allows
a string literal to be converted to char*, see 4.2/2:
"A string literal (2.13.4) that is not a wide string literal can be
converted to an rvalue of type "pointer to
char"; a wide string literal can be converted to an rvalue of type "pointer
to wchar_t". In either case,
the result is a pointer to the first element of the array. This conversion
is considered only when there is an
explicit appropriate pointer target type, and not when there is a general
need to convert from an lvalue to an
rvalue. [Note: this conversion is deprecated. See Annex D. ] For the purpose
of ranking in overload resolution
(13.3.3.1.1), this conversion is considered an array-to-pointer conversion
followed by a qualification
conversion (4.4). [Example: "abc" is converted to "pointer to const char" as
an array-to-pointer conversion,
and then to "pointer to char" as a qualification conversion. ]"



Re: Error C2664: 'CreateProcessA' : cannot convert parameter 2 from 'const char *' to 'char *' by Tim

Tim
Thu Apr 21 00:36:56 CDT 2005

prakash437@gmail.com wrote:

>Hi all ..
>
>Can someone enlighten me why on this earth is this code giving out the
>error.
>
>const char *Path = "\"C:\\winnt\\system32\\cscript.exe\"";
>ZeroMemory( &si, sizeof(si) );
>si.cb = sizeof(si);
>si.dwFlags = STARTF_RUNFULLSCREEN;
>ZeroMemory( &pi, sizeof(pi) );
>
>// Start the child process.
>if( !CreateProcess(NULL,
> TEXT(Path),

As a partly relevant side note, this code has a problem. The TEXT macro
can only be used on a string constant, NOT on a variable of any kind --
even a constant variable.

When UNICODE is not defined, the TEXT macro does nothing: it returns its
first parameter. When UNICODE is defined, it returns its first parameter
with an L before it. Thus:
TEXT("hello")
produces either:
"hello"
or:
L"hello"
the latter being the syntax for a Unicode string constant. However, this:
TEXT(Path)
produces either:
Path
or:
LPath
and LPath doesn't do anything useful. Probably.
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc