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 5986515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:40:57+00:00 2026-05-22T22:40:57+00:00

Lets say we have a native library that works with data like this: double

  • 0

Lets say we have a native library that works with data like this:

 double *pointer = &globalValue; 
 SetAddress(pointer);

 //Then we can change value and write it to disk
 globalValue = 5.0;

 FlushValues(); // this function writes all values 
                // registered by SetAddress(..) functions

 ... //Then we change our value (or values)
 FlushValues(); //and do write any time we need

Now I have to interop it to C#. I would like to avoid using unsafe… but… I dont know =)

So on C# side we will have some class wich fields we will write. And we can do writing like:

 public class Data
 {
     [Serializable] <-- somehow we mark what we are going to write
     double myValue;

     ...

     [Serializable]
     double myValueN;
 }

 public class LibraryInterop
 {
      IntPtr[] pointers; //Pointers that will go to SetAddressMethod
      ...

      public void RegisterObject(Data data)
      {
          ... //With reflection we look for [Serializable] values
              //create a pointer for each and some kind of BindingInfo 
              //that knows connection of pointers[i] to data.myValueN

          //Then add this pointers to C++ library
          foreach(IntPtr pointer in pointers) { SetAddress(pointer);}
      }

      public void Flush() 
      {
          //Loop through all added pointers
          for(int i=0; i<pointers.Length; i++)
          {
              double value = ... //With reflections and BindingInfo we find data.myValueN
                                 // that corresponds to pointers[i]

              // then we write this value to memory of IntPtr
              // we have to brake value to bytes and write it one by one to memory
              // we could use BitConverter.DoubleToInt64Bits() but double - is just 
              // an example, so in general case we will use GetBytes
              int offset = 0;
              foreach(byte b in BitConverter.GetBytes(value))
              {
                  Marshal.WriteByte(pointer[i],offset,byte);
                  offset++;
              }
          }

          //Save values of IntPtr to disk
          FlushValues();
      }
  }

Then the user code looks like this then

  var library = new LibraryInterop();
  var data1 = new Data();
  var data2 = new AnotherData();
  library.RegisterObject(data1);
  library.RegisterObject(data2);

  ...//change data
  library.Flush();
  ...//change data
  library.Flush();
  ...//change data
  library.Flush();

So in C++ we have a very neat structure. We have pointers, we fill data behind this pointers and on FlushValues all this values are writed.

C# part cannot have SetAddress(ref double value). Since address may change, we have to pin pointers – use unsafe+fixed or IntPtr, and have SO many headache.

On the other hand, we can have “managed pointers” by boxing|unboxing data.myValue to Object. So if it would be possible to somehow bind this ValueType data.myValue to IntPtr without this coping and reflection – it would be much much neater.

Is it possible to do this interop and have less ugly and slow C# part then the one I listed here?

  • 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-22T22:40:57+00:00Added an answer on May 22, 2026 at 10:40 pm

    (There are some major downsides to this…)

    You can use GCHandle.Alloc(data1, GCHandleType.Pinned) to “pin” an object, and then get an IntPtr from GCHandle.AddrOfPinnedObject. If you do this, you’ll be able to pass this IntPtr to your native code, which should work as expected.

    However, this is going to cause a lot of issues in terms of undermining the efficiency of the garbage collector. If you decide to do something like this, I’d recommend allocating all of the objects very early on in your program (potentially immediately after calling a GC.Collect(), which is something I really don’t normally recommend), and leaving them alone for the lifetime of your program. This would (slightly) mitigate the GC issues, as it’d allocate them into the “best” possible spot early on, and leave them where they’ll only be touched in Gen2 collections.

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

Sidebar

Related Questions

Lets say I have an array like this: string [] Filelist = ... I
Lets say I have a p tag like this: <div id = example> <p
Lets say i have this in a shell chdir * && whoami.exe >> $$$
Lets say we have something like: <div class=row> <div class=box> <a class=more href=#more/> </div>
Lets say you have website that keeps balance for each user. You give each
Lets say I have a String: Go to this page: http://mysite.com/?page=1 , and I
Lets say you have 3 or 4 site features like photos, videos, wall posts,
Lets say I have this XML: <images> <photo> <thumbImg>images/thumbs/002.jpg</thumbImg> <filename>002</filename> </photo> <photo> <thumbImg>images/thumbs/008.jpg</thumbImg> <filename>008</filename>
Lets say I have 2 datasets: A and B, that have the same columns.
Lets say I have a property that IsRegistrationCompleted . I have 2 checkboxes in

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.