PC Review


Reply
Thread Tools Rate Thread

C# fixed keyword explaination

 
 
=?Utf-8?B?Y296?=
Guest
Posts: n/a
 
      14th Mar 2005
I created a wrapper class for a dll written in C. Will the following code
prevent the "ENTIRE ARRAY" from being moved, not just the first array
element? The Wrapper class will be instantiated in a VB .NET application
which doesn't have the ability to prevent the array from being moved.

Thanks in advance,
coz

public class Wrapper
{
[DllImport("CDll.dll", EntryPoint="CDllRead", SetLastError=true,
CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.Cdecl)]
private static extern uint CDllRead(uint Offset, uint[] Array, uint
Length);

public uint Read(uint Offset, uint[] Array, uint Length)
{
uint typeSize = (uint)Marshal.SizeOf(typeof(uint));
uint len = Length * typeSize;

unsafe
{
fixed(uint* pArray = Array) // Prevent the GC from moving
the array
{
return CDllRead(m_hRfm, Offset, Array, len);
} // end fixed
}// end unsafe
}// end Read()
}// end Wrapper class

 
Reply With Quote
 
 
 
 
Brendan Grant
Guest
Posts: n/a
 
      14th Mar 2005
To quote from
http://msdn.microsoft.com/library/default.asp
url=/library/en-us/csspec/html/vclrfcsharpspec_A_6.asp :

An expression of an array-type with elements of an
unmanaged type T, provided the type T* is implicitly
convertible to the pointer type given in the fixed
statement. In this case, the initializer computes the
address of the first element in the array, and the entire
array is guaranteed to remain at a fixed address for the
duration of the fixed statement. The behavior of the fixed
statement is implementation-defined if the array
expression is null or if the array has zero elements.

So yes, your entire array will remain in place while
within the fixed block.

As to your 2nd point, there should be no problem with
calling the code from VB.NET provided you reference an
assembly containing your code.

>-----Original Message-----
>I created a wrapper class for a dll written in C. Will

the following code
>prevent the "ENTIRE ARRAY" from being moved, not just the

first array
>element? The Wrapper class will be instantiated in a

VB .NET application
>which doesn't have the ability to prevent the array from

being moved.
>
>Thanks in advance,
>coz
>
>public class Wrapper
>{
> [DllImport("CDll.dll", EntryPoint="CDllRead",

SetLastError=true,
> CharSet=CharSet.Ansi, ExactSpelling=true,
> CallingConvention=CallingConvention.Cdecl)]
> private static extern uint CDllRead(uint Offset, uint

[] Array, uint
>Length);
>
> public uint Read(uint Offset, uint[] Array, uint

Length)
> {
> uint typeSize = (uint)Marshal.SizeOf(typeof

(uint));
> uint len = Length * typeSize;
>
> unsafe
> {
> fixed(uint* pArray = Array) // Prevent

the GC from moving
>the array
> {
> return CDllRead(m_hRfm, Offset,

Array, len);
> } // end fixed
> }// end unsafe
> }// end Read()
>}// end Wrapper class
>
>.
>

 
Reply With Quote
 
Nathan Neitzke
Guest
Posts: n/a
 
      14th Mar 2005
coz,
I have never seen the fixed() notation before, it might be exactly what I am
doing here.

Here is what I would do -

use the GCHandle syntax. It is basically like this.

GCHandle gcHandle = GCHandle.Alloc(Array);
uint* pArray = gcHandle.Target;
CDllRead(m_hRfm, Offset, pArray, len);
gcHandle.Release();

Since Arrays are considered a type in .NET - the entire object of the Array
type is pinned by the GC, not just the first element.

If the fixed() notation is the same as what I have - by all means use that.
Like I said, I have just never seen it before. It is probably in the same
spirit as using().

Take care.

--
Nathan

"coz" <cozzmail-(E-Mail Removed)> wrote in message
news:8A48E5ED-6D8A-4DE9-8660-(E-Mail Removed)...
>I created a wrapper class for a dll written in C. Will the following code
> prevent the "ENTIRE ARRAY" from being moved, not just the first array
> element? The Wrapper class will be instantiated in a VB .NET application
> which doesn't have the ability to prevent the array from being moved.
>
> Thanks in advance,
> coz
>
> public class Wrapper
> {
> [DllImport("CDll.dll", EntryPoint="CDllRead", SetLastError=true,
> CharSet=CharSet.Ansi, ExactSpelling=true,
> CallingConvention=CallingConvention.Cdecl)]
> private static extern uint CDllRead(uint Offset, uint[] Array, uint
> Length);
>
> public uint Read(uint Offset, uint[] Array, uint Length)
> {
> uint typeSize = (uint)Marshal.SizeOf(typeof(uint));
> uint len = Length * typeSize;
>
> unsafe
> {
> fixed(uint* pArray = Array) // Prevent the GC from
> moving
> the array
> {
> return CDllRead(m_hRfm, Offset, Array, len);
> } // end fixed
> }// end unsafe
> }// end Read()
> }// end Wrapper class
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I remove lines of data above a keyword and below a keyword in a text file Quentin Microsoft VB .NET 1 25th Apr 2007 04:15 PM
How do I remove lines of data above a keyword and below a keyword in a text file Quentin Microsoft VB .NET 0 25th Apr 2007 02:25 PM
Fields explaination and COde explaination needed =?Utf-8?B?SmF5Skc=?= Microsoft Word Document Management 3 16th Jan 2005 04:52 PM
Provider keyword in connection string not a valid keyword J. Muenchbourg Microsoft Dot NET 1 18th Mar 2004 09:50 PM
slow, hanging browser FIXED FIXED FIXED! Resvon Windows XP Internet Explorer 0 1st Oct 2003 04:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:37 AM.