Hi I am trying to call my managed call back callback , from unmanaged code thorugh reverse P/Invoke , I am using VS 2003 , and i am not able to complie the following code (in C#). The same code if i copy paste to VS 2005 it compiles without any issues and is executing correctly.
public delegate void MyDelegate();
[DllImport("rtxivsys.dll", CallingConvention=CallingConvention.StdCall)]
static internal extern void SampleDelegate(MyDelegate myDelegate);
public class Myclass
{
void callthedelegate()
{
SampleDelegate(this.Target1);//Compilation error here.
}
void Target1()
{
//some code here , i want this to be called by the unmanaged code
}
}
If i don’t give the parenthesis for Target1 , it gives me compilation error that “Target1 is referenced without the parenthesis” and if i give the parenthesis then it says the arguement can’t be converted to MyDelegate.
I am not sure what is making it work in VS2005.
Thanks for any suggestions.
IIRC, the following should work:
The automatic creation of delegates was one of the new features in C# 2.0, first implemented in VS2005.
NB: I don’t have a working VS2003 nearby, so cannot test this.
As an aside: if you add paranthesis, like this
SampleDelegate(this.Target1());you are callingTarget1and trying to feed the result toSampleDelegate. But the returned result is not a delegate, hence the compilation error.