Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-10

Calling Procedures via pointers

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andy Powell

Hi,

I've been away from Pascal for about 10 years now and have just started to get back into it. So, spank me hard and call me a newbie if you must...

Anyhow, onto the problem...

I'm designing a unit (and a little test application) that i want to add a call back function to (as exit code)... i.e.

The program gives the unit a pointer to the procedure MyCallback

When an error occurs when the unit is communicating to the device (for example a serial device) if the pointer pCallback is not NIL I'd like to be able to call the user defined procedure, in this case MyCallBack.

The idea is that I can compile the unit, then define the exit code in my individual applications... can someone give me some pointers (pun intended) on how to go about it....

Many thanks

Andy

below is the sort of thing i'm trying to do...

in program:

Procedure MyCallback();
Begin
	{do some stuff then}
	Halt;
End;

in unit:

Procedure SetCallBack(pProc : Pointer);
Begin
       pCallBack := pProc;
End;

{ Carry out the procedure the user defined}

Procedure DoCallBack;
Begin
       If (pCallBackNIL) then
             {Call the users procedure}
       else
             Halt;
End;

Reply by email

Anton Tichawa
to Andy Powell

Hello, Andy!

Show quoted text 19 lines
Hi, I've been away from Pascal for about 10 years now and have just started to get back into it. So, spank me hard and call me a newbie if you must... Anyhow, onto the problem... I'm designing a unit (and a little test application) that i want to add a call back function to (as exit code)... i.e. The program gives the unit a pointer to the procedure MyCallback When an error occurs when the unit is communicating to the device (for example a serial device) if the pointer pCallback is not NIL I'd like to be able to call the user defined procedure, in this case MyCallBack. The idea is that I can compile the unit, then define the exit code in my individual applications... can someone give me some pointers (pun intended) on how to go about it....

Pascal supports procedure types, which are more reliable compared to untyped pointers. Procedure types also distinguish the types of the procedure's parameters (which is not important for your parameterless callback procedure). My suggestion is:

in program:

Procedure MyCallback();
Begin
        {do some stuff then}
        Halt;
End;
begin
  SetCallBack(MyCallBack);
end.

in unit:

type
  TCallBack = procedure;
var
  pCallBack: TCallBack;
Procedure SetCallBack(pProc : TCallBack);
Begin
       pCallBack := pProc;
End;

{ Carry out the procedure the user defined}

Procedure DoCallBack;
Begin
       If (pCallBackNIL) then
             pCallBack;
       else
             Halt;
End;
begin
    pCallBack := nil;
end.

hth,

Anton.

----------

Ing. Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
phone: +43 1 218 97 59
email:

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Reply by email

Andy Powell
to Anton Tichawa
Anton Tichawa wrote 7 lines
Hello, Andy! Pascal supports procedure types, which are more reliable compared to untyped pointers. Procedure types also distinguish the types of the procedure's parameters (which is not important for your parameterless callback procedure). My suggestion is:

Hey Anton!

You're a Star! I made a minor change (using SetCallBack(@MyCallBack);) and it appears to
be working REALLY well, I just need to test my application...

Many thanks indeed, you saved me there!! And such a quick response too :D Thanks!!!

Andy

Reply by email

Andy Powell
you wrote 19 lines
At 22:50 29/10/2003, Anton Tichawa wrote: >Hello, Andy! > >Pascal supports procedure types, which are more reliable compared to untyped >pointers. Procedure types also distinguish the types of the procedure's >parameters (which is not important for your parameterless callback >procedure). My suggestion is: Hey Anton! You're a Star! I made a minor change (using SetCallBack(@MyCallBack);) and it appears to be working REALLY well, I just need to test my application... Many thanks indeed, you saved me there!! And such a quick response too :D Thanks!!! Andy

Ok, ok, I spoke too soon - it didn't work.... :'(

Andy

Reply by email