在Pascal中调用方法

时间:2013-02-05 15:13:53

标签: pascal invoke

我面临以下问题:

我的主要编程语言是带有Qt4库的C ++,但现在我必须编写一个Pascal Wrapper,它应该可以在Pascal中使用C DLL的功能。 现在我希望能够从任何指针调用方法。但我找不到一个Pascal方法来调用方法。我想要QT中的QMetaObject :: invokeMethod方法。我得到了以下代码:

unit CgPConnect;

//{$mode objfpc}{$H+}
{$mode delphi}

interface

    uses
        Classes, SysUtils, dynlibs;

    type

        Callback = Record
          var callbackObject: Pointer;
          var objectFunction: string;
        end;

        CallbackObject = Record
          var objectName: string;
          var callback: Callback;
        end;

        MutableObject = Object
          var name: string;
          var state: string;
          var properties: array of VariantMap;
          var annotations: array of VariantMap;
        end;



        PConnect = Class
            constructor create(connectorPath: string);
            destructor destroy;

            private
                var hostactionCallbacks: array of CallbackObject;
                var mConnectorPath: string;
                var mConnectorLibrary: TLibHandle;
                function loadConnectorLibrary: Boolean;

            public
                procedure registerCallbackForHostaction(objectName, objectFunction: string; callbackObject: pointer);
                procedure callHostactionCallback(receivedObject :MutableObject);

                var mLibraryLoaded: Boolean;
        end;

    implementation

    constructor PConnect.create(connectorPath: string);
    begin
        mConnectorPath:= connectorPath;
        mLibraryLoaded:= false;
        //Eventuell noch slash hinzufügen
        mLibraryLoaded:= loadConnectorLibrary;
    end;

    destructor PConnect.destroy;
    begin
        UnloadLibrary(mConnectorLibrary);
    end;

    procedure PConnect.registerCallbackForHostaction(objectName, objectFunction: string; callbackObject: pointer);
    var c: Callback;
    var callbackCount: integer;
    begin
        if mLibraryLoaded = true then
        begin
            c.callbackObject:= callbackObject;
            c.objectFunction:= objectFunction;
            callbackCount:= Length(hostactionCallbacks)+1;
            SetLength(hostactionCallbacks, callbackCount);
            hostactionCallbacks[callbackCount].objectName:= objectName;
            hostactionCallbacks[callbackCount].callback:= c;
        end;
    end;

    procedure PConnect.callHostactionCallback(receivedObject :MutableObject);
    var receivedObjectName, objectFunction: string;
    var i, count: integer;
    var callbackObject: pointer;
    begin
        if mLibraryLoaded = true then
        begin
            receivedObjectName:= receivedObject.name;
            count:= Length(hostactionCallbacks);
            for i:=0 to count do
            begin
                if hostactionCallbacks[i].objectName = receivedObjectName
                begin
                    objectFunction:= hostactionCallbacks[i].callback.objectFunction;
                    callbackObject:= hostactionCallbacks[i].callback.callbackObject;
                    if callbackObject <> 0 then
                        //INVOKE METHOD (objectFunction) OF OBJECT (callbackObject)
                end;
            end;
        end;
    end;
end.

我很乐意快速回答:)

1 个答案:

答案 0 :(得分:0)

您不能直接和便携地从Pascal调用C ++方法。如果您的回调函数是C ++对象,请忘记它。

否则填充TMethod对象并将其转换为对象&#34;的正确过程。宣言。不要忘记召唤大会。

对于更奇怪的解决方案,您可能需要查看(RemObjects&#39;)Pascalscript。

P.S。这是相同的,你甚至可以从另一个C ++编译器可靠地调用C ++方法。它不是Pascal与C ++本身。