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

  • SEARCH
  • Home
  • 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 7934753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:43:40+00:00 2026-06-03T21:43:40+00:00

I have these declarations in C++: struct objectStruct; int positionMemory = getPosition(); short size

  • 0

I have these declarations in C++:

struct objectStruct;

int positionMemory = getPosition();

short size = getSize();

void *allocatedObject; // Originally, it is in C#: IntPtr allocatedObject { get; private set; }

byte[] byteName = Encoding.ASCII.GetBytes("Hello There");

I want to convert these code lines from C# to C++:

string result = Marshal.PtrToStringAnsi(new IntPtr(positionMemory), size); 

Marshal.StructureToPtr(objectStruct, new IntPtr(positionMemory), true);

Marshal.Copy(byteName, 0, new IntPtr(positionMemory), size);

long posInMemory = allocatedObject.Offset(size).ToInt64();

I am not familiar with Marshaling.

  • 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-06-03T21:43:42+00:00Added an answer on June 3, 2026 at 9:43 pm

    I don’t know C++ but I do know marshalling so here’s what the lines are doing

    //Get size number of characters of the string pointed to by the positionMemory pointer.
    string result = Marshal.PtrToStringAnsi(new IntPtr(positionMemory), size); 
    
    //Copy the contents of objectStruct to the memory location pointed at by positionMemory
    Marshal.StructureToPtr(objectStruct, new IntPtr(positionMemory), true);
    
    //Copy size number of bytes from the byteName array starting at index 0 to the memory indicated by positionMemory
    Marshal.Copy(byteName, 0, new IntPtr(positionMemory), size);
    
    //I think offsetting the memory location indicated by allocatedObject by size number of bytes and converting the memory pointer to an Int64.
    machineNamePosInMem = allocatedObject.Offset(size).ToInt64();
    

    I can’t see why you’d actually need to convert most of this to C++, the point of Marshalling is about making managed objects available to unmanaged code and converting unmanaged objects into managed ones, which if you’re operating all in C++ you shouldn’t really need to do, even if it’s managed C++.

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

Sidebar

Related Questions

I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
I have these two structures... typedef struct{ MY_SECOND_STRUCT s1; }MY_FIRST_STRUCT; typedef struct{ int s1;
I have this declaration struct Z { void operator ()( int a ) {
If I have these declarations and definitions: enum Events { INIT, RENDER }; struct
These are my declarations. Why it doesn't identify signed as a type? I have
I have these Enum declarations: enum MessageType{ REQ_LOGIN, REQ_GET_FIELD, RES_LOGIN, RES_GET_FIELD } enum Request{
I have these declarations (DLL) and tried to convert it in C# so i
I have the following struct: public struct Declarations { public const string SchemaVersion =
I have these 3 icons enclosed in separate DIVs all of which are enclosed
I have these two modules : package G1; sub new { my $class =

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.