-3

EDIT: I want to implement C++ (with no limits, using intrinsics, inlines, optimizing), choose in the implementation what is visible on .NET (C# at least) and indeed use it, compiling from invisible only what is needed to visible resorces working.

Can we declare and use structures and classes in C# but implement them in C++ (or C++/Cli)?
Something like that with the reality adjustment, correct projets, settings...

First file. EDIT: Setting what is visible.

// 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 ) ;
}

Second file. EDIT: Setting what exists (visible, required invisible and unrequired).

// 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 ) ;
}

Third file. EDIT: using only visible resources, compiling only used visible (in case only one class method) and needed codes to visible code works. If dll, compiles all visible and all needed codes to it.

// 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 == ' ' ) ;
    }

}
      

Must we use C++/CLI? P/Invoke? Can't do with no dll?
What's the step-by-step? I find nothing so palpable.

RHER WOLF
  • 137
  • 10

1 Answers1

1

Generally, there are the following methods for accessing the C++ code from C#:

  • Compile C++ as unmanaged DLL and access using p/invoke. This requires the C++ code be exposed using a C style API.
  • Compile C++ as unmanaged DLL and access using COM. This requires that you wrap your C++ in as COM objects.
  • Compile C++ as mixed/mode C++/CLI assembly and access the assembly as a managed reference. This requires that you wrap the original C++ as managed C++ ref classes.

All of these options, by necessity, involve the creation of another module/assembly. You cannot link the C++ code directly into your C# assembly.

You could refer to this link for more information.

Barrnet Chou
  • 1,738
  • 1
  • 4
  • 7
  • I'm testing third option. – RHER WOLF Dec 29 '20 at 16:21
  • Is the third assembly only a executable to starts, not structs, classes, etc acessed by other way? – RHER WOLF Dec 29 '20 at 19:36
  • Could you elaborate on this sentence and explain your needs in details? – Barrnet Chou Dec 30 '20 at 02:26
  • I want to implement C++ (with no limits, using intrinsics, inlines, optimizing), choose in the implementation what is visible on .NET (C# at least) and indeed use it, compiling from invisible only what is needed to visible resorces working. – RHER WOLF Dec 30 '20 at 14:20
  • In my opiniom, the third method is best for you. After you wrap the original C++ as managed C++ ref classes, it is the same as calling the class in C# when you call it. – Barrnet Chou Dec 31 '20 at 05:26