From: Andrew Falanga on
On Jun 10, 5:04 pm, Arne Vajhøj <a...(a)vajhoej.dk> wrote:
> 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

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
From: Arne Vajhøj on
On 10-06-2010 23:25, Andrew Falanga wrote:
> On Jun 10, 5:04 pm, Arne Vajh�j<a...(a)vajhoej.dk> wrote:
>> On 10-06-2010 12:52, Andrew Falanga wrote:
>>> 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.
>
> 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