Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 08-19-2011, 11:11 AM
CLANDICE CLANDICE is offline
Senior Member
 
Join Date: Nov 2002
Posts: 112
Default TTimer, runtime in a DLL

Hi All,
I've got the following requirement;
I am busy writing a Winamp Plugin. All Winamp plugins need to be a DLL.

The plugin that I am writing has the need for a TTimer component.
I need to create the TTimer at runtime. Also I do not want to use a ttimer in a form as I I only access/create the form when it is need.

I've tried the following but without "housing" the ttimer component in a form and I would get an error at this line:
Timer1.OnTimer := someproc;

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
  private
    procedure someproc(sender: tobject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Timer1.OnTimer := someproc;
end;

procedure TForm1.someproc(sender: tobject);
begin

end;

end.
Many thx in adv.
Reply With Quote
  #2  
Old 08-20-2011, 06:22 AM
SilverBoy SilverBoy is offline
Member
 
Join Date: Jun 2010
Posts: 46
Default

You should do it like this :

Code:
library Project1;

uses
  Windows,
  SysUtils,
  Classes,
  ExtCtrls;

{$R *.res}

type
  TMyTimer = class(TTimer)
  private
    Procedure DoTimer(Sender: TObject);
  end;

var
  Timer1: TMyTimer;

procedure EntryPoint(Event: DWord);
begin
  // Create an instance of TMyTimer
  case Event of
    DLL_PROCESS_ATTACH:
      begin
        Timer1 := TMyTimer.Create(nil);
        Timer1.OnTimer := Timer1.DoTimer;
      end;

    // Free timer on deattach DLL
    DLL_PROCESS_DETACH:
      Timer1.Free;
  end;
end;

{ Timer }

procedure TMyTimer.DoTimer(Sender: TObject);
begin
  // Insert your timer code here
end;

begin
  Dllproc := @EntryPoint;
  EntryPoint(DLL_PROCESS_ATTACH);

end.
or use DataModule to store your TTimer component on it and create an instance of your DataModule on EntryPoint of it .
Reply With Quote
  #3  
Old 08-21-2011, 08:30 PM
CLANDICE CLANDICE is offline
Senior Member
 
Join Date: Nov 2002
Posts: 112
Default

SilverBoy... you ROCK!!!
Your sample code helped me 100%.

Just a bit of a question;
you do this:
EntryPoint(DLL_PROCESS_ATTACH);

But I see you never do this:
EntryPoint(DLL_PROCESS_DETACH);

This there any reason why that is?

Many thx!!!
Reply With Quote
  #4  
Old 08-22-2011, 08:02 AM
SilverBoy SilverBoy is offline
Member
 
Join Date: Jun 2010
Posts: 46
Default

We change the entry point of dll to own event and call it when dll attach to process , it is not possible to do it when dll deattach from process , windows do it automatically , you can test it , change your entry point event code like below :

Code:
procedure EntryPoint(Event: DWord);
begin
  // Create an instance of TMyTimer
  case Event of
    DLL_PROCESS_ATTACH:
      begin
        Timer1 := TMyTimer.Create(nil);
        Timer1.OnTimer := Timer1.DoTimer;
      end;

    // Free timer on deattach DLL
    DLL_PROCESS_DETACH:
      begin
        ShowMessage('Ok ?');
        Timer1.Free;
      end;
  end;
end;
Reply With Quote
  #5  
Old 08-22-2011, 10:58 AM
CLANDICE CLANDICE is offline
Senior Member
 
Join Date: Nov 2002
Posts: 112
Default

Thank you SilverBoy.

I'll try this tonight.

You've really helped me out.

Reply With Quote
Reply

Tags
dll, runtime, ttimer

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 05:55 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.