Search code examples
c++visual-studio-2010winapiconsole-applicationcreateprocess

CreateProcess() function cannot run. Error The memory could not be written


I'm using Windows 8 x64 Enterprise, VS2010.

I've some problem on CreateProcess().

I've created a Win32 Console project to execute _backround_manipulator.exe, my application.

Implementation here.

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

DWORD RunManipulator(TCHAR* tszProcessPath);

int _tmain(int argc, _TCHAR* argv[])
{
    _tprintf(_T("---Manipulator will start...---\n"));
    if(0x08 == RunManipulator(_T("_background_manipulator.exe")))
        _tprintf(_T("---Manipulator Started.---\n"));
    else
        _tprintf(_T("---Manipulator cannot run.---\n"));
    return 0;
}

DWORD RunManipulator(TCHAR* tszProcessPath)
{
    STARTUPINFO _v_startupinfo;
    PROCESS_INFORMATION _v_processinfo;
    ZeroMemory(&_v_startupinfo, sizeof(STARTUPINFO));
    ZeroMemory(&_v_processinfo, sizeof(PROCESS_INFORMATION));

    _v_startupinfo.cb = sizeof(STARTUPINFO);

    if (!CreateProcess(NULL, tszProcessPath, NULL, NULL, FALSE, 0, NULL, NULL, &_v_startupinfo, &_v_processinfo));
    {
        return 0x12;
    }

    return 0x08;
}

But cannot pass CreateProcess(NULL, tszProcesPath, /*...*/) function on debug mode.

Error like this;

enter image description here

What's wrong on my code? Is it because I created the Console Project?


Solution

  • if look for definition of CreateProcess

    BOOL WINAPI CreateProcess(
      _In_opt_    LPCTSTR               lpApplicationName,
      _Inout_opt_ LPTSTR                lpCommandLine,
      ...
    

    we can note that lpCommandLine defined as In-out parameter and defined not as const pointer ( compare with lpApplicationName which is const pointer LPCTSTR)

    and :

    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.

    but you exactly pass literal string _T("_background_manipulator.exe") as lpCommandLine. and got excepted result - memory could not be written