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:

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4.  
  5. void _tmain( int argc, TCHAR *argv[] )
  6. {
  7. STARTUPINFO si;
  8. PROCESS_INFORMATION pi;
  9.  
  10. ZeroMemory( &si, sizeof(si) );
  11. si.cb = sizeof(si);
  12. ZeroMemory( &pi, sizeof(pi) );
  13. /*
  14. if( argc != 2 )
  15. {
  16. printf("Usage: %s [cmdline]\n", argv[0]);
  17. return;
  18. }
  19. */
  20. // Start the child process.
  21. if( !CreateProcess( NULL, // No module name (use command line)
  22. L"c:\\users\\e\\desktop\\mspaint.exe", // Command line
  23. NULL, // Process handle not inheritable
  24. NULL, // Thread handle not inheritable
  25. FALSE, // Set handle inheritance to FALSE
  26. , // No creation flags
  27. NULL, // Use parent's environment block
  28. NULL, // Use parent's starting directory
  29. &si, // Pointer to STARTUPINFO structure
  30. &pi ) // Pointer to PROCESS_INFORMATION structure
  31. )
  32. {
  33. printf( "CreateProcess failed (%d).\n", GetLastError() );
  34. return;
  35. }
  36.  
  37. // Wait until child process exits.
  38. WaitForSingleObject( pi.hProcess, INFINITE );
  39.  
  40. // Close process and thread handles.
  41. CloseHandle( pi.hProcess );
  42. CloseHandle( pi.hThread );
  43. }

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.

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

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. javascript 布尔类型

    true和false 表达式为false的情况 1 false 2 NaN 3 0 4 字符串的空 " " 5 null 6 undefined

  2. 关于 .dyib 文件

    .dylib 意味着这是一个动态链接库. libz.dylib 是提供zip压缩.解压缩的库. 库的接口请 #import "zlib.h"

  3. UVaLive 6581 && Gym 100299B What does the fox say? (模拟+STL)

    题意:给定一些动物的叫声,然后再定某些动物的叫声,让你去除这些叫声后得到的叫声. 析:先存储所有的叫声,然后用map来记录其他的叫声,在输出时再判定一下就好. 代码如下: #pragma commen ...

  4. Unity网格合并_材质合并[转]

    http://blog.csdn.net/chenggong2dm/article/details/41699029

  5. Swift3.0 控制流

    常用的一些判断方式 //for in let numberList = [, , , , ] //获取第一个元素 !拆包 print(numberList.first!)//1 //获取最后一个元素 ...

  6. 瞎比比系列---1st

    A - 项目管理HDU4858 /* 题意: 这个项目有n个节点, 两个节点间可能有多条边,不过一条边的两端必然是不同的节点. 0的时候:接下来两个数u v表示给项目u的能量值加上v: 1的时候: 这 ...

  7. 前端代码规范(转载 http://codeguide.bootcss.com/)

    http://codeguide.bootcss.com/ HTML 语法 HTML5 doctype 语言属性(Language attribute) 字符编码 IE 兼容模式 引入 CSS 和 J ...

  8. iOS 加载Viewcontroller的几种方法

    // 一.根据StoryboardID(需要在Storyboard设置),通过ViewController所在的Storyboard来加载: UIStoryboard *storyboard = [U ...

  9. python3_linux安装

    python安装包地址 https://www.python.org/ftp/python/ 如果没有c complie,就安装: yum -y install gcc gcc-c++ 在编译安装之前 ...

  10. KMP算法笔记(云笔记图片版)