From: Andrew Falanga on
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
From: Andrew Falanga on
On Jun 10, 10:11 am, Andrew Falanga <af300...(a)gmail.com> wrote:
> 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
From: Jackie on
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);
----------------------------------------

--
Regards,
Jackie
From: Arne Vajhøj on
On 10-06-2010 12:52, Andrew Falanga wrote:
> On Jun 10, 10:11 am, Andrew Falanga<af300...(a)gmail.com> wrote:
>> 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.
>
> 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
From: Andrew Falanga on
On Jun 10, 11:03 am, Jackie <Jac...(a)an.on> wrote:
> 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);
> ----------------------------------------
>
> --
> Regards,
> Jackie

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

Andy