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;
日记: CreateProcess D7 to XE , Access Violation kernel32.dll
最新推荐文章于 2023-06-29 14:52:02 发布