Solved

Convert delegate to function pointer in Visual Studio 2005

Posted on 2006-07-18
1
321 Views
Last Modified: 2008-02-01
Hi,

I'm wondering if there is a way to convert delegates to function pointers in Visual Studio 2005 without making function calls through any DLL nor using the /clr:oldSyntax compiler option?

I have read the following articles, but the example codes are based on the VS2003 syntax:
    http://www.codeproject.com/managedcpp/delegate_to_callback.asp
    http://www.codeproject.com/managedcpp/unmanaged_to_managed.asp

Thanks,

James
0
Comment
Question by:james567
1 Comment
 
LVL 15

Accepted Solution

by:
lakshman_ce earned 500 total points
ID: 17135441
Use Marshal::GetFunctionPointerForDelegate

Here is the sample code

#include "stdafx.h"

using namespace System;
using namespace System::Runtime::InteropServices;
delegate void myMethodDelegate( // Declares a delegate for a method that takes in an int and returns a String.
int myInt );
typedef void (*PFUNC)(Int32);

// Defines some methods to which the delegate can point.
ref class mySampleClass
{
public:

   // Defines an instance method.
   void myStringMethod( int myInt )
   {
      if ( myInt > 0 )
              Console::WriteLine("positive");

      if ( myInt < 0 )
            Console::WriteLine ("negative");
  }
};

int main()
{
   mySampleClass^ mySC = gcnew mySampleClass;
   myMethodDelegate^ myD1 = gcnew myMethodDelegate( mySC, &mySampleClass::myStringMethod );
   myD1( 5 );
   IntPtr pint = Marshal::GetFunctionPointerForDelegate(myD1);
   PFUNC pFnc = (PFUNC)pint.ToPointer();
   pFnc(5);  
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

12 Experts available now in Live!

Get 1:1 Help Now