473,394 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C# fixed keyword explaination

coz
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

Nov 16 '05 #1
2 11175
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 codeprevent the "ENTIRE ARRAY" from being moved, not just the first arrayelement? The Wrapper class will be instantiated in a VB .NET applicationwhich 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, uintLength);

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 movingthe array
{
return CDllRead(m_hRfm, Offset, Array, len); } // end fixed
}// end unsafe
}// end Read()
}// end Wrapper class

.

Nov 16 '05 #2
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" <co***********@yahoo.com> wrote in message
news:8A**********************************@microsof t.com...
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

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: PHil Coveney | last post by:
Hello, I am making calls into a legacy DLL. One function in this DLL expects as a parameter a struct with several fields, each of which is a 40-char array. The effect of this function is to copy...
3
by: Dave | last post by:
I have the following code in one of my programs; ert= CO3; int Code = SR(ert, 1024, 1024); char UnsafeTempBuffer = new char; fixed ( char* tBuffer= UnsafeTempBuffer) { Code =...
2
by: ldawson | last post by:
From the same C++ source code, I'm attempting to generate both an unmanaged DLL and a managed assembly. This will eliminate interop as the calling code is slowly migrated to .NET. However, C++...
5
by: Soren Kuula | last post by:
Hi, If I declare an attribute #FIXED as in <!ATTLIST gedefims a1 CDATA #FIXED "fis"> Does the attribute HAVE to be present in instance docuements, as with #REQUIRED? As I read the XML spec,...
1
by: O.B. | last post by:
In the example below, I'm trying to convert a fixed byte array to a string. I get an error about needing to use "fixed" but I have no clue where to apply it. Help? using System; using...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
6
by: =?Utf-8?B?TWFyZWs=?= | last post by:
Hi I am trying to dynamically create the following structure using AssemblyBuilder, TypeBuilder and DefineField: public struct SimpleType2 { public double dScalar1; public fixed double...
3
by: Hongyu | last post by:
Hi, I am a newbie in C++. I saw a code like the below and don't understand it. class A { public: A();
31
by: Redbeard | last post by:
I have a keyword search button on my databases footer bar which when clicked opens a pop-up box that you can type a key word into. When the keyword is entered it put it into a query that filters my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.