使用“奇怪”的 PInvoke功能

发布于 2024-08-11 10:47:07 字数 909 浏览 4 评论 0原文

我有一个用 C++ 编写的 .dll,其函数定义如下:

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 

该函数的用法,如上所示:如果 level = 1,它将像这样调用:

EE_CognitivSetCurrentLevel(userId,1,level1Action);

如果 level = 2,则:

EE_CognitivSetCurrentLevel(userId,2,level1Action,level2Action);

等等。 ..

如何在 C# 中调用这个函数?

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2024-08-18 10:47:07

假设 EE_CognitivLevel_t 和 EE_CognitivAction_t 是整数:

[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);

依此类推...哦,并确保该函数位于 extern "C" {} 内,以便 C++ 编译器不会弄乱这个名字。

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.

﹏半生如梦愿梦如真 2024-08-18 10:47:07

由于它是一个 C++ 函数,我假设它有 默认参数。 也就是说,当您调用参数太少的函数时,C++ 编译器会自动用默认值填充缺少的参数。 C# 不支持默认参数,也不支持从 DLL 调用。如果是这种情况,那么您需要找出这些默认参数是什么并手动将它们传递进去。如果您将错误数量的参数传递给函数,您最终会出现奇怪的行为(或者它可能会起作用,谁知道呢? )。

您可以在 C# 中使用重载来提供与 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);
    }
}

这还假设所有参数都是整数。如果您可以在 C++ 标头中找到参数类型的定义,则可以在 C# 中创建兼容类型。

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#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文