Solved

Convert delegate to function pointer in Visual Studio 2005

Posted on 2006-07-18
1
322 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c++ class logger 6 104
Create a progress bar for timer 4 281
Problem with release version using Visual Studio 2015 19 77
mixing C++ & C# in Vis Studio 2013 7 141
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

17 Experts available now in Live!

Get 1:1 Help Now