Показать сообщение отдельно
  #10  
Старый 07.04.2009, 17:02
8shar 8shar вне форума
Прохожий
 
Регистрация: 05.04.2009
Сообщения: 6
Репутация: 10
По умолчанию

Цитата:
Сообщение от Karsh
Можно использовать AttachConsole, но эта функция есть только в Windows XP и выше.
Вот готовая программа на WinAPI, котороя ловит окно по его названию и создает текстовый файл с содержимым этого окна.
Код:
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
program uAttachConsole;
 
uses                  
  Windows;
 
const
   _WIN32_WINNT = $0501;
 
type
   TAttachConsole = function (dwProcessId: DWORD): LongBOOL stdcall;
 
var
  AttachConsole: TAttachConsole;
  mProcessID, Wnd, Hcwnd, chRead: Cardinal;
  BufInfo: _CONSOLE_SCREEN_BUFFER_INFO;
  lpCh: PChar;
  Coord: _COORD;
  i: Integer;
 
function FileExists(const FileName: string): Boolean;
var
  FindData: TWin32FindData;
  F: THandle;
begin
  F:= FindFirstFile(PChar(FileName), FindData);
  Result:= (F <> INVALID_HANDLE_VALUE);
  if Result then FindClose(F);
end;
 
function ChangeFileExt(const FName, newExt: string): string;
var
  i, x, e: integer;
begin
  e:= Length(FName); x:= e;
  for i:= e downto 1 do
    if FName[i] = '.' then
    begin
      x:= i - 1;
      break;
    end;
  Result:= Copy(FName, 1, x) + newExt;
end;
 
function OpenFile(var fLog: TextFile; const LogFileName: String;
   const IsErase: Boolean = False): Boolean;
{var
   TmpStr: String; }
begin
{$I-}
   AssignFile(fLog, LogFileName);
   Result := IOResult = 0;
   if Result then begin
      if IsErase or (not FileExists (LogFileName)) then
         Rewrite(fLog)
      else
         Append(fLog);
      Result := IOResult = 0;
   end;
{$I+}
end;
 
function WriteLnStr(var fLog: TextFile; const fStr: string): boolean;
begin
{$I-}
   Writeln (fLog, fStr);
   Result := IOResult = 0;
   if Result then begin
      Flush (fLog);
      Result := IOResult = 0;
   end;
{$I+}
end;
 
procedure CloseFile(var fLog: TextFile);
begin
{$I-}
  Flush(fLog);
  System.CloseFile(fLog);
{$I+}
end;
 
 
function StrFile(const fStr, fFileName: String;
   const IsErase: Boolean = False): Boolean;
var
   fLog: TextFile;
begin
   Result := OpenFile(fLog, fFileName, IsErase);
   if Result then begin
      try
         Result := WriteLnStr(fLog, fStr);
      finally
         CloseFile(fLog);
      end;
   end;
end;
 
begin
  @AttachConsole:= GetProcAddress(GetModuleHandle('kernel32.dll'), 'AttachConsole');
 
  if @AttachConsole = nil then
  begin
    MessageBox(0, 'Программа работает только под Windows XP и выше!', 'Error', 16);
    Halt(1);
  end;
 
//  Wnd:= FindWindow(nil, 'Командная строка');
  Wnd:= FindWindow(nil, 'C:\WINDOWS\system32\cmd.exe');
 
  GetWindowThreadProcessId(Wnd, @mProcessID);
 
  if AttachConsole(mProcessID) then begin
    Hcwnd:= GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(Hcwnd, BufInfo);
    GetMem(lpCh, BufInfo.dwMaximumWindowSize.X+1);
    try
      for i:=0 to BufInfo.dwSize.Y-1 do begin
         Coord.X := 0;
         Coord.Y := i;
         ReadConsoleOutputCharacter(Hcwnd, lpCh, BufInfo.dwMaximumWindowSize.X, Coord, chRead);
         StrFile(lpCh, ChangeFileExt(ParamStr(0), '.log'), False);
      end;
    finally
      FreeMem(lpCh, BufInfo.dwMaximumWindowSize.X+1);
    end;
  end;
  Halt(0);
end.

Да!!!
Лед тронулся:
Код:
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
procedure TForm1.Button9Click(Sender: TObject);
type
   TAttachConsole = function (dwProcessId: DWORD): LongBOOL stdcall;
var
  AttachConsole: TAttachConsole;
  mProcessID, Hcwnd, chRead: Cardinal;
  BufInfo: _CONSOLE_SCREEN_BUFFER_INFO;
  lpCh : PChar;
  Coord: _COORD;
  i: Integer;
 
  Phwnd:HWND;
  ii:bool;
begin
  Phwnd:=FindWindow(nil,'C:\WINDOWS\system32\cmd.exe');
 
  @AttachConsole := GetProcAddress(GetModuleHandle('kernel32.dll'), 'AttachConsole');
  GetWindowThreadProcessId(Phwnd,@mProcessID);
 
  if AttachConsole(mProcessID) then begin
    Hcwnd:=GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo (Hcwnd, BufInfo);
    GetMem(lpCh, BufInfo.dwMaximumWindowSize.Y*BufInfo.dwMaximumWindowSize.X);
    Coord.X:=0;
    Coord.Y:=0;
   ii := ReadConsoleOutputCharacter(Hcwnd,lpCh,BufInfo.dwMaximumWindowSize.X,Coord,chRead);
 
    If ii then Memo1.Lines.Add('yes'); //успешон забрали с консоли
    If not ii then Memo1.Lines.Add('no'); //не забрали с консоли
     Memo1.Lines.Add(lpCh);  //выводим то что забрали
 
        Memo1.Lines.Add('Phwnd '+IntToStr(Phwnd));
        Memo1.Lines.Add('Hcwnd '+IntToStr(Hcwnd));
  end else Memo1.Lines.Add('Nea');
 
end;
Ответить с цитированием