Managed function pointers

Posted on 2006-05-23
Last Modified: 2013-12-03
OK, this is my case:

I have one class library that recieves network data, and interprets that very data. When a command reaches the interpreter, I want a function in another class library to be executed when a specific command is executed.

The class library which holds the function that should be executed already holds a reference to the network library, so adding the class library holding the function to be executed can not be referenced from the network library.

I would therefore need function pointers I guess.

My problem is that I don't know how I would create these pointers, is delegates the correct way?

Unmanaged function pointers just doesn't seem to work in managed C++.

Any help is appreciated!
Question by:bjqrn
    1 Comment
    LVL 48

    Accepted Solution

    I hope this sample can help:

    using namespace System;

    public delegate int FunctionPtr(int);

    ref class Server
        int Function(int n)
            return n + 1;

        static int StaticFunction(int n)
            return n - 1;

    ref class Client
        void DoSomething(FunctionPtr^ ptr, int n)
            int result = ptr(n);

    int main(array<System::String ^> ^args)
        Server^ server = gcnew Server();
        Client^ client = gcnew Client();

        FunctionPtr^ ptr = gcnew FunctionPtr(server, &Server::Function);
        client->DoSomething(ptr, 1);

        ptr = gcnew FunctionPtr(&Server::StaticFunction);
        client->DoSomething(ptr, 10);

        return 0;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now