473,756 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="CDl lRead", SetLastError=tr ue,
CharSet=CharSet .Ansi, ExactSpelling=t rue,
CallingConventi on=CallingConve ntion.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.S izeOf(typeof(ui nt));
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 11200
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="CDl lRead", SetLastError=tr ue, CharSet=CharSet .Ansi, ExactSpelling=t rue,
CallingConventi on=CallingConve ntion.Cdecl)]
private static extern uint CDllRead(uint Offset, uint [] Array, uintLength);

public uint Read(uint Offset, uint[] Array, uint Length) {
uint typeSize = (uint)Marshal.S izeOf(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.Releas e();

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******** *************** ***********@mic rosoft.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="CDl lRead", SetLastError=tr ue,
CharSet=CharSet .Ansi, ExactSpelling=t rue,
CallingConventi on=CallingConve ntion.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.S izeOf(typeof(ui nt));
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
464
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 characters into these char array fields. I am calling this function with (roughly) the following: public class Foo { public byte A;
3
2307
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 = Siofunc(tBuffer, 128); }
2
4656
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++ native compilation allows fixed size buffers (e.g. char pathName) which we've used to represent a memory mapped file, and I can't find out how to implement the same in C++/CLI i.e. #ifdef _MANAGED ref class MyClass
5
1971
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, I can't find where that is said; only that the value of the "a1" attribute must be "fis" if present.
1
10430
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 System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;
1
9435
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 form data in a fixed length structure that is written direct to disk. I need to load the existing files, into a fixed length structure to initialize the form data within the project. I am running into problems with statements like the...
6
2181
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 dArray; public int iScalar1;
3
2003
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
2649
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 form to that keyword in several fields. Problem is that since my upgrade to 2007, it only works the first time. If you try to use it again it does not display the pop-up box and just re-filters for the first work you typed. The only way around...
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9843
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.