使用“奇怪”的 PInvoke功能
我有一个用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设 EE_CognitivLevel_t 和 EE_CognitivAction_t 是整数:
依此类推...哦,并确保该函数位于 extern "C" {} 内,以便 C++ 编译器不会弄乱这个名字。
Assuming
EE_CognitivLevel_t
andEE_CognitivAction_t
are integers:And so on... Oh, and make sure that function is inside an extern "C" {} so that the C++ compiler does not mangle the name.
由于它是一个 C++ 函数,我假设它有 默认参数。 也就是说,当您调用参数太少的函数时,C++ 编译器会自动用默认值填充缺少的参数。 C# 不支持默认参数,也不支持从 DLL 调用。如果是这种情况,那么您需要找出这些默认参数是什么并手动将它们传递进去。如果您将错误数量的参数传递给函数,您最终会出现奇怪的行为(或者它可能会起作用,谁知道呢? )。
您可以在 C# 中使用重载来提供与 C++ 中相同的行为:
这还假设所有参数都是整数。如果您可以在 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++:
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#.