【问题标题】:CreateProcess() function cannot run. Error The memory could not be written [duplicate]CreateProcess() 函数无法运行。错误无法写入内存[重复]
【发布时间】:2018-02-20 22:21:58
【问题描述】:

我使用的是 Windows 8 x64 Enterprise,VS2010。

我在CreateProcess() 上遇到了一些问题。

我创建了一个 Win32 控制台项目来执行 _backround_manipulator.exe,我的应用程序。

在这里实现。

#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;
}

但在debug模式下无法通过CreateProcess(NULL, tszProcesPath, /*...*/)函数。

错误像这样;

我的代码有什么问题? 是因为我创建了控制台项目吗?

【问题讨论】:

  • 您是否尝试过使用调试器一步一步地找出导致错误的行?
  • 我无法传递 CreateProcess 函数。
  • 1) if 后面有分号,我猜这不是我们想要的行为。 2)在失败报告ERROR_FILE_NOT_FOUND的情况下添加GetLastError。路径中有_background_manipulator.exe 吗?
  • 您的tszProcesPath 是只读的。这里不清楚什么?小错误
  • 像“_background_manipulator.exe”这样的字符串文字通常在只读部分放置.rdata

标签: c++ visual-studio-2010 winapi console-application createprocess


【解决方案1】:

如果寻找CreateProcess的定义

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

我们可以注意到,lpCommandLine 定义为 In-out 参数,而不是定义为 const 指针(比较 lpApplicationName 是 const 指针 LP CTSTR)

和:

这个函数的Unicode版本,CreateProcessW,可以修改 这个字符串的内容。因此,该参数不能是 指向只读内存的指针(例如 const 变量或 文字字符串)。如果此参数是一个常量字符串,该函数可能会导致访问冲突。

但您将 文字字符串 _T("_background_manipulator.exe") 准确地传递为 lpCommandLine。并得到异常结果 - 无法写入内存

【讨论】:

  • 谢谢。我刚刚使用另一个TCHAR 变量解决了这个问题。观点 ; _tcscpy(tszCommandLine, tszProcessPath);谢谢你的好意。
  • @RbMm 这是一个重复数百次的骗局,也许最好这样接近
猜你喜欢
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多