-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.

4

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm testing third option.
Is the third assembly only a executable to starts, not structs, classes, etc acessed by other way?
Could you elaborate on this sentence and explain your needs in details?
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.