Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using an example variable, I managed to access its memory address. Using Visual Studio Community's debugger mode, I can get the address, do a lookup, and verify that the address matches the value of the variable. So far, so good:
using System;
using System.Runtime.InteropServices;

namespace ConsoleApp1
    internal class Program
        static void Main(string[] args)
            string Value = "This is my string";          
            GCHandle Handle = GCHandle.Alloc(Value, GCHandleType.Pinned);
            IntPtr Object = Handle.AddrOfPinnedObject();

            IntPtr Pointer = new IntPtr((int)Object);            
            byte[] Data = new byte[(int)Object];
            Marshal.Copy(Pointer, Data, 0, 100);

What I have tried:

I tried to access the Data value, but to no avail. The Marshal couldn't understand in detail how it actually works or if it returns the data. Any suggestions on how to access this data, which in this case would be the value of the Value variable?
Grateful for the attention!
Updated 1-Jan-22 1:07am
PIEBALDconsult 1-Jan-22 12:30pm    
Use a fixed context?

1 solution

Um. GCHandle.AddrOfPinnedObject[^] returns a pointer to the object memory, which on a 64 bit system will be a 64 bit value.
int in C# is a 32 bit value, so casting a 64 bit pointer to an int will probably render it useless.

Plus, the value you get as an int is a partial address, not a size, so this line:
byte[] Data = new byte[(int)Object];
will generate a fairly random sized data object.

I'm not at all sure what you are trying to accomplish with this code, but I can be sure that isn't the right way to do it!
Share this answer
Jirandeio Tesrla 1-Jan-22 8:47am    
I understand. The objective is to return the memory address of this Value string (which is not the same every time the application is executed), then obtain the value of this string from the memory address and finally, try to change this value ("This is my string"). I'm trying to understand how this memory management part of Windows works. What would be the most suitable method to do this?
OriginalGriff 1-Jan-22 8:55am    
You cannot change a string value: they are immutable (and you will get some very strange effects later on if you try as they are cached to reduce duplication of memory). The only way to change it would be to use unsafe pointers, and really that isn't a good idea.

If you need to change the content, then either use Encoding.ASCII.GetBytes to generate a byte[] with a copy of the content, or use the string handling methods / regex to generate a new string.

Just because you can "play" with string content in some other languages doesn't mean it's something you need to do in C#! Why do you think you need to down this road?
Jirandeio Tesrla 1-Jan-22 9:57am    
It is not mandatory to follow this path, so much so that I asked for a suggestion of other ways to do this (I have no domain of C# or any other language). Using unsafe methods is another option, but I would like to avoid using that. Exploring other paths is also part of it.
OriginalGriff 1-Jan-22 10:24am    
So use the string class methods (Substring, IndexOf, Replace, and so on) to create a new string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900