3

I want to find a way to know that a form was created at run time (or destroyed). This for Delphi or fpc. Many thanks

PS : Is there a way to retrieve that info for all objects ?

3 Answers3

6

I want to have a event that tells me that a new object was just created at run time (or destroyed).

There are no built in events that fire whenever an object is created or destroyed.

Because I like writing code hooks, I offer the following unit. This hooks the _AfterConstruction method in the System unit. Ideally it should use a trampoline but I've never learnt how to implement those. If you used a real hooking library you'd be able to do it better. Anyway, here it is:

unit AfterConstructionEvent;

interface

var
  OnAfterConstruction: procedure(Instance: TObject);

implementation

uses
  Windows;

procedure PatchCode(Address: Pointer; const NewCode; Size: Integer);
var
  OldProtect: DWORD;
begin
  if VirtualProtect(Address, Size, PAGE_EXECUTE_READWRITE, OldProtect) then
  begin
    Move(NewCode, Address^, Size);
    FlushInstructionCache(GetCurrentProcess, Address, Size);
    VirtualProtect(Address, Size, OldProtect, @OldProtect);
  end;
end;

type
  PInstruction = ^TInstruction;
  TInstruction = packed record
    Opcode: Byte;
    Offset: Integer;
  end;

procedure RedirectProcedure(OldAddress, NewAddress: Pointer);
var
  NewCode: TInstruction;
begin
  NewCode.Opcode := $E9;//jump relative
  NewCode.Offset := NativeInt(NewAddress)-NativeInt(OldAddress)-SizeOf(NewCode);
  PatchCode(OldAddress, NewCode, SizeOf(NewCode));
end;

function System_AfterConstruction: Pointer;
asm
  MOV     EAX, offset System.@AfterConstruction
end;

function System_BeforeDestruction: Pointer;
asm
  MOV     EAX, offset System.@BeforeDestruction
end;

var
  _BeforeDestruction: procedure(const Instance: TObject; OuterMost: ShortInt);

function _AfterConstruction(const Instance: TObject): TObject;
begin
  try
    Instance.AfterConstruction;
    Result := Instance;
    if Assigned(OnAfterConstruction) then
      OnAfterConstruction(Instance);
  except
    _BeforeDestruction(Instance, 1);
    raise;
  end;
end;

initialization
  @_BeforeDestruction := System_BeforeDestruction;
  RedirectProcedure(System_AfterConstruction, @_AfterConstruction);

end.

Assign a handler to OnAfterConstruction and that handler will be called whenever an object is created.

I leave it as an exercise to the reader to add an OnBeforeDestruction event handler.

Note that I am not saying that such an approach is a good thing to do. I'm just answering the direct question that you asked. You can decide for yourself whether or not you want to use this. I know I would not do so!

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Use TForm's OnCreate event to inform whoever you want in whatever way you want.

David Jashi
  • 4,490
  • 1
  • 21
  • 26
  • Ok, it is what i thinking but how can you assisgn a general event for all new form created, for example a dialog box ? – user2418856 Jun 11 '13 at 11:23
  • To be more clear, is it possible to assign a new OnCreate event for all new forms who will be created ? – user2418856 Jun 11 '13 at 11:24
  • @user2418856 yes - just assign one. When you do `NewForm := TNewForm.Create;`, follow it with `NewForm.OnCreate := MyOnCreateHandler;` – J... Jun 11 '13 at 11:39
  • @user2418856 Many thanks, it seems to be on the right way. But, please, could you give a short code example. I do not understand what you mean with "just assign one". Many thanks – user2418856 Jun 11 '13 at 11:48
  • I think i must be clearer. It is about a new library for making your application assistive. There is a procedure that you load at runtime "loadassistive" who scan all the form of the application and assisgn a new onClick, onKeypress, etc for each object (with application.component). It is working perfectly but if you create a new form at runtime the list of assistive objects is not updated, so that is the question : does it exist a way to know if a new form was created. Knowing that the library does not know what are the name of the form who gonna be created. Thanks – user2418856 Jun 11 '13 at 11:53
  • 1
    I think it's better you program an Event in an ancestor form and inherit it for all others forms. – EProgrammerNotFound Jun 11 '13 at 11:59
  • @ David. ;-). Ok, i try one more time. I want to have a signal saying that a new form (or object) was created at run-time. – user2418856 Jun 11 '13 at 12:04
  • @ Matheus : thanks, but could you give me a code example how to do that ?. Thanks – user2418856 Jun 11 '13 at 12:06
  • 1
    Generally, to create an inherited form, you goes at Menu: File / New / Other / / Choose your form, make sure inherited is marked, and use it. I don't know about other versions, I'm using Delphi 7 yet. – EProgrammerNotFound Jun 11 '13 at 12:11
0

In MS Windows you can hook events of your process using this small template:

{$mode objfpc}{$H+}
uses
    Windows, JwaWinUser;

function ShellProc(nCode: longint; wParam: WPARAM; lParam: LPARAM): longint; stdcall;
var
    wnd: HWND;
begin
    Result := 0;
    case nCode of
        HSHELL_WINDOWCREATED:
        begin
            wnd := wParam;
            // Check window
            // Get task handle
            // Get window icon
            // Add task to the list
            // Call event
        end;
        HSHELL_WINDOWDESTROYED:
        begin
            wnd := wParam;
            // Check window
            // Get task handle
            // Get window icon
            // Remove task to the list
            // Call event
        end;
        HSHELL_LANGUAGE:
        begin
            // Get language
            // Call event
        end;
        HSHELL_REDRAW:
        begin
            // Call event
        end;
        HSHELL_WINDOWACTIVATED:
        begin
            // Get language
            // Call event
        end;
        //HSHELL_APPCOMMAND:
        //begin
        //    { TODO 1 -ond -csys : Specify return value for this code }
        //    Result := -1;
        //end;
    end;

    // Call next hook in the chain
    Result := CallNextHookEx(
        0,
        nCode,
        wParam,
        lParam);
end;

var
    FCallbackProc: HOOKPROC;

function InitShellHook(AProc: HOOKPROC): HHOOK; stdcall; export;
begin
    FCallbackProc := AProc;
    Result := SetWindowsHookEx(WH_SHELL, @ShellProc, 0, 0);
end;

procedure DoneShellHook(AHook: HHOOK); stdcall; export;
begin
    UnhookWindowsHookEx(AHook);
end;

HSHELL_WINDOWCREATED will inform you that your process was create new window.

Call InitShellHook with your procedure address (see HOOCPROC declaration).

Abelisto
  • 14,826
  • 2
  • 33
  • 41
  • It's quite hard to relate that to Delphi objects. Not least to window re-creation techniques in VCL. – David Heffernan Jun 12 '13 at 06:09
  • @DavidHeffernan It works for any window (as i remember question was also about ShowMessage). For regular Delphi forms i will try to see to Application object or try to write TForm helper. – Abelisto Jun 12 '13 at 10:01
  • Not any window. Only top level unowned windows. My point was more about re-creation. You'll get these notifications more than once over a form's life, and not when the form is originally created. – David Heffernan Jun 12 '13 at 10:08
  • @DavidHeffernan This hook works on the any event of calling process: new window, nwe keybd layout and so on (remember my question about win taskbar? It is part of this). – Abelisto Jun 12 '13 at 10:13
  • I know. I use that same hook myself. I'm just pointing out that it will not fire once and once only when the Delphi form is created. – David Heffernan Jun 12 '13 at 10:16
  • @DavidHeffernan In case of native Delphi form I hope `if Form.Handle = wnd` will helpful :o) In my current poject i will use a lot of Win API features. So I hope for your advices :o) – Abelisto Jun 12 '13 at 10:22