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

发布于 2024-10-16 20:08:18 字数 1116 浏览 1 评论 0原文

帮助!

我对看似相当简单的任务感到完全疲惫/沮丧。我不确定我做错了什么;更不用说我做得是否正确了。在开发 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 实现,并且两个外部项(command_status 和 struct command_str)。

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

预先感谢您的帮助。

Help!

I'm totally exhausted/frustrated with what seems to be a reasonably easy task. I’m not sure what I'm doing wrong; let alone if I'm doing it correct. I'm "required" to use an existing library (a C static library – over 100,000 lines of straight C code) in developing a WPF application (VS 2010, C# 4.0). Oh, and I can't touch the existing C code - use it as is!

I've read so many postings (advanced topics, how-to, etc), yet I'm so new to C++/CLI that it's just not making sense. From what I've read the best approach is to wrap the C static library as follows:

Unmanaged C static library <---> C++/CLI managed wrapper DLL <--->
managed WPF application

This is the stripped down C header file:

/* 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;

The problem(s):

What I can’t seem to do correctly is provide a C++/CLI implementation for the call back functions, and the two extern items (command_status and struct command_str).

Can someone provide a sample C++/CLI implementation for the above missing call back functions and externs?

Thanks in advance for your assistance.

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

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

发布评论

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

评论(2

你的背包 2024-10-23 20:08:18

在您的 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();
}

in your C++/CLI managed wrapper project, add 2 files :

a .c file :

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;
}

a cpp file

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

void doSomething() {
    doSomethingManagedWrapper();
}
筑梦 2024-10-23 20:08:18

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

还在 C 头文件的 #include 周围放置一个 extern "C" 块。

when you implement these in your c++/cli module, use the same signature shown in the c header file,but prefixed with extern "C".

also put an extern "C" block around the #include of the C header file.

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