C++ 中的 C# 形式 程序
在我的上一个问题中,我询问了有关在 C++-cli 程序中运行 C# 表单的问题。 我可以正常工作,但我遇到了问题。 我会尽量简短。
我的 C++ 程序必须执行 C# 表单并执行其中的一些简单函数(增加计数器并显示它们)。 但是我不知道最好的方法来做到这一点。 我在 init 函数中初始化了表单:
C++-cli
SUTAdapter::Form1^ *ptForm1; // Global variable
...
FormProject::Form1^ form1;
form1 = gcnew FormProject::Form1();
ptForm1 = &form1;
(*ptForm1)->Show();
(*ptForm1)->incCounter(0);
C++ 程序中的一些其他函数只调用 incCounter。 我的问题是,从另一个函数对 incCounter 的第二次调用使我的 C# Form1 null (this == null),因此我可以使用 incCounter 的函数代码,但不能使用类变量。 很奇怪,好像程序处置了FormProject。
C#
public void incCounter(int counter)
{
int param1 = counter;
this.count[counter]++; // this == null in sucessive calls from c++ program
}
我做错了什么? 我实际上已经禁用了表单,只使用函数和变量,以防 UI 出现问题(调用等)。 退出 C++ init 函数(第一块代码)是否会清除 Form1?
In my last question I asked about running a C# form in a C++-cli program. I got it working, but I am having problems. I will try to be brief.
My C++ program must execute a C# form and execute some simple functions in it (increasing counters and showing them). However I do not know the best way to do this. I have the form initialized in an init function:
C++-cli
SUTAdapter::Form1^ *ptForm1; // Global variable
...
FormProject::Form1^ form1;
form1 = gcnew FormProject::Form1();
ptForm1 = &form1;
(*ptForm1)->Show();
(*ptForm1)->incCounter(0);
Some other functions in the C++ program just call incCounter. My problem is, that a second call from another function to incCounter makes my C# Form1 null (this == null), so I can use the function code of incCounter but not the class variables. It is strange, as if the program disposed the FormProject.
C#
public void incCounter(int counter)
{
int param1 = counter;
this.count[counter]++; // this == null in sucessive calls from c++ program
}
What am I doing wrong? I have actually disabled the form and just using the function and variables in case the problem is with the UI (invoke and so). Is exiting the C++ init function (the first chunk of code) clearing the Form1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是 *ptForm1 不足以让 CLR 将其计为引用。 也许应该是 Form1^^ ptForm? 或者为什么不直接将 form1 变量作为全局指针插入?
My guess would be that *ptForm1 isn't sufficient to make the CLR count it as a reference. Maybe it should be Form1^^ ptForm, instead? Or why aren't you just putting the form1 variable as you global insted of a pointer?
我认为您应该将句柄存储在全局(即静态)对象中,或者至少存储在方便使用的类的静态字段中。 正如 danbystrom 所说,保留指向 Form 引用的指针不会阻止垃圾收集器在原始句柄变为空后回收表单。
尽管我怀疑 Form 引用变为 null 也意味着其他问题。
I think you should just be storing the handle in a global (i.e. static) object, or at least a static field of a convenient class to use. Like danbystrom said, keeping a pointer to the Form reference will not prevent the garbage collector from reclaiming the form once the original handle goes null.
Though I suspect the Form reference going null means something else is wrong too.
如果必须获取托管堆上 .NET 对象的地址,请将其放入 pin_ptr<>,这样 GC 就不会移动它。 这可能是你的问题的根源。
If you must take the address of a .NET object on the managed heap, put it in a pin_ptr<> so that the GC does not move it. That could be the source of your problem.