Win32 CreateProcess Help
Hi 2 all :)
Please help me with this code:
Please tell me the solution as to how to fix it.
Thanks
~Techie Gal
# 1 Re: Win32 CreateProcess Help
Debug the program (maybe single-step through it) and tell us which line gives the access violation?
Mike
# 2 Re: Win32 CreateProcess Help
Change the line
TCHAR * processPath=_T("notepad.exe");to
static TCHAR processPath[]=_T("notepad.exe");
# 3 Re: Win32 CreateProcess Help
Why do you think that that will help?
# 4 Re: Win32 CreateProcess Help
The documentation for CreateProcess says that the lpCommandLine parameter must be an LPTSTR, in other words pointer to modifiable char buffer, and that in the unicode version it really modifies the buffer (I have no idea why.) Since string literals are stored in read-only memory, the program was getting an access violation when trying to write to this literal.
# 5 Re: Win32 CreateProcess Help
The documentation for CreateProcess says that the lpCommandLine parameter must be an LPTSTR, in other words pointer to modifiable char buffer, and that in the unicode version it really modifies the buffer (I have no idea why.)I couldn't believe it, so I ran this test.
It does in fact attempt to right at the location of zero-terminator of the passed in buffer. Weird! Why?
To googler - great find!
# 6 Re: Win32 CreateProcess Help
One more comment. I found a very nice article here (http://www.possibility.com/Cpp/const.html) (search for "Const Storage and String Literals").
It claims that the reason C++ Standard allows assignment of string literal to non-const char array is to support existing incorrect code base.
Sounds reasonable...
However, it will be a great practice to always useconst TCHAR * processPath=_T("notepad.exe");I wonder if there is a way to generate at least a warning for these non-const assignments?
# 7 Re: Win32 CreateProcess Help
@googler: So, the "static" part (which, unfortunately, is what I was focusing on) isn't necessary, right?
@VladinirF: According to this link, it should fail to compile in VS2005: "String Literals Have Proper Type of const char[]" at http://msdn2.microsoft.com/en-us/library/16c4s7yy(vs.80).aspx
Mike
# 8 Re: Win32 CreateProcess Help
Hello once again !
Thanks to all of you (especially googler) from the bottom of my heart !! I failed to understand the reason why it is giving such exception... anyways ! thanks once again !
Cheers
Techie Gal :)
# 9 Re: Win32 CreateProcess Help
" at http://msdn2.microsoft.com/en-us/library/16c4s7yy(vs.80).aspxI tried really hard, but couldn't find the "fail to compile" part in there.
Also, it compiles just fine in my VS 2005, causing AV at run time...
# 10 Re: Win32 CreateProcess Help
Well, you're correct that it doesn't say "fail to compile". But it says that there is a breaking change because "String literals now have the type const char []". From that I inferred that the following line should faile to compile:
// why isn't this an error (const / non-const mismatch),
// if "notepad.exe" has type const char []?
TCHAR * processPath=_T("notepad.exe");
FOr example, the following code does not compile on Comeau:
int main()
{
const char test_c[] = "Test";
const char* testp_c = test_c;
char* testp_nc = test_c;
return 0;
}
Output:
Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 6: error: a value of type "const char *" cannot be used to
initialize an entity of type "char *"
char* testp_nc = test_c;
^
"ComeauTest.c", line 5: warning: variable "testp_c" was declared but never referenced
const char* testp_c = test_c;
^
1 error detected in the compilation of "ComeauTest.c".
So, I must have read too much into the "breaking change" part of this cite.
Mike
