How to implement in C++ and use in C# in VS?

Asked

Viewed 67 times

0

EDIT: I want to implement in C++ (no limitations, using intrinsics, inlines, optimizing and everything else normally), choose what in what has been implemented is visible in . NET (C# at least) and actually use it in C#, compiling only what is necessary (only visible used or, if dll, all visible and then from which is invisible only what is necessary for the functioning of the compiled visible).

Can I declare and use structures, classes, etc in C# but implement them in C++ (or C++/Cli)? Or something equivalent? Something like this to follow with adaptations to reality, projects all straight, configured...

First file. EDIT: determining 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: defining what exists (visible, invisible necessary and even invisible unnecessary).

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

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

__forceinline unsigned __int64 __generate( void *source ){
    unsigned __int64 state , temporary ;
    memmove( &state , source , 8 ) ;
    temporary = __emulu( (unsigned __int32)( state >> 32 ) , 0x5422A313u ) << 32 ;
    state = ( temporary | 0x1ABE623Eu ) + __emulu( (unsigned __int32)state , 0x5422A313u ) ;
    memmove( source , &state , 8 ) ;
    return state ;
}

__forceinline unsigned __int32 __randomize( void *source ){
    unsigned __int64 newstate = __generate( source ) ;
    return (unsigned __int32)( newstate >> 32 ) ;
}

template< typename type > __forceinline type __randomize( void *source , type minimal , type maximal ){
    if( minimal > maximal ){ minimal ^= maximal ; maximal ^= minimal ; minimal ^= maximal ; }
    unsigned __int32 difference = (unsigned __int32)( maximal-minimal ) ;
    unsigned __int64 newstate = __generate( source ) ;
    unsigned __int64 temporary1 = __emulu( difference , (unsigned __int32)newstate ) ;
    unsigned __int32 highnewstate = (unsigned __int32)( newstate >>= 32 ) ;
    temporary1 = (unsigned __int32)( temporary1 >> 32 ) + __emulu( difference , highnewstate ) ;
    unsigned __int64 temporary2 = (unsigned __int64)minimal ;
    temporary2 = highnewstate | ( temporary2 << 32 ) ;
    return (type)( ( temporary1 + temporary2 ) >> 32 ) ;
}

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 the necessary, visible ones used in the application (in this case only a class method) and codes necessary for the functioning of the visible code. If it is with dll, compile everything that is determined to be visible and everything that is necessary for it to work, nothing that is not.

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

}
      

Does it have to be with C++/CLI? Does it have to be with P/Invoke? It has to be with dll, never without? Does Com dll have a way to determine whether or not the dll will be statically embedded so that it is unnecessary (and no antivirus deletes it by mistake)? And how do you do step-by-step with this? From project creations and solutions to compilation. I couldn’t find anything very clear.

  • You can answer in English here: https://stackoverflow.com/questions/65484968

  • since you quoted the English OS you have many similar questions there, such as: https://stackoverflow.com/questions/13293888/how-to-call-a-c-sharp-library-from-native-cusing-c-cli-and-ijw

  • They said that over there, I’m looking at everything. If I can find the solution, I put myself here. I think it’s missing.

  • in general, the class needs to have the attributes "Comvisible" and "Guid" (before it was necessary to generate a key, but with Guid it is sufficient) and then import in c++

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.