WinXP환경에서 델파이 2010을 사용하고 있습니다.
http://www.acrosoft.pe.kr/board/?mid=cg_tips&search_target=title&document_srl=24088
위 링크에 있는 소스그대로 사용했는데요.
결과가
펼쿇솷븠맆₫냅ꪳ될뢩붣뷊뿃⃀㩅䍜䑟噒륜엙죁뢭峩횼僚쏇>
이렇게 깨져서 보입니다.
제가 뭘 잘못한건가요?
procedure TForm1.RunDosCommand(Command : string; Output : TMemo);
var
hReadPipe : THandle;
hWritePipe : THandle;
SI : TStartUpInfo;
PI : TProcessInformation;
SA : TSecurityAttributes;
SD : TSecurityDescriptor;
BytesRead : DWORD;
Dest : array[0..1023] of char;
CmdLine : array[0..512] of char;
TmpList : TStringList;
S, Param : string;
Avail, ExitCode, wrResult : DWORD;
UsingWinNT : boolean;
begin { Dos Application }
UsingWinNT := true;
if UsingWinNT then
begin
InitializeSecurityDescriptor(@SD, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@SD, True, nil, False);
SA.nLength := SizeOf(SA);
SA.lpSecurityDescriptor := @SD;
SA.bInheritHandle := True;
CreatePipe(hReadPipe, hWritePipe, @SA, 1024);
end else
CreatePipe(hReadPipe, hWritePipe, nil, 1024);
try
Screen.Cursor := crHourglass; { STARTUPINFO를 설정한다. }
FillChar(SI, SizeOf(SI), 0);
SI.cb := SizeOf(TStartUpInfo);
SI.wShowWindow := SW_HIDE;
SI.dwFlags := STARTF_USESHOWWINDOW; { STARTF_USESTDHANDLES 를 빼면 PIPE로 입출력이 Redirect 되지 않는다. }
SI.dwFlags := SI.dwFlags or STARTF_USESTDHANDLES;
SI.hstdoutput := hWritePipe;
SI.hStdError := hWritePipe;
StrPCopy(CmdLine, Command); { Process를 생성한다. }
if CreateProcess(nil, CmdLine, nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil, SI, PI) then
begin
ExitCode := 0;
while ExitCode = 0 do
begin
wrResult := WaitForSingleObject(PI.hProcess, 500);
{ Pipe에 출력된 내용이 있는 지 조사한다. }
if PeekNamedPipe(hReadPipe, nil, 0, nil, @Avail, nil) then
begin
if Avail > 0 then
begin
{ Redirect된 화면 내용을 출력 윈도에 내보낸다. }
TmpList := TStringList.Create;
try
FillChar(Dest, SizeOf(Dest), 0);
// ReadFile(hReadPipe, Dest, Avail, BytesRead, nil);
// 원본에 버그 있음 버퍼 크기를 넘어가는 경우가 생김 : 수정
ReadFile(hReadPipe, Dest, sizeof(Dest) -1, BytesRead, nil);
TmpList.Text := StrPas(Dest);
Output.Lines.AddStrings(TmpList);
finally
TmpList.Free;
end;
end;
end; // end of peek
{ WAIT_TIMEOUT이 아니면 루프를 마친다. }
if wrResult <> WAIT_TIMEOUT then
ExitCode := 1;
end; // while
{ 실행이 끝났다.. }
GetExitCodeProcess(PI.hProcess, ExitCode);
CloseHandle(PI.hProcess);
CloseHandle(PI.hThread);
end; // if
finally
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
Screen.Cursor := crDefault;
end;
end;