Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

c - CreateProcess method ends up with an error

I've a problem with my following code:

int main(int argc, char **argv) {
  PROCESS_INFORMATION pi;  
  STARTUPINFO si;     

  printf("Process %d reporting for duty
",GetCurrentProcessId());
  GetStartupInfo(&si);
  CreateProcess(NULL,"notepad.exe", NULL,NULL,FALSE,DETACHED_PROCESS, NULL,NULL, &si, &pi);
  printf("New Process ID: %d
",pi.dwProcessId);
  return(0);
}        

And on the runing time,I ran this while debuggin and it crashes on the CreateProcess method,with this error message:" Unhandled exception at 0x7c82f29c in Tests.exe: 0xC0000005: Access violation writing location 0x00415760." What does it means???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

32 bit executables invariably have a base address of 0x00400000. The address that cannot be written to, according to the exception is 0x00415760. Which means that your code is almost certainly trying to write to a read-only part of the executable image. That happens, for example, when you try to write to string literals.

Now, the second parameter to CreateProcess must be modifiable memory (it is declared as LPTSTR). But you are passing a string literal. Put "notepad.exe" in a modifiable buffer to solve your problem.

char CommandLine[] = "notepad.exe";
CreateProcess(NULL, CommandLine, ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...