C++/CLI 托管 C 静态库包装器
帮助!
我对看似相当简单的任务感到完全疲惫/沮丧。我不确定我做错了什么;更不用说我做得是否正确了。在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的 C++/CLI 托管包装器项目中,添加 2 个文件:
一个 .c 文件:
一个 cpp 文件
in your C++/CLI managed wrapper project, add 2 files :
a .c file :
a cpp file
当您在 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.