Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
495 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Catch debug information of an applicationGo to Tommy Andersen's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
N/A
Skill Level:
Scoring:
Last Update:
05/06/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet OpenProcess, DebugActiveProcess, WaitForDebugEvent, DebugEvent, OUTPUT_DEBUG_STRING_EVENT, CREATE_THREAD_DEBUG_EVENT, ContinueDebugEvent, CloseHandle
Times Scored:
3
Visits:
3087
Uploader: Tommy Andersen
Company: EasyWare
Reference: N/A
 
Question/Problem/Abstract:
Ever wanted to read the debug information an application sends out?
Answer:



This little codesnippet will ask for the ProcessID (PID) of the application you want to catch debug information from. I did not have time to write some code for it right now, so in the meantime use the taskmanager and get the PID there. :)


Type
  TDebugThread = Class(TThread)
  Private
    { Private declarations }
  Protected
    { Protected declarations }
    Procedure Execute; override;
  Public
    { Public declarations }
    Constructor Create;
    Destructor Destroy; override;
  End;

  TForm1 = class(TForm)
    Memo1: TMemo;
    TreeView1: TTreeView;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    DebugThread : TDebugThread;
  public
    { Public declarations }
    Procedure BeginCompile(Sender: TObject);
  end;


var
  Form1: TForm1;

Implementation


Procedure TForm1.FormCreate(Sender: TObject);
Begin
   DebugThread := TDebugThread.Create;
End;

procedure TForm1.FormDestroy(Sender: TObject);
Begin
   DebugThread.Terminate;
End;

Procedure TDebugThread.Execute;
Var
  DebugEvent : _Debug_Event;
  ReadLen    : DWord;
  StrBuffer  : pChar;
  UniBuffer  : WideString;

  S : String;
  DbgHandle  : THandle;
  OpenHandle : THandle;

  // ProcessNode : TTreeNode;
  ThreadNode  : TTreeNode;

Begin
   S := InputBox('Process', 'Handle', '0');
   DbgHandle := StrToIntDef(S, 0);
   OpenHandle := OpenProcess(PROCESS_ALL_ACCESS, True, DbgHandle);

   IF (DebugActiveProcess(DbgHandle)) Then Form1.Caption := 'Debug ok'
      Else Form1.Caption := 'Debug failed';


   Form1.TreeView1.Items.Clear;
   ThreadNode := Form1.TreeView1.Items.AddChild(NIL, 'Threads');
   // ProcessNode := Form1.TreeView1.Items.AddChild(NIL, 'Processes');

   While (not Terminated) do
   Begin
      Sleep(0);

      IF (WaitForDebugEvent(DebugEvent, 100)) Then
      Begin
         IF (not Application.Terminated) Then
         Begin
            Case DebugEvent.dwDebugEventCode of
              OUTPUT_DEBUG_STRING_EVENT : Begin
                                             IF (DebugEvent.DebugString.fUnicode = 0) Then
                                             Begin
                                                StrBuffer := StrAlloc(DebugEvent.DebugString.nDebugStringLength);
                                                ReadProcessMemory(OpenHandle{DebugEvent.dwProcessId}, @DebugEvent.DebugString.lpDebugStringData^, StrBuffer, DebugEvent.DebugString.nDebugStringLength, ReadLen);
                                                Form1.Memo1.Lines.Add(IntToStr(ReadLen)+' - STR - '+StrPas(StrBuffer));

                                                StrDispose(StrBuffer);
                                             End
                                              Else
                                               Begin
                                                  SetLength(UniBuffer, DebugEvent.DebugString.nDebugStringLength);
                                                  ReadProcessMemory(DebugEvent.dwProcessId, DebugEvent.DebugString.lpDebugStringData, @UniBuffer[1], DebugEvent.DebugString.nDebugStringLength, ReadLen);
                                                  UniBuffer := Copy(UniBuffer, 1, ReadLen);

                                                  Form1.Memo2.Lines.Add(IntToStr(DebugEvent.DebugString.nDebugStringLength)+' - UNI - '{+UniBuffer});
                                               End;
                                          End;
              CREATE_THREAD_DEBUG_EVENT : Begin
                                             Form1.TreeView1.Items.AddChild(ThreadNode, IntToStr(DebugEvent.CreateThread.hThread));
                                          End;
            End;
         End;

         ContinueDebugEvent(DebugEvent.dwProcessId, DebugEvent.dwThreadId, DBG_CONTINUE);
      End
       Else Sleep(50);
   End;

   CloseHandle(OpenHandle);
End;

Constructor TDebugThread.Create;
Begin
   Inherited Create(False);
   FreeOnTerminate := True;
End;

Destructor TDebugThread.Destroy;
Begin
   Inherited Destroy;
End;

End.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Kleiner
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)