CreateProcess导致内存权限异常

使用UNICODE字符集的时候,MSDN有如此描述:

BOOL WINAPI CreateProcess(
  _In_opt_     LPCTSTR lpApplicationName,
  _Inout_opt_  LPTSTR lpCommandLine,
  _In_opt_     LPSECURITY_ATTRIBUTES lpProcessAttributes,
  _In_opt_     LPSECURITY_ATTRIBUTES lpThreadAttributes,
  _In_         BOOL bInheritHandles,
  _In_         DWORD dwCreationFlags,
  _In_opt_     LPVOID lpEnvironment,
  _In_opt_     LPCTSTR lpCurrentDirectory,
  _In_         LPSTARTUPINFO lpStartupInfo,
  _Out_        LPPROCESS_INFORMATION lpProcessInformation
);

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.

lpCommandLine要传入可修改的内存。

	wchar_t cmdline[] = L"/c ping localhost > d:\pingres.log";
	if (!CreateProcess(L"c:\\windows\\system32\\cmd.exe", cmdline
		,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) {
			MessageBox(L"Error on CreateProcess()");
			return;
	}

  

原文地址:https://www.cnblogs.com/horane/p/3090474.html