C静态库的C ++ / CLI托管包装器

时间:2011-02-10 16:35:10

标签: c++-cli

帮助!

我对完成一项看似相当容易的任务感到非常疲惫/沮丧。我不确定我做错了什么;如果我做得对,更不用说了。在开发WPF应用程序(VS 2010,C#4.0)时,我“需要”使用现有的库(C静态库 - 超过100,000行直接C代码)。哦,我无法触及现有的C代码 - 按原样使用它!

我已经阅读了很多帖子(高级主题,操作方法等),但我对C ++ / CLI这么新,以至于它没有意义。根据我的阅读,最好的方法是将C静态库包装如下:

  

非托管C静态库< ---> C ++ / CLI托管包装器DLL< --->   托管WPF应用程序

这是剥离的C头文件:

/* Call this function to execute a command. */
int issue_command(int command, long param1, long param2);

/* Completion call back function; you must supply a definition. */
extern  int command_completed(int command, long param1, long param2);

struct struct_command_str
{
      char   command_str[10];
      char   param1_st[2];
      char   param2_st[2];
      char   success;
};

/* You must supply definitions to the following extern items. */
extern  int command_status;
extern struct struct_command_str  command_str;

问题:

我似乎无法正确做的是为回调函数提供C ++ / CLI实现,以及两个extern项(command_status和struct command_str)。

有人可以为上述缺少的回调函数和外部函数提供示例C ++ / CLI实现吗?

提前感谢您的协助。

2 个答案:

答案 0 :(得分:3)

在C ++ / CLI托管包装器项目中,添加2个文件:

.c文件:

extern void doSomething();

int command_status = 0;

struct_command_str command_str = { "command1", "p1", "p2", 't' };

int command_completed(int command, long param1, long param2) {
    ...
    command_status = 1;
    ...
    doSomething();
    ...
    command_status = 2;
    ...
    return 3;
}

cpp文件

void doSomethingManagedWrapper() {
    ...
    call managed code
    ...
}

void doSomething() {
    doSomethingManagedWrapper();
}

答案 1 :(得分:1)

在c ++ / cli模块中实现这些时,请使用c头文件中显示的相同签名,但前缀为extern "C"

还在C头文件的extern "C"周围添加了#include块。

相关问题