Wie erhält man Delegat aus Funktionszeiger



  • Hallo,

    ich habe eine manged Klasse und ich möchte einer Methode einen Funktionszeiger übergeben. Diese soll daraus ein Delegate erstellen und intern abspeichern.

    Die Methode sieht z.B. folgendermaßen aus:

    delegate void FunctionDelegate(void);
    
    ref class ManagedClass
    {
    private:
        FunctionDelegate ^fktDel;
    
    public:
        ManagedClass();
        void setFktPtr(System::String ^Name, void (*f)(void));
    };
    
    void ManagedClass::setFktPtr(System::String ^Name, void (*f)(void))
    {
        this->fktDel += (Umwandlung)f;
    }
    


  • Hallo,

    habe das bisher nur andersrum gebraucht aber versuch doch.

    System::Runtime::InteropServices::Marshal::GetDelegateForFunctionPointer(...)
    


  • Danke, hat geklappt.

    Es ist momentan so implemetiert:

    delegate void MailboxSockEvent(void);
    
    void SubscribeVariable(char *kszVarName, void (*f)(void))
    {
        System::IntPtr funcPtr = static_cast<System::IntPtr>(f);
        MailboxSockEvent ^eventDelegate;
        try
        {
            eventDelegate = safe_cast<MailboxSockEvent^>
                (System::Runtime::InteropServices::Marshal::
                GetDelegateForFunctionPointer(funcPtr, MailboxSockEvent::typeid));
        }
        catch(...)
        {
        }
    }
    

Anmelden zum Antworten