H
hsn1982
Unregistered / Unconfirmed
GUEST, unregistred user!
功能: 打开cmd.exe,定位在文件目录册,并写入文件名称;
-------------------------------------------------------------------------------}
procedure OpenWithCmd(const FileName: string);
var
fStdInRead,fStdInWrite: THandle;
fStdOutRead, fStdOutWrite: THandle;
fStdErrRead, fStdErrWrite: THandle;
osvi: OSVERSIONINFO ;
processinfo: PROCESS_INFORMATION;
si: STARTUPINFO;
sSysDir: array [0..MAX_PATH] of char;
nLenWrite,nWriter: DWORD;
bRetCode: LongBool;
fSA: SECURITY_ATTRIBUTES;
begin
FillChar(fSA, SizeOf(SECURITY_ATTRIBUTES), 0);
fSA.nLength := SizeOf(SECURITY_ATTRIBUTES);
fSA.lpSecurityDescriptor := nil;
fSA.bInheritHandle := True;
if not CreatePipe(fStdInRead, fStdInWrite, @fSA, 0) then Exit;
// CreatePipe(fStdOutRead, fStdOutWrite, @fSA, 0);
if not CreatePipe(fStdErrRead, fStdErrWrite, @fSA, 0) then Exit;
// if not CreateSimplePipe(fStdInRead, fStdInWrite) then
// Exit;
// if not CreateSimplePipe(fStdOutRead, fStdOutWrite) then
// Exit;
try
Assert(False,'成功创建pipe!');
//得到当前进程的进程启动信息
// GetStartupInfo(si);
//对其中某些属性进行调整,并继承那些不做调整的属性
FillChar(si, SizeOf(STARTUPINFO), 0);
si.cb:= SizeOf(STARTUPINFO);
si.dwFlags:= STARTF_USESTDHANDLES;// or STARTF_USESHOWWINDOW);
si.hStdInput := fStdInRead;
// si.hStdOutput := fStdOutWrite;
si.hStdError := fStdErrWrite;
si.wShowWindow:= SW_SHOW;
//在使用osvi得到操作系统信息时,要注意一定要先初始化这个大小变量
osvi.dwOSVersionInfoSize:= SizeOf(OSVERSIONINFO);
//得到操作系统版本信息,放入结构变量osvi中。
GetVersionEx(osvi);
//得到Cmd.exe所在的系统目录的全路径名
FillChar(sSysDir,MAX_PATH,#0);
GetSystemDirectory(sSysDir,MAX_PATH);
StrCat( sSysDir,'/Cmd.exe');
//如果dwPlatformId值为2,说明操作系统是NT以后的版本的操作系统
if(osvi.dwPlatformId = 2) then
bRetCode:= CreateProcess(nil, sSysDir, nil, nil, TRUE, NORMAL_PRIORITY_CLASS,nil,
PChar(ExtractFilePath(FileName)),si, processinfo)
else
bRetCode:= CreateProcess(nil,'command.com',nil,nil,true,NORMAL_PRIORITY_CLASS,nil,
PChar(ExtractFilePath(FileName)),si,processinfo);
Assert(bRetCode,PChar('Fail CreateCmdProcess.'+ SysErrorMessage(GetLastError)));
try
nLenWrite:= StrLen(sSysDir);
bRetCode:= WriteFile(fStdInWrite,sSysDir,nLenWrite,nWriter,nil);
Assert(bRetCode,SysErrorMessage(GetLastError));
except
Assert(False,SysErrorMessage(GetLastError));
end;
finally
if fStdInWrite<>0 then
CloseHandle(fStdInWrite);
// CloseHandle(fStdOutRead);
if fStdErrRead<>0 then
CloseHandle(fStdErrRead);
end;
end;
不起作用,写不进cmd窗口中。我弄了一天了,谁能给个解决方法,谢谢了
-------------------------------------------------------------------------------}
procedure OpenWithCmd(const FileName: string);
var
fStdInRead,fStdInWrite: THandle;
fStdOutRead, fStdOutWrite: THandle;
fStdErrRead, fStdErrWrite: THandle;
osvi: OSVERSIONINFO ;
processinfo: PROCESS_INFORMATION;
si: STARTUPINFO;
sSysDir: array [0..MAX_PATH] of char;
nLenWrite,nWriter: DWORD;
bRetCode: LongBool;
fSA: SECURITY_ATTRIBUTES;
begin
FillChar(fSA, SizeOf(SECURITY_ATTRIBUTES), 0);
fSA.nLength := SizeOf(SECURITY_ATTRIBUTES);
fSA.lpSecurityDescriptor := nil;
fSA.bInheritHandle := True;
if not CreatePipe(fStdInRead, fStdInWrite, @fSA, 0) then Exit;
// CreatePipe(fStdOutRead, fStdOutWrite, @fSA, 0);
if not CreatePipe(fStdErrRead, fStdErrWrite, @fSA, 0) then Exit;
// if not CreateSimplePipe(fStdInRead, fStdInWrite) then
// Exit;
// if not CreateSimplePipe(fStdOutRead, fStdOutWrite) then
// Exit;
try
Assert(False,'成功创建pipe!');
//得到当前进程的进程启动信息
// GetStartupInfo(si);
//对其中某些属性进行调整,并继承那些不做调整的属性
FillChar(si, SizeOf(STARTUPINFO), 0);
si.cb:= SizeOf(STARTUPINFO);
si.dwFlags:= STARTF_USESTDHANDLES;// or STARTF_USESHOWWINDOW);
si.hStdInput := fStdInRead;
// si.hStdOutput := fStdOutWrite;
si.hStdError := fStdErrWrite;
si.wShowWindow:= SW_SHOW;
//在使用osvi得到操作系统信息时,要注意一定要先初始化这个大小变量
osvi.dwOSVersionInfoSize:= SizeOf(OSVERSIONINFO);
//得到操作系统版本信息,放入结构变量osvi中。
GetVersionEx(osvi);
//得到Cmd.exe所在的系统目录的全路径名
FillChar(sSysDir,MAX_PATH,#0);
GetSystemDirectory(sSysDir,MAX_PATH);
StrCat( sSysDir,'/Cmd.exe');
//如果dwPlatformId值为2,说明操作系统是NT以后的版本的操作系统
if(osvi.dwPlatformId = 2) then
bRetCode:= CreateProcess(nil, sSysDir, nil, nil, TRUE, NORMAL_PRIORITY_CLASS,nil,
PChar(ExtractFilePath(FileName)),si, processinfo)
else
bRetCode:= CreateProcess(nil,'command.com',nil,nil,true,NORMAL_PRIORITY_CLASS,nil,
PChar(ExtractFilePath(FileName)),si,processinfo);
Assert(bRetCode,PChar('Fail CreateCmdProcess.'+ SysErrorMessage(GetLastError)));
try
nLenWrite:= StrLen(sSysDir);
bRetCode:= WriteFile(fStdInWrite,sSysDir,nLenWrite,nWriter,nil);
Assert(bRetCode,SysErrorMessage(GetLastError));
except
Assert(False,SysErrorMessage(GetLastError));
end;
finally
if fStdInWrite<>0 then
CloseHandle(fStdInWrite);
// CloseHandle(fStdOutRead);
if fStdErrRead<>0 then
CloseHandle(fStdErrRead);
end;
end;
不起作用,写不进cmd窗口中。我弄了一天了,谁能给个解决方法,谢谢了