Results 1 to 1 of 1
  1. #1
    dwson
    dwson is offline
    Guest
    Join Date
    2013 Aug
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Using C++ classes in C# with C++/CLI

    1. Constructor and Destructor

    Code:
    //.h
        public ref class WrapperLibrary
        {
        public:
            WrapperLibrary();
            !WrapperLibrary();
            ~WrapperLibrary();
     
        internal: 
            CCppLibrary *u_cppLibrary;
        };
    Code:
    //.cpp
        WrapperLibrary::WrapperLibrary()
        {
            u_cppLibrary = new CCppLibrary();
        }
     
        WrapperLibrary::!WrapperLibrary()
        {
            delete u_cppLibrary;
        }
     
        WrapperLibrary::~WrapperLibrary()
        {
            this->!WrapperLibrary();
        }


    2. Inheritance

    Code:
    //.h
        public ref class WrapperLibrary
        {
     
            // ...
     
        internal:
            WrapperLibrary(CCppLibrary *u_cppLibrary);
            CCppLibrary *u_cppLibrary;
        };
     
        public ref class ChildWrapper : WrapperLibrary
        {
        public:
            ChildWrapper();
        };
    Code:
    //.cpp
        WrapperLibrary::WrapperLibrary(CCppLibrary *u_cppLibrary)
        {
            this->u_cppLibrary = u_cppLibrary;
        }
     
        ChildWrapper::ChildWrapper()
            : WrapperLibrary(new CChildCpp())
        {
        }

    3. String Argument

    Code:
    //.cpp
        String ^WrapperLibrary::AppendHello(String ^str)
        {
            msclr::interop::marshal_context context;
     
     
            const char *u_str = context.marshal_as<const char *>(str);
     
     
            std::string result = u_cppLibrary->AppendHello(u_str);
     
     
            String ^ret = context.marshal_as<String  ^>(result);
     
     
            return ret;
        }

    4. Function Pointer Argument

    Code:
    //.h
     
        // ...
     
        using namespace System::Runtime::InteropServices;
     
        // ...
     
        [UnmanagedFunctionPointerAttribute(CallingConvention::Cdecl)]
        public delegate void Callable();
     
        // ...
     
        public ref class WrapperLibrary
        {
            // ...
     
        public:
      
            void Call(Callable ^callable);
      
            // ...
        };
    Code:
    //.cpp
        void WrapperLibrary::Call(Callable ^callable)
        {
            void (*u_callable)() = callable == nullptr ? NULL : (void (*)()) Marshal::GetFunctionPointerForDelegate(callable).ToPointer();
      
            u_cppLibrary->Call(u_callable);
        }

    5. Variable Argument Function

    Code:
    //.cpp
     
    struct FakeValist
    {
        // NOTE : the number of arguments is limited
        static const int COUNT = 100;
        double list[COUNT];
    };
     
     
    void GetNumbers(FakeValist *fakeValist, ...array<double> ^numbers)
    {
        int count = numbers->GetLength(0);
     
     
        for (int i = 0; i < count; i++)
        {
            fakeValist->list[i] = numbers[i];
        }
    }
     
    // ...
     
        double WrapperLibrary::Average(...array<double> ^numbers)
        {
            int count = numbers->GetLength(0);
            FakeValist u_numbers;
            GetNumbers(&u_numbers, numbers);
     
            double ret = u_cppLibrary->Average(count, u_numbers);
     
            return ret;
        }

    5-1. Variable Argument Function ( String Formatting )

    Code:
    //.cpp
        void WrapperLibrary::Printf(String ^format, ...array<Object ^> ^args)
        {
            msclr::interop::marshal_context context;
     
            String ^formatted = String::Format(format, args);
            const char *u_formatted = context.marshal_as<const char *>(formatted);
     
            u_cppLibrary->Printf("%s", u_formatted);
        }

    6. Export Unmanaged Member

    Code:
    //.h
     
    //...
     
    class CCppLibrary;
    #pragma make_public(CCppLibrary)
     
    //...
        public ref class WrapperLibrary
        {
            //...
     
        public:
    ​        WrapperLibrary(CCppLibrary *u_cppLibrary);    // Used in OtherCliLibrary. Turned private if 'make_public' not exist
     
        //...

    Please register or login to download attachments.

    Last edited by dwson; 2013-10-10 at 09:01 AM.

Similar Threads

  1. [C#] Using C++ classes in C# with C++/CLI
    By dwson in forum VB, .NET Framework
    Replies: 0
    Last Post: 2013-08-21, 11:09 AM
  2. [Guide] [Guide] Classes and Races.
    By jimmis in forum Lineage II Bots, Hacks, Cheats
    Replies: 0
    Last Post: 2013-07-29, 05:23 PM
  3. Raças e Classes AIKA
    By BeNDeR in forum Português
    Replies: 3
    Last Post: 2013-05-03, 02:11 PM
  4. [Dev] GF - Classes (15-10-12)
    By freitag in forum Grand Fantasia
    Replies: 1
    Last Post: 2012-12-15, 10:37 PM
  5. [Guide] Aika Classes
    By jujuba01 in forum Aika Guides, Tutorials
    Replies: 0
    Last Post: 2010-11-22, 02:28 AM
Visitors found this page by searching for:
Nobody landed on this page from a search engine, yet!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •