【问题标题】:How to declare in C# and implement in C++ in VS?如何在 VS 中用 C# 声明并用 C++ 实现?
【发布时间】:2021-04-05 15:49:05
【问题描述】:

编辑:我想实现 C++(没有限制,使用内在函数、内联、优化),在实现中选择 .NET 上可见的内容(至少 C#)并确实使用它,仅从不可见编译可见资源工作需要什么。

我们可以在 C# 中声明和使用结构和类,但在 C++(或 C++/Cli)中实现它们吗?
现实调整,正确的项目,设置......

第一个文件。 编辑:设置可见内容。

// Random.cs
// Declarations

[StructLayout( Size=8 )] struct Random {
    public Random() ;
    public Random( long Seed ) ;
    public Random( ulong Seed ) ;
    public uint randomize { get ; }
    public static uint Randomize { get ; }
    public int randomize( int Minimal , int Maximal ) ;
    public uint randomize( uint Minimal , uint Maximal ) ;
    public static int Randomize( int Minimal , int Maximal ) ;
    public static uint Randomize( uint Minimal , uint Maximal ) ;
}

第二个文件。 编辑:设置存在的内容(可见的、需要的、不可见的和不需要的)。

// Random.cpp
// Definitions
// +Intrinsics
// +Inlines

# include <intrin.h>
# include <memory.h>

__forceinline unsigned __int64 __generate( void *source ){
    // Implementation
}

__forceinline unsigned __int32 __randomize( void *source ){
    // Implementation
}

template< typename type > __forceinline type __randomize( void *source , type minimal , type maximal ){
    // Implementation
}

Random __global ;

Random::Random(){
    unsigned __int64 seed = __rdtsc() ;
    memmove( this , &seed , 8 ) ;
}
Random::Random( __int64 seed ){
    memmove( this , &seed , 8 ) ;
}
Random::Random( unsigned __int64 seed ){
    memmove( this , &seed , 8 ) ;
}

unsigned __int32 Random::randomize::get {
    return __randomize( this ) ;
}
unsigned __int32 Random::Randomize::get {
    return __randomize( &__global ) ;
}

__int32 Random::randomize( __int32 minimal , __int32 maximal ){
    return __randomize( this , minimal , maximal ) ;
}
__int32 Random::Randomize( __int32 minimal , __int32 maximal ){
    return __randomize( &__global , minimal , maximal ) ;
}

unsigned __int32 Random::randomize( unsigned __int32 minimal , unsigned __int32 maximal ){
    return __randomize( this , minimal , maximal ) ;
}
unsigned __int32 Random::Randomize( unsigned __int32 minimal , unsigned __int32 maximal ){
    return __randomize( &__global , minimal , maximal ) ;
}

第三个文件。 编辑:仅使用可见资源,仅编译使用的可见资源(如果只有一种类方法)并且需要将代码转换为可见代码。如果是 dll,则将所有可见的和所有需要的代码编译到它。

// VCsTester.cs
// Application
using System ;

class VCsTester {

    static void Main( string[] args ) {
        do {
            Console.Write( "\b \b\b \b\b \b\b \b\b \b\b\b \b\b \b\b\b \b\b \b\b \b\b\b \b\b \b\b \b\b \b\b \b\b\b \b\b \b\b \b\b \b\b \b\b\b \b\b \b\b \b\b \b\b \b\b \b\b \b\b\b \b\b \b\b\b \b\b \b\b \b\b \b\b \b\b\b \b\b \b\b \b\b \b\b \b" ) ;
            Console.WriteLine( " Random Value = " + Random.randomize ) ;
            Console.Write( "Press space to repeat. Press other key to quit." ) ;
        } while( Console.ReadKey( true ).KeyChar == ' ' ) ;
    }

}
      

我们必须使用 C++/CLI 吗? P/调用?没有dll不行吗?
步骤是什么?我发现没有什么比这更明显的了。

【问题讨论】:

标签: c# c++ visual-studio c++-cli pinvoke


【解决方案1】:

一般来说,从C#访问C++代码有以下几种方法:

  • 将 C++ 编译为非托管 DLL 并使用 p/invoke 进行访问。这需要 使用 C 风格的 API 公开 C++ 代码。
  • 将 C++ 编译为非托管 DLL 并使用 COM 访问。这就要求 您将 C++ 包装为 COM 对象。
  • 将 C++ 编译为 mixed/mode C++/CLI assembly 并以以下方式访问程序集 托管参考。这要求您将原始 C++ 包装为 托管 C++ 引用类。

所有这些选项都必然涉及创建另一个模块/程序集。您不能将 C++ 代码直接链接到您的 C# 程序集中。

您可以参考此link 了解更多信息。

【讨论】:

  • 我正在测试第三个选项。
  • 第三个程序集是否只是启动的可执行文件,而不是通过其他方式访问的结构、类等?
  • 您能否详细说明这句话并详细说明您的需求?
  • 我想实现 C++(没有限制,使用内在函数、内联、优化),在实现中选择在 .NET 上可见的内容(至少是 C#)并确实使用它,仅从不可见进行编译可见资源工作需要什么。
  • 在我看来,第三种方法最适合您。把原来的C++包装成托管的C++引用类后,调用的时候就和C#中的类调用一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
相关资源
最近更新 更多