tags:

views:

63

answers:

2

I have a .dll written in C++ with a function defined like this:

EDK_API int EE_CognitivSetCurrentLevel  ( unsigned int  userId,  
  EE_CognitivLevel_t  level,  
  EE_CognitivAction_t  level1Action,  
  EE_CognitivAction_t  level2Action,  
  EE_CognitivAction_t  level3Action,  
  EE_CognitivAction_t  level4Action   
)    

Set the current Cognitiv level and corresponding action types. 


Parameters:
userId  - user ID  
level  - current level (min: 1, max: 4)  
level1Action  - action type in level 1  
level2Action  - action type in level 2  
level3Action  - action type in level 3  
level4Action  - action type in level 4

usage of this function, as you can see above: if level = 1, it'll be called like this:

EE_CognitivSetCurrentLevel(userId,1,level1Action);

if level = 2, then:

EE_CognitivSetCurrentLevel(userId,2,level1Action,level2Action);

and so on...

How can I invoke this function in C#?

Thank you very much!

+3  A: 

Assuming EE_CognitivLevel_t and EE_CognitivAction_t are integers:

[DllImport ("yourdll", EntryPoint ("EE_CognitivSetCurrentLevel")]
extern static int EE_CognitivSetCurrentLevel1 (int level, int level1);

[DllImport ("yourdll", EntryPoint ("EE_CognitivSetCurrentLevel")]
extern static int EE_CognitivSetCurrentLevel2 (int level, int level1, int level2);

And so on... Oh, and make sure that function is inside an extern "C" {} so that the C++ compiler does not mangle the name.

Gonzalo
+2  A: 

Since it's a C++ function I'm assuming that it has default parameters. That is when you call the function with too few parameters the C++ compiler auto fills in the missing parameters with default values. C# doesn't support default parameters and neither does calling from a DLL. If this is the case, then you need to find out what these default parameters are and manually pass them in. If you pass the wrong number of parameters to a function you'll end up with strange behavior (or it could work, who knows).

You can use overloading in C# to provide the same behavior you see in C++:

class EEFunctions {
    [DllImport ("yourdll", EntryPoint ("EE_CognitivSetCurrentLevel")]
    private extern static int EE_CognitivSetCurrentLevelDLL(int level, int level1,
        int level2, int level3, int level4);

    private int defaultLevel = 0;  // This is just an example

    public static int EE_CognitivSetCurrentLevel(int level, int level1) {
        return EE_CognitivSetCurrentLevel(level, level1, 
             defaultLevel, defaultLevel, defaultLevel);
    }

    public static int EE_CognitivSetCurrentLevel(int level, int level1, int level2) {
        return EE_CognitivSetCurrentLevel(level, level1, 
             level2, defaultLevel, defaultLevel);
    }

    public static int EE_CognitivSetCurrentLevel(int level, int level1,
         int level2, int level3) {
        return EE_CognitivSetCurrentLevel(level, level1, 
             level2, level3, defaultLevel);
    }

    public static int EE_CognitivSetCurrentLevel(int level, int level1,
         int level2, int level3, int level4) {
        return EE_CognitivSetCurrentLevel(level, level1, 
             level2, level3, level4);
    }
}

This also assumes that all the parameters are ints. If you can find the definitions for the parameters types in the C++ headers, you can create compatible types in C#.

shf301