https://stackoverflow.com/questions/11339186/createprocess-fails-with-an-access-violation

My aim is to execute an external executable in my program. First, I used system() function, but I don't want the console to be seen to the user. So, I searched a bit, and found CreateProcess() function. However, when I try to pass a parameter to it, I don't know why, it fails. I took this code from MSDN, and changed a bit:

#include <windows.h>
#include <stdio.h>
#include <tchar.h> void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi; ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
/*
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
*/
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
L"c:\\users\\e\\desktop\\mspaint.exe", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
} // Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}

However, this code crated access violation somehow. Can I execute mspaint without showing user the console?

Thank you very much.

asked Jul 5 '12 at 6:54
John Doe

3001022
  • 2
    For one thing, CreateProcess requires that its second parameter (if provided) be a non-const string. I'm not sure that this is a problem in practice, but I wanted to mention it for completeness. – reuben Jul 5 '12 at 6:56
  •  
    ...and otherwise, where the is AV happening? Do you have a call stack? – reuben Jul 5 '12 at 6:57
  •  
    @reuben Uhm... I'm not quite sure, but I guess this is the output of call stack:kernel32.dll!76da70ac() [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll] > msvcr100d.dll!_nh_malloc_dbg(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine) Line 302 + 0x1d bytes C++ – John Doe Jul 5 '12 at 7:03

3 Answers

Try this, it should work.

TCHAR lpszClientPath[]= TEXT("c:\\users\\e\\desktop\\mspaint.exe");
if(!CreateProcess(NULL, lpszClientPath, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS|CREATE_NEW_CONSOLE|CREATE_UNICODE_ENVIRONMENT,NULL, NULL, &si, &pi))
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return;
}
 

CreateProcess Access violation(越界访问)的更多相关文章

  1. VS2010程序崩溃,报错Unhandled exception at **.exe:0xC0000005: Access violation reading location 0x000000008899.

    最近被派到另外一个组支援,从而从Linux下开发暂转到Windows下开发,个人觉得Windows自己搞的一套并不那么完美,坑多. 网文可能出现的原因: 未处理的异常: 0xC0000005: 读取位 ...

  2. 如何捕获access violation异常

    文章目录 access violation的由来 access violation的实例 Win32 exception SEH异常与C++标准异常 捕获方法 1.access violation的由 ...

  3. Access Violation at address 00000000.Read of address 00000000 解决办法

    是数组越标或没有初始化某个对象之类的问题,搂住细细检查一下代码, 使用指针前未做检查,而这个指针未初始化. 可能是new后没有delete,这样出现溢出的可能性比较大     检查代码或者跟踪试试 使 ...

  4. Navicat 提示 Access violation at address ***(如004ECCF4) in module ‘navicat.exe’. Read of address ***(如00000048)

    Navicat 提示 Access violation at address ***(如004ECCF4) in module ‘navicat.exe’. Read of address ***(如 ...

  5. 解决 “access violation at address xxxxxxxxx”错误

    在进行磁盘整理的时候,打开Foxmail的时候出现了“access violation at address32383137”错误 和“access violation at address00000 ...

  6. PHP has encountered an Access Violation at 01F4A622解决方法

    php搭建的网站出现以下问题的解决方法分享: Z-blog,DedeCMS,Dsicuz!,PhpWind,PhpCMS,帝国CMS等都有可能出现php访问冲突问题. 今天访问网站发现出现了一个错误& ...

  7. cout显示Mat类对象报错Access Violation

    AV(Access Violation)错误:非法访问. image_match.exe 中的 0x0000002a 处有未经处理的异常: 0xC0000005: Access violation 程 ...

  8. 使用scanf_s报错:0xC0000005: Access violation writing location 0x00000000

    在vs2010中写了一行scanf("%s",name); 调式时 提示warning , 提示修改为scanf()使用可能会存在不安全,建议使用scanf_s() 但是我修改成s ...

  9. Access Violation分成两大类:运行期和设计期(很全的解释)

    用Delphi开发程序时,我们可以把遇到的Access Violation分成两大类:运行期和设计期. 一.设计期的Access Violation 1.硬件原因  在启动或关闭Delphi IDE以 ...

随机推荐

  1. 链表中获取倒数第K个结点

    /* * 链表中查找倒数第K个结点.cpp * * Created on: 2018年5月1日 * Author: soyo */ #include<iostream> using nam ...

  2. nable to load bean: type:com.opensymphony.xwork2.util.ValueStackFactory

    严重: Exception starting filter struts2 Unable to load bean: type:com.opensymphony.xwork2.util.ValueSt ...

  3. ccflow_002.表单引擎与流程引擎的关系

    本节主要内容 表单.数据.流程引擎的关系 图形展示三者之间的关系 流程编号和节点编号命名规则 这里的206就是这个流程的编号 2601:01就是当前节点的编号 流程编号206转换为int类型之后加上0 ...

  4. 设置socket IP_TOS选项 (转载)

    转自:http://zhangjunxin520.blog.163.com/blog/static/305037032011721102857609/ 在IP头中,有一Type-of-Service字 ...

  5. 在Emacs下用C/C++编程(转载)

    转自:http://www.caole.net/diary/emacs_write_cpp.html Table of Contents 版权说明和参考文献 参考文献: 版权说明: 序 基本流程 基本 ...

  6. clipse maven 项目 出现红色叹号 解决方法

    感叹号代表jar包不全,看你是maven工程,pom.xml报错,应该是jar包路径不对.打开pom.xml文件,查看报错位置和报错信息. 缺失的jar包的配置去http://search.maven ...

  7. 洛谷 - P2261 - 余数求和

    https://www.luogu.org/problemnew/show/P2261 看了一下题解,取模运算可以换成减法来做. $a\%b=a-b*\lfloor\frac{a}{b}\rfloor ...

  8. 洛谷 - P1217 - 回文质数 - 枚举

    https://www.luogu.org/problemnew/show/P1217 考虑暴力生成所有的回文数然后再判断是不是质数.注意个位的选择实际上只有4种.所以是 $4*10^3*10^3=4 ...

  9. C++章节练习题

    笔试宝典:http://www.bishibaodian.com/writtenCircle/lightquestionlist http://www.bishibaodian.com/written ...

  10. 2016 Multi-University Training Contest 1 GCD【RMQ+二分】

    因为那时候没怎么补所以就分到了未搞分组里!!!然后因为标题如此之屌吧= =点击量很高,然后写的是无思路,23333,估计看题人真的是觉得博主就是个撒缺.废话不多说了,补题... update////2 ...