A r t i c l e s

Note: This Wiki is
outdated, personal views
may have changed.
L505 A.I. bot is dead
long live THX 1138

M a i n P a g e

D i r e c t o r y

Regular Procedure Bound To Class Method

See also:
program testing123;
{$MODE OBJFPC} {$H+} {$M+}

  TTest = class
    procedure test(i: integer);

var Tester: TTest;

procedure TTest.test(i: integer);
  writeln(i, ' ', i, ' ', i);

function GetMethodAddress: pointer;
  result:= tester.MethodAddress('test');;

function GetClassPointer: pointer;
  result:= pointer(tester);

var test: procedure (self: pointer; i: integer);
var fakeself: pointer;

procedure FuckClasses;
  fakeself:= GetClassPointer;
  pointer(test):= GetMethodAddress;


  Tester:= TTest.create;

  // bind the procedure to a class method and get the class instance

  // now we can launch the class methods without any class
  test(fakeself, 888);  
  // above is the same as doing:
  // Tester.test(888);  
  // it's only syntax sugar differences


Why would this be useful? DLL, plugin. Make use of existing class code inside existing software... export all the class methods.. then have plugin writers using C/C++/FPC/Delphi talk to your software without any package system required (BPL).

Advantages are that you don't use a proprietary package system that may or may not be cross compiler compatible (fpc/delphi/c++/c).

My other attempts at exporting classes have included using virtual classes and then inheriting them in the DLL - but this isn't cross compiler compabitle if some asshole has a Cee compiler and wants to build a plugin for your app (or NameYourLanguageHere).

You could build a procedure to query whether or not the class instance is alive. In the above code I create and free the class.. but in a DLL you'd have to make sure the class was available to use.

How would you export your existing class infrastructure inside your executable (an existing program that has no plugin system)? Well, you would export functions from executable and have the DLL import them from the EXE. Yes, you can export from the EXE, not just the DLL. Unfortunately, many software programmers don't realize you can export from the EXE.

Export the GetMethodAddress and GetClassPointer procedures from the EXE and put the FuckClasses procedure in your DLL and call the GetMethodAddress and GetClassPointer routines from within you DLL after you've imported them from the EXE. Then you can use the methods from your classes by simply setting up regular old procedures in your DLL to bind to the method addresses and class instance pointers.

I'm not sure what class functionality won't work.. for example I haven't tested exporting abstract/virtual/overriden/overloaded methods and other fucked up class nonsense. Well actually I tested overloaded functions and found that the MethodAddress returns the first procedure in the class.

{$MODE OBJFPC} {$H+} {$M+}

  TTest = class
    procedure test(s: string);  // MethodAddress will return this one
    procedure test(i: integer);
Not sure how to gain access to the second overloaded one.

Note: This Wiki is outdated, personal views may have changed.

This wiki contains info on life, health, humans, nature, programming, database, fads, paradigms, poems, principles, theories.

Articles may contain statements which some may find helpful and encouraging, or even discouraging.

Beware, I believe in the Grand Justice system.
_ _ _