【问题标题】:Visual Studio 2010 C++ forms application creates pointers variables instead of regular variablesVisual Studio 2010 C++ 表单应用程序创建指针变量而不是常规变量
【发布时间】:2014-10-03 15:08:08
【问题描述】:

我正在尝试将我的应用程序正在启动的进程的控制台输出重定向到文本框。我正在关注this 示例。

这是我目前所拥有的:

private: System::Void button_backup_Click(System::Object^  sender, System::EventArgs^  e) {

                 Process^ childprocess = gcnew Process();
                 childprocess->StartInfo = gcnew ProcessStartInfo("cmd.exe");
                 childprocess->StartInfo->UseShellExecute = false;
                 childprocess->EnableRaisingEvents = true;
                 childprocess->StartInfo->CreateNoWindow = true;
                 childprocess->ErrorDataReceived += gcnew DataReceivedEventHandler(process_DataReceived);
                 childprocess->OutputDataReceived += gcnew DataReceivedEventHandler(process_DataReceived);
                 childprocess->Start();
                 childprocess->WaitForExit();
}

第一个问题: 由于几天前我开始学习 C++ Windows Forms 应用程序,所以我一直无法使用包含点运算符的代码。我总是不得不用 -> 替换所有内容。例如,在前面的代码块中,如果我尝试:

Process process = new Process();

就像教程中写的一样,我收到错误 C2750: 'System::Diagnostics::Process' : cannot use 'new' on the reference type;请改用“gcnew”。

当我使用表单 GUI 创建一些对象时也会发生这种情况。比如说一个组合框。要设置我需要做的项目:comboBox->Items->Add。使用点它不会编译并给出错误,但我看到的大多数教程都有点而不是 ->。为什么我的 Visual Studio 没有生成可以处理点的代码?

第二个问题: 我想完成我链接到的示例。我需要实现

 void process_DataReceived(object sender, DataReceivedEventArgs e)
  {
   richTextBox.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action( delegate()
   {
    richTextBox.AppendText(e.Data);
   })
   );
  }

但它根本不起作用。我把这段代码放在前一个块之后,它本身就在 Visual Studio C++ 生成的#pragma endregion 之后(这是 Visual Studio++ 放置事件代码的地方)。

我注意到我的 Visual Studio C++ 生成的参数列表如下所示:

(System::Object^  sender, System::EventArgs^  e) 

而在示例中:

(object sender, DataReceivedEventArgs e)

为什么格式不一样?

谢谢。

【问题讨论】:

  • 该示例是用 C# 编写的。您正在使用 C++/CLI。这些看起来有误导性的相似性,但它们是非常不同的,只是表面相关的语言。
  • 是的,这不是 C++,也不是指针。标记已更改。

标签: winforms visual-studio-2010 visual-c++ c++-cli


【解决方案1】:
(System::Object^  sender, System::EventArgs^  e) 

那是一种叫做 C++/CLI (Common Language Infrastructure) 的语言。它是直接使用 CLR 的 C++ 的改编版本。它不是 C++。您可以创建可由任何其他 CLR 语言本机使用的模块,但代码(至少,ref 类)被编译为 MSIL,而不是本机机器代码。

(object sender, DataReceivedEventArgs e)

那是 C#。这两个签名在语义上是相同的(嗯,除了 EventArgs 参数的类型之间的差异),但语言不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 2018-11-01
    • 2012-02-12
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多