这段代码编译通过,却弹出窗口问题.

appleshao 2009-03-08 01:42:08
#include <windows.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;

si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = TRUE;
BOOL bRet = ::CreateProcess (
NULL,
L"cmd.exe", //写成TEXT("cmd")也一样,弹处一个gbox关闭窗口.
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);

if(bRet)
{
::CloseHandle (pi.hThread);
::CloseHandle (pi.hProcess);

printf(" 新进程的进程ID号:%d \n", pi.dwProcessId);
printf(" 新进程的主线程ID号:%d \n", pi.dwThreadId);
}
return 0;
}
...全文
98 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hityct1 2009-03-08
  • 打赏
  • 举报
回复
明白出错的原因了,关于第二个参数:
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.


第二个参数不能为常量字符串。


#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;

si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = TRUE;
TCHAR cmdline[] =L"c:\\windows\\system32\\cmd.exe";
BOOL bRet = ::CreateProcess (
NULL,
cmdline,
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);

int error = GetLastError();
if(bRet)
{
::CloseHandle (pi.hThread);
::CloseHandle (pi.hProcess);

printf(" 新进程的进程ID号:%d \n", pi.dwProcessId);
printf(" 新进程的主线程ID号:%d \n", pi.dwThreadId);
}
else
{
printf("error code:%d\n",error );
}
return 0;
}


hityct1 2009-03-08
  • 打赏
  • 举报
回复
楼主要干什么?要运行cmd.exe?如果是的第一个参数是L“cmd.exe”。我的是xp系统。


#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;

si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = TRUE;
BOOL bRet = ::CreateProcess (
L"c:\\windows\\system32\\cmd.exe",
NULL,
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);

int error = GetLastError();
if(bRet)
{
::CloseHandle (pi.hThread);
::CloseHandle (pi.hProcess);

printf(" 新进程的进程ID号:%d \n", pi.dwProcessId);
printf(" 新进程的主线程ID号:%d \n", pi.dwThreadId);
}
else
{
printf("error code:%d\n",error );
}
return 0;
}
appleshao 2009-03-08
  • 打赏
  • 举报
回复
有什么方法让CreateProcess接受L"cmd.exe"
appleshao 2009-03-08
  • 打赏
  • 举报
回复
ceshi.exe 中的 0x76b00969 处未处理的异常: 0xC0000005: 写入位置 0x00315786 时发生访问冲突
appleshao 2009-03-08
  • 打赏
  • 举报
回复
编译环境vs2008+vista

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧