프로그래밍/WIN32

CreateProcess 가 되지 않을때...

Songker 2009. 4. 1. 20:59
if(!(CreateProcess(NULL,"child.exe",NULL,NULL,TRUE,0,NULL,NULL,&si,&pi)))
 {
  fprintf(stderr,"Process creation failed\n");
  return -1;
 }

위 부분에서, 런타임 오류가 발생하면서 프로그램이 죽어버렸다.
도대체 왜이럴까... 구글링을 하던중... 해결책을 발견하였다.

문제 발생환경 : Visual c++ 2008 , Windows serer 2003


해결책 :
wchar_t runit[] = L"113.exe";
if(!(CreateProcess(NULL,runit,NULL,NULL,TRUE,0,NULL,NULL,&si,&pi)))


실행이 되었음.... 감동 ㅠㅠㅠㅠ


질문 :
The 'L' should indicate that this is a long / wide string, although this could be gcc specific.
Why when I get rid of the L I get compiler errors that say
error C2440: 'initializing' : cannot convert from 'const char [23]' to 'wchar_t []'
There is no context in which this conversion is possible
what does the L do ?

답 :
Back in the day (aka before most of use were born), there was no standard way of reprisenting text in bits and bytes. So "Ascii" was designed, and since then it's been the specification for reprisenting characters in bytes. However, there are only 256 possible different characters, and some of them are used up by things like newlines, string enders and so forth.

So relatively recently, some more people designed "Unicode", which is just the same as ascii, but it uses 2 bytes to reprisent characters. That gives it 2^16 possible characters to reprisent, which fits in every major language in the world. When window 9x was designed, they decided to ignore unicode and make is entirely ascii based. However, NT and all the recent varients exclusivly use unicode for all the native api calls and core operations.

"char" is your compiler's base definition of a 8 bit byte. The normal "CreateProcess" api call takes ascii not unicode, but it appears .net has done something weird to that... Anyhoo, I only noticed because you were casting that ascii string to LPWSTR. That means it's expecting 16 bit characters instead of 8. So, we change the type to wchar, and add 'L' to the front. The 'L' just tells the compiler to reprisent this string as 16 bit characters instead of 8 bit ascii.


간단히 말하면, 아스키코드를 인식하게 설계되었는데, 유니코드를 넣어서 발생한 문제라네요

http://forums.jinx.com/topic.asp?TOPIC_ID=50939

가셔서 더 자세한 답변을 보셔요 ^^