简体   繁体   English

C ++ / CLI DLL中的本机代码

[英]Native code in C++/CLI DLL

I would like to be as close to real time as possible, so my intention is to have the most critical part of the code unmanaged (I don't want GC to block execution). 我希望尽可能接近实时,因此我的意图是使代码中最关键的部分不受管理(我不想让GC阻止执行)。 So I tried this: 所以我尝试了这个:

class Native 
{
public:
    int GetValue() 
    {       
        return 1;
    }
};

ref class Managed 
{
public:
    void Test() 
    {
        Native n;
        Console::WriteLine(n.GetValue());
    }
};

ILDASM shows Native::GetValue compiled to IL. ILDASM显示Native :: GetValue编译为IL。 Is it possible to keep this class/function unmanaged? 是否可以使此类/功能不受管理? Is a dedicated unmanaged dll the only way to do it? 专用的非托管dll是唯一的方法吗?

It looks like I need #pragma directives: 看来我需要#pragma指令:

#pragma unmanaged

class Native 
{
public:
    int GetValue() 
    {       
        return 1;
    }
};

#pragma managed

ref class Managed 
{
public:
    void Test() 
    {
        Native n;
        Console::WriteLine(n.GetValue());
    }
};

C++-CLI are managed extensions that allow you to directly use .NET functions such as Console.WriteLine that you have shown in your example. C ++-CLI是托管扩展,使您可以直接使用示例中显示的.NET功能,例如Console.WriteLine It is compiled into a managed assembly. 它被编译成托管程序集。 If you want unmanaged code you will have to instruct the compiler to produce unmanaged code. 如果要使用非托管代码,则必须指示编译器生成非托管代码。 But in this case obviously you will have to forget about the .NET functions. 但是在这种情况下,显然您将不得不忘记.NET函数。

Hm, I probably misunderstood you requirement but I am choking on the this: 嗯,我可能误解了您的要求,但我对此感到cho恼:

I don't want GC to block execution 我不想GC阻止执行

It depends on the interop scenario you are using, but in general you will not be able to prevent suspension of execution threads by simply making parts of application unmanaged. 这取决于您使用的互操作方案,但是通常,仅通过使应用程序的某些部分不受管理,就无法防止执行线程的挂起。 Any thread which has CLR roots along its call stack could be hijacked and halted by the GC, no matter if this thread just happens to execute "unmanaged" code. 无论调用线程是否恰好执行“非托管”代码,任何在调用堆栈中具有CLR根的线程都可以被GC劫持和暂停。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM