Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:56:45+00:00 2026-05-28T22:56:45+00:00

I am trying to port a native C++ library to C++/CLI, so I can

  • 0

I am trying to port a native C++ library to C++/CLI, so I can use it in C#. I am trying to figure out how to convert a few of the classes that basically lightly wrap a buffer for extracting fields. I’ve written a small sample to better describe what is going on in the API.

So, I have a Packet class that reads packets from a file:

#include <fstream>
#include <cassert>
#include "OptionalA.h"

class Packet
{
public:
    Packet(void)
    {
        buffer = new char[PACKET_SIZE];
    }
    ~Packet(void)
    {
        delete [] buffer;
    }

    void ReadNextPacket(std::ifstream& fileStream)
    {
        fileStream.read(buffer, PACKET_SIZE);
    }

    bool HasOptionalA()
    {
        return (buffer[0] & 0x1) == 1;
    }

    OptionalA GetOptionalA()
    {
        assert(HasOptionalA());
        return OptionalA(&buffer[1], &buffer[1] + OptionalA::OPTIONAL_A_SIZE);
    }

private:
    const static int PACKET_SIZE = 3;
    char* buffer;
};

The Packet I created has a very simple format:

[Packet: <Header Byte> [OptionalA: <Field1><Field2>]]

The OptionalA class is defined as follows:

#pragma once
#include <cassert>

class OptionalA
{
public:
    static const int OPTIONAL_A_SIZE = 2;

    OptionalA(const char* begin = 0, const char* end = 0) :
    begin(begin), end(end)
    {
        assert((end - begin) == OPTIONAL_A_SIZE);
    }

    char GetField1()
    {
        return begin[0];
    }

    char GetField2()
    {
        return begin[1];
    }

private:
    const char* begin;
    const char* end;
};

What is the correct way to wrap this type of class architecture such that I duplicate the minimum amount of memory in C++/CLI?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:56:47+00:00Added an answer on May 28, 2026 at 10:56 pm

    a simple way, but somewhat time consuming depending on the amount of classes you have to convert, is to create CLI wrappers that contain a native pointer to the type they wrap:

    ref class FileStream
    {
    public:
      FileStream( String^ file ) :
        p( new std::ifstream() )
      {
        //open the stream here, if it fails throw a managed exception that makes sense
      }
    
        //bonus: CLI classes automatically implement IDisposable so 
        //this gets called at the end of scope with 'using( var x = new FileStream(){}'
      ~FileStream()
      {
        delete p;
      }
    
        //make sure to implement a finalizer to make the GC work with this class
      !FileStream()
      {
        this->~FileStream();
      }
    
      //not repeating all functions here, we just use this as a placeholder
      std::ifstream& Stream()
      {
        return *p;
      }
    
    private: 
      std::ifstream* p;
    }
    
    ref class Packet
    {
    public:
      Packet() :
        p( new native::Packet() )
      {
      }
    
      //again destructor/finalizer pair
    
      void ReadNextPacket( FileStream^ fileStream )
      {                   
        if( fileStream == nullptr )
          throw gcnew System::ArgumentNullException( "fileStream" );
        p->ReadNextPacket( fileStream->Stream() );
      }                   
    
      bool HasOptionalA()
      {
        return p->HasOptionalA();
      }
    
      OptionalA^ GetOptionalA()
      {
        retrun gcnew OptionalA( p->GetOptionalA() );
      }
    
    private:
      native::Packet* p;
    };
    
    ref class OptionalA
    {
    public:
      OptionalA( const native::OptionalA& optionalA ) :
        p( new native::OptionalA( optionalA ) )
      {
      }
    
      //again destructor/finalizer pair
    
      char GetField1()    
      {    
        return p=>GetField1();   
      }    
    
    private:
      native::OptionalA* p;
    }
    

    To spice things up and make better code, consider using something like this instead of raw native pointers and get cleanup for free. Also maybe consider using std::string and passing iterators around instead of raw char pointers, it’s C++ after all.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to port a SIP stack library (pjSIP) to the PSP Console
I am trying to build a native client module that uses the improv lib
I am trying to use ServerSocket with port 2649, and other people cannot connect.
I am trying to use a C++ dll from a native program. I am
I am trying to make a simple app that read from a midi port
Hi im trying to read com port, so I add library to my java
I am trying to port some software to native-client. It uses some inline asm
I'm trying to port some code to 64-bit, but it seems that the thread
I am trying to port a php file that is used with MySQL. My
I'm trying to port an application to Mono, however Mono doesn't support Application Settings.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.