How to call a procedure or function when we do not know the parameters?

My application should provide the ability to call various functions and procedures from external DLLs. Thus, we do not know the number of parameters and their types. What should I do to do this?

Let me explain this more. My application is a RAD tool, and it has its own script and syntax ... I want users to be able to use ANY DLL file and call any function or procedure that they want. I can’t use the simple method to call dll ( LoadLibraryand then GetProcAddress) because I don’t know what type it belongs to GetProcAddress( var Proc:procedure (A:??;B:??;...)).

+5
source share
4 answers

This is a simple example that works on my machine, but I am not an expert on this.

procedure TForm4.Button1Click(Sender: TObject);
var
  hmod: HMODULE;
  paddr: pointer;         
  c1, c2, ret: cardinal;
begin
  c1 := 400; //frequency
  c2 := 2000; // duration

  hmod := LoadLibrary('kernel32'); // Of course, the name of the DLL is taken from the script
  if hmod <> 0 then
    try
      paddr := GetProcAddress(hmod, 'Beep'); // ...as is the name of the exported function
      if paddr <> nil then
      begin
        // The script is told that this function requires two cardinals as
        // arguments. Let call them c1 and c2. We will assume stdcall
        // calling convention. We will assume a 32-bit return value; this
        // we will store in ret.
        asm
          push c2
          push c1
          call [paddr]
          mov ret, eax
        end;
      end;
    finally
      FreeLibrary(hmod);
    end;
end;
+4
source

I have a Delphi implementation in the script functionality of my ZGameEditor project, find "TExpExternalFuncCall.Execute" in the file below:

http://code.google.com/p/zgameeditor/source/browse/trunk/ZExpressions.pas

Tested and works under Windows (x86 and x64), Linux, Android (ARM) and OS X (x86). Manages stdcall and cdecl invocation calls.

But libFFI is probably more general than my implementation, so I would recommend this approach.

+5
source

, , " " (FFI) .

, FFI . FFI libffi.

Wikipedia libffi libffi:

Python, Dalvik, F- Script, PyPy, PyObjC, RubyCocoa, JRuby, Rubinius, MacRuby, gcj, GNU Smalltalk, IcedTea, Cycript, Pawn, Squeak, Java Native , PLT-, Lisp Mozilla.

libffi Python/ctypes DLL Delphi, , , Python/ctypes .

, , libffi. , , Delphi, C/asm.

+3

, FFI, .

, , ctypes Python FFI, , libFFI (ctypes) ( python). python .

, Python:

http://code.activestate.com/recipes/146847/

python ( C) , Python Delphi, . ( RAD-), FFI.

I personally am not ready to develop a complete, workable programming language and all its libraries from scratch, so I prefer to hybridize what I know. Source code in C or Delphi and dynamic scripts in Python. You can combine all three easily into one application, if necessary.

+3
source

All Articles