1

I was translating some C++ code to C# and there was a function pointer within a structure definition, say func*

This func* was a pointer to a lot of other function pointers all contained within a C++ header file (This header file won't be translated).

Is there a way to translate this?

Code snippet:

struct CK_FUNCTION_LIST {
int version; 
/* Pile all the function pointers into it. */
#include "pkcs11f.h"
};

The class which I wish to translate contained a member of typeCK_FUNC_LIST*.

Tyler Durden
  • 1,188
  • 2
  • 19
  • 35
  • 1
    I think you’re on the right track since you tagged your pose with Delegates. But not sure if you’re saying func* pointed to a series of functions or to a dingle function. The delegate would be the approach to have a “pointer” to a specific function. If you do need a “pointer” to a series of pointers I think you could use array of delegates (or list, or collection, or what works for how you populate the series). If not what you mean, explain more? Or post the section of c++ code? – asantaballa Oct 24 '13 at 16:07

3 Answers3

2

If you are porting the code, you have a couple of options in C#:

1) Use lamdas (e.g. unnamed delegates). If these are 1-off functions (e.g. they are only used once), this will be the best choice.

2) Use named delegates. These will behave very similarly to C++ function pointers. If you are using the same functions in several places, this will be the better choice.

Just for clarification (as Ben Voigt pointed out): these are effectively the same thing as the lamda syntax will create a delegate for you.

The delegate type can be framework-provided (Action, Func, Predicate) or user-declared, and the delegate can be created inline (lambda, anonymous delegate) or by naming a method.

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • That's really only one option. Lambda syntax produces a delegate (unless you're suggesting he use an expression tree?) – Ben Voigt Oct 24 '13 at 16:10
  • 1
    @BenVoigt True, but you can also write and name your own delegate (the difference I was getting at here). I deliberately left out Expression Trees as they are a pain in the you-know-what ... and likely would not serve this purpose well. – Zac Howland Oct 24 '13 at 16:13
  • 1
    Then perhaps you meant to use delegates, with some options: The delegate type can be framework-provided (`Action`, `Func`, `Predicate`) or user-declared, and the delegate can be created inline (lambda, anonymous delegate) or by naming a method. – Ben Voigt Oct 24 '13 at 16:15
  • I like that verbiage better ... I'll copy that into the answer. – Zac Howland Oct 24 '13 at 16:17
  • 1
    @TylerDurden That is a different question and has already been answered: http://stackoverflow.com/questions/2462814/func-delegate-with-ref-variable – Zac Howland Oct 25 '13 at 00:13
2

To demonstrate what you usually do when you want to use the equivalent of function pointers in C#, take a look at this:

struct MyFunctions {
  public Func<int,string,bool> ptr1;
  public Action<int> ptr2;
}

class Program
{
  static void Main(string[] args)
  {
     MyFunctions sample = new MyFunctions() { ptr1 = TestValues, ptr2 = DoThing };
     sample.ptr1(42, "Answer");
     sample.ptr2(100);
  }

  static bool TestValues(int a, string b)
  {
     Console.WriteLine("{0} {1}", a, b);
     return false;
  }

  static void DoThing(int a)
  {
     Console.WriteLine("{0}", a);
  }
}

The output is:

42 Answer
100
BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146
  • So, in my case, I'll have to redefine the functions of "pkcs11f.h"in C# in the same file where I am defining the structure CK_FUNC_LIST? – Tyler Durden Oct 24 '13 at 16:18
  • And why were Func and Action used as member types in the struct def? Are they keywords? – Tyler Durden Oct 24 '13 at 16:19
  • It depends on if you intend to call those functions. If not, then why are they part of the structure. If you are inter-operating with unmanaged code, you can use `IntPtr` as a place-holder (pointer) to unmanaged memory that you may or may not intend to use. – BlueMonkMN Oct 24 '13 at 16:20
  • 1
    Func and Action are generic (templated) types defined by the framework that are designed to simplify the process of defining delegates. Instead of separately declaring a named delegate type that accepts an integer and a string and returns bool and then using it, you can use Func to indicate the function signature all at once. – BlueMonkMN Oct 24 '13 at 16:21
  • If your question about defining functions of pkcs11f.h is emphasizing the "have to redefine" aspect, my above comment answers that - you can use `IntPtr` as a placeholder or remove it from the structure. If, on the other hand, the emphasis is on "in the same file", then the answer is no. You can implement the functions anywhere in your code, and you can declare/define delegates (anywhere) instead of using Func and Action. The structure can be in a separate file from the rest of the code in the sample above too. – BlueMonkMN Oct 24 '13 at 16:27
  • That greatly helped. Thanks. Another doubt, how do I make ptr1 accept ref arguments? I mean the function that I want ptr1 to point to has a few ref arguments because I want the change to reflect. When I write ref, it gives an error – Tyler Durden Oct 24 '13 at 20:53
  • 1
    Looks like the answer to that one is to avoid `Func` and define your delegate "manually" according to http://stackoverflow.com/questions/2462814/func-delegate-with-ref-variable?rq=1 – BlueMonkMN Oct 24 '13 at 23:16
0

The normal way of using function pointers in C# is to declare an Action or Func. These are type safe, so they will only accept methods matching the signature of the type arguments.

Delegates are more generic (Action and Func are just specific named ones) but less clear. Really, we'd need more information to be any more specific.

Magus
  • 1,302
  • 9
  • 16