0

我面临以下问题:

我的主要编程语言是带有 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.

我会很高兴得到一个快速的答案:)

4

1 回答 1

0

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

否则填充一个 TMethod 对象并将其转换为正确的“对象过程”声明。不要忘记调用约定。

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

Ps 这与您甚至无法从另一个 C++ 编译器可靠地调用 C++ 方法相同。它本身不是 Pascal vs C++。

于 2013-02-06T16:02:56.323 回答