日记: CreateProcess D7 to XE , Access Violation kernel32.dll

CreateProcess D7 ok , 移植到XE出现下面错误:(OS: Win7 x64)

Access Violation at address xxx in module 'kernel32.dll'. Write of address xxx

解决:

XE默认WideChar,32位kernel32.dll 似乎是 AnsiChar (不确定,不去研究了)

XE更改为强制 AnsiChar 。

XE版本:

function exec_cmd_line(const sCmd: AnsiString; var nExitCode: Cardinal; nDefWaitTime: Cardinal = 10 * 1000): string;

var
  si: TStartupInfoA;
  pi: TProcessInformation;

begin

  FillChar(si, SizeOf(si), #0);
  FillChar(pi, SizeOf(pi), #0);

  si.cb := SizeOf(si);

  if Windows.CreateProcessA(
    nil, // LPCTSTR lpApplicationName, // pointer to name of executable module
    PAnsiChar(sCmd), //LPTSTR lpCommandLine,    // pointer to command line string
    nil, //LPSECURITY_ATTRIBUTES lpProcessAttributes,    // pointer to process security attributes
    nil, //LPSECURITY_ATTRIBUTES lpThreadAttributes,    // pointer to thread security attributes
    false, //BOOL bInheritHandles,    // handle inheritance flag
    0, //DWORD dwCreationFlags,    // creation flags
    nil, //LPVOID lpEnvironment,    // pointer to new environment block
    nil, //LPCTSTR lpCurrentDirectory,    // pointer to current directory name
    si, //LPSTARTUPINFO lpStartupInfo,    // pointer to STARTUPINFO
    pi //LPPROCESS_INFORMATION lpProcessInformation     // pointer to PROCESS_INFORMATION
    ) then begin

    if Windows.WaitForInputIdle(pi.hProcess, INFINITE) <> 0 then begin

    end;

    if Windows.WaitForSingleObject(pi.hProcess, nDefWaitTime) = Windows.WAIT_TIMEOUT then
      Windows.TerminateProcess(pi.hProcess, Windows.WAIT_TIMEOUT);

    Windows.GetExitCodeProcess(pi.hProcess, nExitCode);

  end
  else begin
    nExitCode := GetLastError;
  end;

  //nExitCode := nExitCode mod 256;

end;

D7版本:

function exec_cmd_line(const sCmd: string; var nExitCode: Cardinal; nDefWaitTime: Cardinal = 10 * 1000): string;
var
  si: TStartupInfo;
  pi: TProcessInformation;

begin

  FillChar(si, SizeOf(si), #0);
  FillChar(pi, SizeOf(pi), #0);

  si.cb := SizeOf(si);

  if Windows.CreateProcess(
    nil, // LPCTSTR lpApplicationName, // pointer to name of executable module
    PChar(sCmd), //LPTSTR lpCommandLine,    // pointer to command line string
    nil, //LPSECURITY_ATTRIBUTES lpProcessAttributes,    // pointer to process security attributes
    nil, //LPSECURITY_ATTRIBUTES lpThreadAttributes,    // pointer to thread security attributes
    false, //BOOL bInheritHandles,    // handle inheritance flag
    0, //DWORD dwCreationFlags,    // creation flags
    nil, //LPVOID lpEnvironment,    // pointer to new environment block
    nil, //LPCTSTR lpCurrentDirectory,    // pointer to current directory name
    si, //LPSTARTUPINFO lpStartupInfo,    // pointer to STARTUPINFO
    pi //LPPROCESS_INFORMATION lpProcessInformation     // pointer to PROCESS_INFORMATION
    ) then begin

    if Windows.WaitForInputIdle(pi.hProcess, INFINITE) <> 0 then begin

    end;

    if Windows.WaitForSingleObject(pi.hProcess, nDefWaitTime) = Windows.WAIT_TIMEOUT then
      Windows.TerminateProcess(pi.hProcess, Windows.WAIT_TIMEOUT);

    Windows.GetExitCodeProcess(pi.hProcess, nExitCode);

  end
  else begin
    nExitCode := GetLastError;
  end;

  //nExitCode := nExitCode mod 256;

end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值