积极答复者
请问编译出错 哪的问题呢?谢谢

问题
-
代码如下:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <winbase.h>
using namespace std;int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFOW si;
si.cb=sizeof(si);
wchar_t *sz_CommandLine=L"notepad";
PROCESS_INFORMATION pi;
::CreateProcessW(NULL,sz_CommandLine,NULL,NULL,FALSE,NULL,NULL,NULL,&si,&pi);
return 0;
}
编译提示:
1.exe 中的 0x7c82f29c 处最可能的异常: 0xC0000005: 写入位置 0x00415806 时发生访问冲突
1.exe 中的 0x7c82f29c 处未处理的异常: 0xC0000005: 写入位置 0x00415806 时发生访问冲突
程序“[2316] 1.exe: 本机”已退出,返回值为 0 (0x0)。
答案
-
Psdty 你好:
我觉得这个问题是由 wchar_t *sz_CommandLine=L"notepad"; 代码引起的。sz_CommandLine指向的是一个常量字符数组。并且si和pi并没有初始化。请参考下面代码:
PROCESS_INFORMATION pi; ZeroMemory(&pi,sizeof(PROCESS_INFORMATION)); STARTUPINFO si; ZeroMemory(&si,sizeof(STARTUPINFO)); si.cb=sizeof(STARTUPINFO); si.wShowWindow=SW_SHOW; si.dwFlags=STARTF_USESHOWWINDOW; wchar_t szCmdline[] = L"C:\\WINDOWS\\notepad.exe d:\\test.txt"; BOOL fRet=::CreateProcess(NULL, szCmdline, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi);
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- 已标记为答案 psdty 2010年2月5日 5:42
全部回复
-
谢谢
问题在这里
The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.
但是改了之后 调式还会出错
si = {cb=3435973836 lpReserved=0xcccccccc <错误的指针> lpDesktop=0xcccccccc <错误的指针> ...}
lpReserved2 = 0xcccccccc <错误的指针> -
Psdty 你好:
我觉得这个问题是由 wchar_t *sz_CommandLine=L"notepad"; 代码引起的。sz_CommandLine指向的是一个常量字符数组。并且si和pi并没有初始化。请参考下面代码:
PROCESS_INFORMATION pi; ZeroMemory(&pi,sizeof(PROCESS_INFORMATION)); STARTUPINFO si; ZeroMemory(&si,sizeof(STARTUPINFO)); si.cb=sizeof(STARTUPINFO); si.wShowWindow=SW_SHOW; si.dwFlags=STARTF_USESHOWWINDOW; wchar_t szCmdline[] = L"C:\\WINDOWS\\notepad.exe d:\\test.txt"; BOOL fRet=::CreateProcess(NULL, szCmdline, NULL, NULL, FALSE, NULL, NULL, NULL, &si, &pi);
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- 已标记为答案 psdty 2010年2月5日 5:42
-
Nancy Shao 非常感谢 还是你说到点子上了 呵呵