1

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?

piet.t
  • 11,718
  • 21
  • 43
  • 52
AleXelton
  • 767
  • 5
  • 27
  • Have you tried using the debugger step-by-step and find which line causes the error? – roalz Sep 12 '17 at 12:54
  • I cannot pass on CreateProcess function. – AleXelton Sep 12 '17 at 12:56
  • 1
    1) You have semicolon after `if`, which I guess is not the desired behavior. 2) Adding `GetLastError` in the case of failure reports `ERROR_FILE_NOT_FOUND`. Do you have `_background_manipulator.exe` in the path? – karastojko Sep 12 '17 at 13:03
  • Thank you, @karastojko. Read carefully my problem. 1) Have you set same evironment as me? I mean `ConsoleProject`. 2) Also same error will occur after change `_background_manipulator.exe` into `calc.exe`. – AleXelton Sep 12 '17 at 14:09
  • 1
    your `tszProcesPath` is read only. what here unclear ? trivial error – RbMm Sep 12 '17 at 14:37
  • @RbMm, thank you. I cannot find why `tszProcessPath` is read only. Do you know why this happens? I did not do anything to `tszProcessPath`. – AleXelton Sep 12 '17 at 14:43
  • @A.Godnov: Thanks for the point - usually I read the question and try the given code. It works when `calc.exe` is used (in the Console mode). – karastojko Sep 12 '17 at 14:43
  • 1
    the string literals like "_background_manipulator.exe" usual is in read only section placed .rdata – RbMm Sep 12 '17 at 14:45
  • Is the Readonly, Writeonly attributes related to CreateProcess? It looks like the Writeonly attribute does not matter. Do I just have to read and write? Please tell me more. – AleXelton Sep 12 '17 at 14:52
  • @karastojko, I'm really glad that you are running code well. But I really don't know why that error occurs. I am looking for a cause because I can't execute specified process. I think something is coming up now, from Mr. RbMm. – AleXelton Sep 12 '17 at 14:59
  • Why use TCHAR? Do you really support Windows 95? – David Heffernan Sep 12 '17 at 15:58

1 Answers1

2

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

RbMm
  • 31,280
  • 3
  • 35
  • 56