Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: implementing a callback mechanism

  1. #1

    implementing a callback mechanism

    i need help with adding callback mechanism to my program, i have this:

    // global
    var
    CBackProc: Procedure(src: Longword); stdcall;

    // a procedure
    CBackProc:= Sources[src].DeleteCB; <-deletecb is pointer to a procedure, filled from another function

    CBackProc(src); // call it

    this causes my program to collapse and crash, any idea why? and perhaps what am i doing wrong?
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  2. #2

    implementing a callback mechanism

    From a quick glance, I don't see whats wrong. So I'll show you another way of doing it that is common:

    [pascal]unit test;

    interface

    type
    TMyProc=procedure(SomeVar1, SomeVar2 : PChar); cdecl;

    var
    MyProc : TMyProc;

    procedure LoadProcs;

    implementation

    procedure LoadProcs;
    var
    l : Cardinal;
    begin
    l := LoadLibrary('MyDLL.dll');
    if l <> nil then
    begin
    @MyProc := GetProcAddress(l, 'FOO');
    end;
    end;

    end.[/pascal]

    Then later to call it:
    [pascal]
    MyProc('Test1', SomeOtherPChar);
    [/pascal]

    While this is FPC source it should work for you. Also you might try taking a look at the Dr Bob's article (a bit dated, but still useful) at: http://www.drbob42.com/delphi/headconv.htm

  3. #3

    implementing a callback mechanism

    no, what i am doing is right the opposite, and it has nothing to do with dlls, this will be used in a unit to notify main program that a sound source got deleted (within the application).. something for my audio library.
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  4. #4
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    implementing a callback mechanism

    Why not just make it a TNotifyEvent and use it as an event handler?
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  5. #5

    implementing a callback mechanism

    i don't use delphi's vcl... this is intended for raw coding..
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  6. #6
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    implementing a callback mechanism

    I dont use the VCL either but I still make use of TNotifyEvent

    Maybe you need to make your own event handler then:

    [pascal]
    unit Unit1;

    interface

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

    Type
    TMyNotify = Procedure(Value : Integer) of Object;

    type
    TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    private
    FNotify: TMyNotify;
    procedure SetNotify(const Value: TMyNotify);
    { Private declarations }
    public
    { Public declarations }
    Property Notify : TMyNotify read FNotify write SetNotify;
    Procedure MyNotifyHandler(Value : Integer);
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    If Assigned(Notify) then
    Notify(100);
    end;

    procedure TForm1.MyNotifyHandler(Value: Integer);
    begin
    Button1.Caption := IntToStr(Value);
    end;

    procedure TForm1.SetNotify(const Value: TMyNotify);
    begin
    FNotify := Value;
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    Notify := MyNotifyHandler;
    end;

    end.
    [/pascal]
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  7. #7

    implementing a callback mechanism

    Why the need for stdcall? Have you tried without it?
    Are you compiling in Delphi?

    Try a simple example first, such as:

    var
    CBackProc: Procedure(src: Longword);

    procedure test(src: Longword);
    begin
    WriteLn('Hello')
    end;

    begin
    CBackProc:= Test;
    CBackProc(src); // call it
    end.
    ZGameEditor - Develop 64kb games for Windows.
    Thrust for Vectrex - ROM-file and 6809 source code.

  8. #8

    implementing a callback mechanism

    I'm doing just that, but i think the problem is storing the function reference in a pointer..

    declarations:

    Type
    TDeleteNotifyproc = Procedure(src: Longword; userdata: pointer); stdcall;

    Tsource = packed record
    UserData: Pointer;
    mode: TSourceMode;
    ALsource: ALuint;
    DeleteCB: TDeleteNotifyproc;
    VirtualSource: Tvirtualsource;
    end;
    This is my game project - Top Down City:
    http://www.pascalgamedevelopment.com...y-Topic-Reboot

    My OpenAL audio wrapper with Intelligent Source Manager to use unlimited:
    http://www.pascalgamedevelopment.com...source+manager

  9. #9

    implementing a callback mechanism

    this works for me...

    [pascal]
    program Project1;

    {$APPTYPE CONSOLE}

    type
    TDeleteNotifyproc = procedure( src : Longword; userdata : pointer ); stdcall;

    TSource = packed record
    DeleteCB : TDeleteNotifyproc;
    end;

    var
    aSource : TSource;
    aCallBack : TDeleteNotifyproc;

    procedure test( src : Longword; userdata : pointer ); stdcall;
    begin
    WriteLn( 'Hello' )
    end;

    begin
    aSource.DeleteCB := Test;
    aCallBack := aSource.DeleteCB;
    aCallBack( 0, nil ); // call it
    end.
    [/pascal]

    If you are doing everything within Pascal you really do not need the "stdcall" calling convention. If you plan to make it accessible to C and allow plugins then on windows use stdcall and on Linux I believe you need to use cdecl.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  10. #10

    Re: implementing a callback mechanism

    Quote Originally Posted by Delfi
    // a procedure
    CBackProc:= Sources[src].DeleteCB; <-deletecb is pointer to a procedure, filled from another function
    just looking back over this code are you sure that Sources[src].DeleteCB is pointing to a valid callback function?

    to avoid the AV you could use some defensive programming and try
    [pascal]
    if Assigned ( aCallBack ) then
    begin
    aCallBack( 0, nil ); // call it
    end
    else
    begin
    WriteLn( 'No Callback function defined' )
    end
    [/pascal]

    Using "if aCallBack <> nil" will not work as the compiler treats that as an attempt to make a function/procedure call.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •