【问题标题】:Using Thread in in Mixed App C++ /cli to Call native Function in native class from managed class在混合应用程序 C++ /cli 中使用线程从托管类调用本机类中的本机函数
【发布时间】:2018-05-18 16:29:55
【问题描述】:

我正在开发混合应用程序,同时使用托管代码和本机代码 我想从位于托管类 Program.cpp 中的 Main() 函数调用部署在本机类中的函数。

我尝试使用 std::thread 但使用 /cli 失败

我尝试使用 Managed System::Threading::Thread 但失败了,因为我需要在本地类中调用本地函数。

那么我如何在不使用任何第三方的情况下处理事情呢?

【问题讨论】:

  • 正式verboten,托管代码不保证在操作系统线程上运行。您必须修改 .h 文件,以便 C++/CLI 编译器看不到 std::thread 成员。考虑一个接口或 Pimpl。
  • 但我正在开发 API,它应该是原生的,而且我的应用程序类都是托管的,所以我尝试使用接口但编译器拒绝包括 ...
  • 当您的 .h 文件不再包含 std::thread 时,您也不必#include 该文件。并确保编译原生代码没有 /clr 生效,静态库项目可以轻松地对代码进行分区。
  • 那么目前情况下没有解决方案?

标签: multithreading c++-cli


【解决方案1】:

如果您从原生项目开始,您需要执行以下步骤:

  • 选择项目属性并将选项“No Common Language Runtime Support”更改为“Common Language Runtime Support /clr”。
  • 从“查看”菜单/“其他窗口”打开“属性管理器”,并将属性表“C++ 公共语言运行时支持”添加到我的系统上所需的配置(例如:调试 | Win32),此表位于此表下“C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140”。我使用“Microsoft.Cpp.ManagedExtensions.props”文件。
    • 您需要完全删除 std::thread。

标题:

#pragma once
#include<stddef.h>

using namespace System;
using namespace System::Threading;

namespace FooSpace
{
    // Native stuff 
    public class Native
    {
    public:
        static void Foo() { }
        void Bar() {
        }
    };

    // Managed stuff
    public ref class Managed
    {
    private:
        Native* m_Native;

    public:
        Managed()
        {
            m_Native = new Native();
        }

        ~Managed()
        {
            if (NULL != m_Native)
            {
                delete m_Native;
                m_Native = NULL;
            }
        }

        void Bar()
        {
            m_Native->Bar();
        }

        static void ThreadCall(Object^ o)
        {
            auto me = (Managed^)o;
            me->Bar(); // Call a method of an instance of the native class
            Native::Foo(); // Call a static method of the Native class
        }

        void StartThread()
        {
            auto t = gcnew Thread(gcnew ParameterizedThreadStart(ThreadCall));
            t->Start(this);
            t->Join();
        }
    };
}

原文件:

#include "stdafx.h"
#include "CppCli_Native.h"

using namespace FooSpace;

int main()
{
    Native::Foo(); // call native static method
    auto native = new Native(); // create native instance
    native->Bar(); // call native method

    auto managed = gcnew Managed();
    managed->Bar(); // This will call bar
    managed->StartThread(); // This will start a thread
    delete managed;

    Console::ReadLine();
    return 0;

}

编辑:事实证明,您不需要使用IntPtr 来存储本机类。

我发现this 的答案也很有用,它还让我们快速了解了 c++-cli 语法。

【讨论】:

  • 我今天要试试这个,我希望这样的东西能解决我的问题。
  • @AliAl-Masry 如果你发现一些额外的点,请给我反馈,我们可以为未来的探索者改进这个答案。
  • 当然,但我想通知您,我需要从 Program.cpp Main void 调用本机函数。
  • @AliAl-Masry:你能纠正这个问题吗?该主题讲述了从托管线程调用本机函数!
  • 是的,Program.cpp -> Main void 是 Managed ,因为我使用 Common Language Runtime Support /clr
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多