SetTimer and Dll

Hello All,

    I'm developing a DLL, and I need a timer to check some events. I could use VCL TTimer, but DLL becomes too large, then I try to use SetTimer and KillTimer Win32 API, but it don't works well. Could someone tell me/show me an example about how to use SetTimer and how to define TimerProc callback?? OBS : My application don't have forms. I read Win32 reference and it says that the window handle can be NULL, but don't works too.

                                 Tanks in advance.



                                              Oberdan




















 
oberdanAsked:
Who is Participating?
 
ITugayConnect With a Mentor Commented:
Hi oberdan!

I have no idea why it happen. But sample bellow is working.

//*********** LIBRARY ***************
library p_dll;
uses  Windows;

var T : integer;

procedure DoSomething(hWnd: HWND; Msg,TimerID: Word; SysTime: LongInt); StdCall;
var F : Text;
begin
    assign(F,'c:\000.txt');
    rewrite(F);
    writeln(F,Msg,' ',TimerID,' ',SysTime);
    CloseFile(F);
end;

procedure InitTimer; stdcall;
begin // interval 2 sec
  T:=SetTimer(0,0,2000,@DoSomething);
end;

procedure DoneTimer; stdcall;
begin
  KillTimer(T,0);
end;

exports
   InitTimer,
   DoneTimer;
begin

end.



//************* PROJECT ************
program CALL_DLL;

uses
  ShareMem,
  Forms,
  Windows;

procedure InitTimer; external 'p_dll.dll';
procedure DoneTimer; external 'p_dll.dll';

var WaitFor : integer;
begin
   Application.Initialize;
   WaitFor:=GetTickCount+5000; // 5 sec to work
   InitTimer;
   while GetTickCount < WaitFor do Application.ProcessMessages;
{let windows to do any deals}
   DoneTimer;
end.

//********** END ***********

Best regards.

Igor.
0
 
LischkeCommented:
What's wrong with the callback proc? It should work fine unless you have wrongly declared the callback procedure. Important: it must be declared with calling convention stdcall:

prcoedure MyCallback(Wnd: HWND; uMsg: Cardinal; iEvent: Cardinal; dwTime: DWORD); stdcall;

begin
  // your stuff here
end;

Ciao, Mike
0
 
LischkeCommented:
Igor, this was not fair. You gave essentially the same information as I but locked the question although you could see I was already discussing...

Please be so kind in the future and work only with comments unless you are the very first one in a thread or noone else can give further information and you have the solution.

Thank you, Mike
0
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
LischkeCommented:
Btw: your code is Delphi 1 stuff, right?
0
 
oberdanAuthor Commented:
Hello,

     I read the comments and the answer, and my code is the same as showed here. But I discover my error (and was a very, very stupid error). My call to SetTimer was right, and my callback was right too. The problem was on my Timer Handler. I'm using one pointer to a record with several Handlers and different data types, including the Timer handler. I forgot to initilize the pointers with "new". So, I was passing a null pointer to SetTimer, and my program blows. Your code show me that the error was not on the clock creation as I was thinking (it's the first time I try to do it).


                                        Very tanks,

                                                   Oberdan
   
0
 
ITugayCommented:
To  Lischke.

Sorry, I was sure that I'm first. (I'm new here).

By.

Igor
0
 
LischkeCommented:
Mmmh, Oberdan already argued why (s)he still picked your contribution and finally, well, it's always up to the questioneer to pick one solution. I just wanted to let you know for the future :-)

Ciao, Mike
0
All Courses

From novice to tech pro — start learning today.