Passing a routine address to a C# subroutine?

V

viboater

How do you implement the following prototype in C#?

C/C++ :
char *blah(int zz)
{
...
...
...
}
char *foo(char *(pRtn)(char *))
{
return pRtn(3);
}
void xxx()
{
char *p;
p=foo(&blah);
}



How do you do this in C#?
Thanks
 
B

Bruce Wood

delegate string BlahDelegate(int zz);

string blah(int zz)
{
....
}

// I'm assuming that your char *foo(char *(pRtn)(char *))
// was a typo and you actually meant
// char *foo(char *(pRtn)(int))
string foo(BlahDelegate myBlah)
{
return myBlah(3);
}

void xxx()
{
string p = foo(new BlahDelegate(blah));
}
 
D

Daniel O'Connell [C# MVP]

How do you implement the following prototype in C#?

C/C++ :
char *blah(int zz)
{
..
..
..
}
char *foo(char *(pRtn)(char *))
{
return pRtn(3);
}
void xxx()
{
char *p;
p=foo(&blah);
}



How do you do this in C#?
Delegates, basically.

public delegate string MyDelegate(int zz);

string blah(int x)
{
....
}

string foo(MyDelegate del)
{
return del(3);
}

void xxx()
{
string x;
x = foo(new MyDelegate(blah));
}

This is assuming you want a string. If you want a character buffer for
interop reasons you have to return an IntPtr or use unsafe code and return
byte *(char is a 16bit type in C#).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top