mam*_*mcx 3 delphi delphi-2009
我在互联网上尝试了几个样本,但没有一个工作 - 脚本没有执行 - (也许是因为是Delphi 2009之前的unicode?).
我需要运行一些python脚本并将参数传递给它们,例如:
python "..\Plugins\RunPlugin.py" -a login -u Test -p test
Run Code Online (Sandbox Code Playgroud)
并将输出捕获到字符串并将错误捕获到其他字符串.
这就是我现在拥有的:
procedure RunDosInMemo(DosApp:String; var OutData: String);
var
SA: TSecurityAttributes;
SI: TStartupInfo;
PI: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WasOK: Boolean;
Buffer: array[0..255] of Char;
BytesRead: Cardinal;
WorkDir: string;
Handle: Boolean;
begin
OutData := '';
with SA do begin
nLength := SizeOf(SA);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SA, 0);
try
with SI do
begin
FillChar(SI, SizeOf(SI), 0);
cb := SizeOf(SI);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES or CREATE_UNICODE_ENVIRONMENT;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;
WorkDir := 'C:\';
Handle := CreateProcess(nil, PChar(DosApp),
nil, nil, True, 0, nil,
PChar(WorkDir), SI, PI);
CloseHandle(StdOutPipeWrite);
if Handle then
begin
try
repeat
WasOK := ReadFile(StdOutPipeRead, Buffer, 255, BytesRead, nil);
if BytesRead > 0 then
begin
Buffer[BytesRead] := #0;
OutData := OutData + String(Buffer);
end;
until not WasOK or (BytesRead = 0);
WaitForSingleObject(PI.hProcess, INFINITE);
finally
CloseHandle(PI.hThread);
CloseHandle(PI.hProcess);
end;
end else begin
raise Exception.Create('Failed to load python plugin');
end;
finally
CloseHandle(StdOutPipeRead);
end;
end;
Run Code Online (Sandbox Code Playgroud)
Create_Unicode_Environment是一个进程创建标志,用于dwCreationFlags参数CreateFile.它不是用于TStartupInfo记录的标志.如果你给它们不理解的标志值,API函数可能会失败,如果你给它们标志值的意思不是你期望的东西,它们可能会做一些奇怪的事情.
你声明一个256 Char秒的缓冲区; 回想一下,Char在Delphi 2009中是一个2字节的Unicode类型.然后调用ReadFile并告诉它缓冲区长度为255 个字节而不是实际值512.当文档说值是字节数时,请将其作为使用该SizeOf函数的提示.
因为ReadFile读取字节,所以将缓冲区数组声明为字节大小的元素数组是个好主意,例如AnsiChar.这样,当您设置时Buffer[BytesRead],您将不会包含实际读取的数据的两倍.
Unicode版本CreateProcess可以修改其命令行参数.您必须确保传递给该参数的字符串的引用计数为1.在调用UniqueString(DosApp)之前调用CreateProcess.
当API函数失败时,您当然想知道原因.不要只是做了一个理由.使用提供的功能,例如Win32Check和RaiseLastOSError.至少,GetLastError就像MSDN告诉你的那样.当更具体的异常类型可用时,不要抛出通用异常类型.
| 归档时间: |
|
| 查看次数: |
2304 次 |
| 最近记录: |