-
Notifications
You must be signed in to change notification settings - Fork 3
/
SqlPPConsoleUnit.pas
89 lines (75 loc) · 2.26 KB
/
SqlPPConsoleUnit.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
unit SqlPPConsoleUnit;
interface
uses
Winapi.Windows, System.SysUtils, System.Classes,
System.StrUtils, System.Variants, ConstUnit;
type
TSqlPPConsole = class
private
FConsoleFile: TPathName;
function TempPath: string;
public
function Run(Command: TCommand; Path: TPathName): boolean;
property ConsoleFile: TPathName read FConsoleFile;
end;
implementation
{ TSqlPPConsole }
function TSqlPPConsole.Run(Command: TCommand; Path: TPathName): boolean;
var
i: integer;
ATempPath, CurDir: string;
stdOut: THandle;
startUpInfo: TStartUpInfo;
ProcInfo: TProcessInformation;
SecAtrtrs: TSecurityAttributes;
lpTempFileName: array[0..MAX_PATH] of char;
begin
with SecAtrtrs do
begin
nLength := SizeOf(TSecurityAttributes);
lpSecurityDescriptor := nil;
bInheritHandle := true;
end;
FConsoleFile := '';
ATempPath := TempPath;
GetTempFileName(PChar(ATempPath), '~lpp', 0, lpTempFileName);
CurDir := GetCurrentDir;
try
stdOut := CreateFile(lpTempFileName, GENERIC_WRITE, 0, @SecAtrtrs,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
FillChar(startUpInfo, SizeOf(TStartUpInfo), 0);
startUpInfo.cb := SizeOf(TStartUpInfo);
startUpInfo.hStdOutput := stdOut;
startUpInfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
startUpInfo.wShowWindow := SW_HIDE;
if Path <> '' then ChDir(Path);
Result := CreateProcess(nil, PChar(Command),nil, nil, true,
NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcInfo);
if Result then
begin
try
WaitForInputIdle(ProcInfo.hProcess, INFINITE);
WaitForSingleObject(ProcInfo.hProcess, INFINITE);
finally
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
CloseHandle(stdOut);
end;
i := StrLen(lpTempFileName);
SetLength(FConsoleFile,i);
StrCopy(PChar(FConsoleFile),lpTempFileName);
end;
finally
ChDir(CurDir);
end;
end;
function TSqlPPConsole.TempPath: string;
var
i: integer;
begin
SetLength(Result, MAX_PATH);
i := GetTempPath(Length(Result), PChar(Result));
SetLength(Result, i);
IncludeTrailingPathDelimiter(Result);
end;
end.