如何在C#中声明并在VS中用C++实现?

问题描述

编辑:我想实现 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,8 ) ;
}
Random::Random( unsigned __int64 seed ){
    memmove( this,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,maximal ) ;
}

unsigned __int32 Random::randomize( unsigned __int32 minimal,unsigned __int32 maximal ){
    return __randomize( this,maximal ) ;
}
unsigned __int32 Random::Randomize( unsigned __int32 minimal,unsigned __int32 maximal ){
    return __randomize( &__global,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++代码有以下几种方法:

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

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...