使用 C++-cli,如何实例化 C# 库中定义的 WinForms 表单?
如果这个问题已经得到解答,但我找不到它,我很抱歉。
我试图在调用 C++ 程序中的函数时打开 C# 表单(主程序在 C++-cli 中,表单在 C# 中,它是一个空表单,只是为了尝试它是如何工作的)。 我正在使用 Visual Studio 2005,并且我的解决方案中有这两个项目。 C# 项目只是一个表单,我已将其配置为类库。 然而,当我进入 C++ 程序的函数时,我在开始时输入:
int _stdcall Init(void)
{
...
FormProject::Form1 form1;
form1 = new FormProject::Form1::Form1();
form1.something();
...
}
我在 C++ 方面经验很少,我尝试过不同的组合,但我很不幸。 C++ 项目已与 CLI 兼容。 我已经包含了所需的内容:
#using <System.Windows.Forms.dll>
#using <System.dll>
我知道存在一些大错误,但我只需要运行此表格,不再需要。 非常感谢。
编辑:我已经添加了参考文献。
I'm sorry if this question has already been answered but I couldn't find it.
I am trying to open a C# form when a function in a C++ program is called (the main program is in C++-cli, the form is in C#, it is an empty form just to try how it works). I am using Visual Studio 2005 and I have both projects in my solution. The C# project is just a form and I have configured it as a class library. However, when I go to the C++ program's function I type this in the beginning:
int _stdcall Init(void)
{
...
FormProject::Form1 form1;
form1 = new FormProject::Form1::Form1();
form1.something();
...
}
I have little experience in C++, I have tried different combinations but I am unlucky. The C++ project has been made compatible with CLI. I have already included the needed:
#using <System.Windows.Forms.dll>
#using <System.dll>
I am aware there are some big mistakes but I only need this form running, no more. Thank you very much.
Edit: I have already added the references.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解你的意思,我认为你需要使用 C++/CLI 的语法增强来实例化 C# 对象。 例如,您发布的代码应该是:
我建议您在托管 C++ 中创建一个新的 WinForms 项目,然后查看生成的代码以获取正确的语法。
If I understand you, I think you need to instantiate the C# object using C++/CLI's syntax enhancements. For example, the code you posted should be:
I suggest you create a new WinForms project in managed C++, and just look at the generated code for the correct syntax.
C++/CLI 项目需要添加对 C# 项目的引用,然后才能使用 C# 库中的类型。
The C++/CLI project will need to add a reference to the C# project before you can use types in your C# library.
您是否已将 FormProject 的引用添加到您的 C++ 项目中? 如果您已经这样做了,您是否包含了 FormProject 命名空间的 #using 语句?
Have you added a reference to the FormProject to your C++ project? If you've done that, have you included a #using statement for the FormProject namespace?