尝试转换非托管 C++ 时出现奇怪的错误托管 C++ 类类(用于.net)
您好,
首先,我不是 C++ 开发人员,所以请原谅我的缺点......
我正在尝试采用另一个开发人员的 C++ 非托管代码并重新编写它,以便可以从我们的 c#.net 的托管 DLL 中调用它应用。请记住,我是一名 .net 开发人员,并且我已经有大约 12 年没有接触过 C++ 了。当我这样做的时候……我做了一个石头剪刀布的游戏。 :(
不管怎样...我在论坛上发现了另一位开发人员的一个不错的小注释,建议我们继续使用“Public __gc”向 .h 文件中的类添加垃圾收集器。好吧,我有两个类我试图转换为托管代码,所以我继续这样做...其中一个立即起作用了!另一个...好吧,抛出此错误:
'无法从 'char * 转换参数 5 __gc *' 到 'char **'
好的,所以我知道这与指向更多指针和其他内容的指针有关......但这里是当垃圾收集器不工作时确实起作用的变量t 添加到类名中,现在不会添加它:
<<< HEADER FILE >>
public __gc class bob; { ... 民众: 字符*img_buff; ... int 初始化(无效); }
<<<< CPP 文件 >>>
int 初始化(无效) { ... nRet = is_AllocImageMem(hcam, 图像宽度, img_hgt, 深度, &img_buff,<<不喜欢这里 &mem_id); ... ?
有什么建议或想法吗 我只是对“*”和&了解不够。还有 C++ 中的指针之类的东西。
提前致谢。最好的祝愿!!!
Greetings,
First off, I am not a C++ developer, so please forgive my shortcomings....
I am attempting to take another developer's C++ unmanaged code and re-work it so it can be called from a managed DLL from our c#.net application. Please keep in mind that I'm a .net developer, and I haven't touched C++ in like 12 years. And when I did... I made a rock-paper-scissor game. :(
Anyway... I found a nice little note from another developer on a forum that suggested we go ahead and add a garbage collector to the class in the .h file with "Public __gc". Well, there are two classes I'm trying to convert to managed code, so I went ahead and did that... and one of them just instantly worked! The other... well, is throwing this error:
'cannot convert parameter 5 from 'char *__gc *' to 'char **'
Ok, so I know this has something to do with pointers pointing to more pointers and stuff... but here are the variables in question that DID work when the garbage collector wasn't added to the class name, and now won't build with it added:
<<< HEADER FILE >>>
public __gc class bob
{
...
public:
char *img_buff;
...
int init(void);
}
<<< CPP FILE >>>
int init(void)
{
...
nRet = is_AllocImageMem(hcam,
img_wid,
img_hgt,
depth,
&img_buff, << doesn't like it here
&mem_id );
...
}
Any suggestions or ideas? I just don't know enough about "*" and & and pointers and stuff in C++.
Thanks in advance. Best wishes!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您拥有什么版本的 Visual Studio?
__gc
是托管 C++,这很糟糕。您需要 C++/CLI,其中的神奇词是public ref class
。尽管如此,无论您以哪种方式执行此操作,您都必须稍微了解指针,因为您将有代码显示“我采用本机指针”,并且您打算将其托管引用传递给它垃圾收集堆上的东西。您必须更改该代码才能使其正常工作。我非常确定使用 PInvoke 对您来说是一个更好的计划,因为它不需要对 C++ 代码进行太多调整。如果团队中有 C++ 专业知识,我的答案会有所不同。
What version of Visual Studio do you have? The
__gc
stuff is Managed C++, which was awful. You want C++/CLI, where the magic words arepublic ref class
. Still, no matter which way you do this, you're going to have to understand pointers a little bit, because you're going to have code that says "I take a native pointer" and you intend to hand it a managed reference to something on the garbage collected heap. You'll have to change that code to make it work.I am pretty sure using PInvoke is going to be a better plan for you because it won't require as much tweaking of the C++ code. If you have C++ expertise on the team my answer would be different.