⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unitcmd.pas

📁 Windows CMD Emulator is Based Upon Creating a Pipe to Read and Write Data to, it Opens a CMD Window
💻 PAS
字号:
{$A+,B-,C-,D-,E-,F-,G+,H+,I-,J-,K-,L-,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y-,Z1}
{$MINSTACKSIZE $00004000}
{$MAXSTACKSIZE $00100000}
{$IMAGEBASE $00400000}
{$APPTYPE GUI}

unit UnitCMD;

interface

uses
  Windows,
  Messages,
  StdCtrls;

type
 TShellThread = Packed Record
  OutputMemo:TMemo;
 end;
 PShellThread = ^TShellThread;

 //Function
 procedure ShellThread(P : PShellThread); stdcall;

var
 ShellThreadID : DWord;

implementation

procedure ShellThread(P : PShellThread); stdcall;
var
 StartupInfo: TStartupInfo;
 ProcessInformation: TProcessInformation;
 Secu:PSecurityAttributes;
 hPipeRead1, hPipeWrite1, hPipeRead2, hPipeWrite2, BytesRead, exitcode: dword;
 ComSpec:array [0..MAX_PATH] of char;
 Buf:array [0..1024] of char;
 Msg: TMsg;
 TempStr : String;
begin
 GetMem(Secu,sizeof(SECURITY_ATTRIBUTES));
 Secu.nLength := SizeOf(SECURITY_ATTRIBUTES);
 Secu.bInheritHandle := True;
 Secu.lpSecurityDescriptor := nil;
 CreatePipe(hPipeRead1, hPipeWrite1, Secu, 0);
 CreatePipe(hPipeRead2, hPipeWrite2, Secu, 0);
 //GetEnvironmentVariable Gets CMD.EXE Path C:\windows\system32\cmd.exe
 GetEnvironmentVariable('COMSPEC', ComSpec, sizeof(ComSpec));
 ZeroMemory(@StartupInfo, SizeOf(StartupInfo));
 StartupInfo.cb := SizeOf(StartupInfo);
 StartupInfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
 StartupInfo.wShowWindow := SW_HIDE;
 StartupInfo.hStdInput := hPipeRead2;
 StartupInfo.hStdOutput := hPipeWrite1;
 StartupInfo.hStdError := hPipeWrite1;
 if CreateProcess(nil,ComSpec,Secu,Secu,TRUE,0,nil,nil,StartupInfo,ProcessInformation) = true
 then while true do
 begin
  GetExitCodeProcess(ProcessInformation.hProcess,{var }exitcode);
  if exitcode <> STILL_ACTIVE then break;
  TempStr := '';
  PeekNamedPipe(hPipeRead1,nil,0,nil, @BytesRead,nil);
  while BytesRead > 0 do
  begin
   if ReadFile(hPipeRead1, Buf, sizeof(Buf), BytesRead,nil) then
   begin
    TempStr := TempStr + Copy(Buf, 1, bytesRead);
   end else break;
   PeekNamedPipe(hPipeRead1,nil,0,nil,@BytesRead,nil);
  end;

  if Length(TempStr) > 0 then
  begin
   P.OutputMemo.Text := P.OutputMemo.Text + TempStr;
   SendMessage(P.OutputMemo.Handle, EM_LINESCROLL, 0, Length(P.OutputMemo.Text));
  end;
  GetMessage(msg, 0, 0, 0);
  if (Msg.message = WM_ACTIVATE) then
  begin
   WriteFile(hPipeWrite2, pchar(msg.lParam)^, msg.wParam, BytesRead, nil);
   WriteFile(hPipeWrite2, #13#10, 2, BytesRead,nil);
  end;
 end; //main loop
 TerminateProcess(ProcessInformation.hProcess,0);
 CloseHandle(ProcessInformation.hProcess);
 CloseHandle(ProcessInformation.hThread);
 FreeMem(Secu);
 ShellThreadID := 0;
end;

procedure ShellPostMessageTimer;
begin
 if ShellThreadID <> 0 then PostThreadMessage(ShellThreadID, 0, 0, 0);
end;

begin
 ShellThreadID := 0;
 SetTimer(0, 0, 500, @ShellPostMessageTimer);
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -