How to assign a value to memory pointed by an IntPtr

A

Andrew Falanga

Hi,

In an earlier post, I was wondering why I can't compile unsafe code
*EVEN* when the, "Allow unsafe code," check box is checked. I'm still
working on that one. However, the whole problem can be averted if
someone here can explain how I might assign a value to memory pointed
to by an IntPtr object. Using the language that I know (C++) to
illustrate, this is what I want to do with an IntPtr object:

int *pInt = new int;
*pInt = 5;

My C# code is:

IntPtr dataSize = Marshal.AllocHGlobal(sizeof(int));
dataSize = ???????

So how do I assign a value to the memory just allocated?

This particular parameter is being passed to a p/invoke call and is an
in/out parameter. So far, using a standard C# int object and
referencing it as an "out" object in the definition of the p/invoke
function seems to be producing some undesired behavior. That's the
background as to why I need to do this.

I appreciate any help,
Andy
 
A

Andrew Falanga

Hi,

In an earlier post, I was wondering why I can't compile unsafe code
*EVEN* when the, "Allow unsafe code," check box is checked.  I'm still
working on that one.  However, the whole problem can be averted if
someone here can explain how I might assign a value to memory pointed
to by an IntPtr object.  Using the language that I know (C++) to
illustrate, this is what I want to do with an IntPtr object:

int *pInt = new int;
*pInt = 5;

My C# code is:

IntPtr dataSize =  Marshal.AllocHGlobal(sizeof(int));
dataSize = ???????

So how do I assign a value to the memory just allocated?

This particular parameter is being passed to a p/invoke call and is an
in/out parameter.  So far, using a standard C# int object and
referencing it as an "out" object in the definition of the p/invoke
function seems to be producing some undesired behavior.  That's the
background as to why I need to do this.

I appreciate any help,
Andy

Actually, it looks like I might have the answer. I think I'm going to
have to use the Marshal.Copy() family of functions. Please let me
know if there is a better way..

Andy
 
J

Jackie

How about something like this?
----------------------------------------
IntPtr mem = Marshal.AllocHGlobal(sizeof(int));
int dataToWrite = 1234;
int dataRead;

// Write to memory
Marshal.WriteInt32(mem, dataToWrite);

// Read from memory
dataRead = Marshal.ReadInt32(mem);

//Console.WriteLine(dataToWrite);
//Console.WriteLine(dataRead);

Marshal.FreeHGlobal(mem);
 
A

Arne Vajhøj

Actually, it looks like I might have the answer. I think I'm going to
have to use the Marshal.Copy() family of functions. Please let me
know if there is a better way..

The Marshal class is it. There are multiple methods.

But I would try to avoid the mechanism if possible.

C# is not C and should be used as such.

Arne
 
A

Andrew Falanga

How about something like this?
----------------------------------------
     IntPtr mem = Marshal.AllocHGlobal(sizeof(int));
     int dataToWrite = 1234;
     int dataRead;

     // Write to memory
     Marshal.WriteInt32(mem, dataToWrite);

     // Read from memory
     dataRead = Marshal.ReadInt32(mem);

     //Console.WriteLine(dataToWrite);
     //Console.WriteLine(dataRead);

     Marshal.FreeHGlobal(mem);

Thank you. Somehow I missed these functions and this will be more
intuitive than what I have at this time.

Andy
 
A

Andrew Falanga

The Marshal class is it. There are multiple methods.

But I would try to avoid the mechanism if possible.

C# is not C and should be used as such.

Arne

Unfortunately, there is no other choice. I'm calling into kernel and
BIOS runtime libraries. All through p/invoke methods. The data has
to be marshaled.

Andy
 
A

Arne Vajhøj

Unfortunately, there is no other choice. I'm calling into kernel and
BIOS runtime libraries. All through p/invoke methods. The data has
to be marshaled.

Yes.

But in many cases the standard marshalling of structs will
be sufficient.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top