Re: Size of a class
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Mon, 20 Mar 2006 20:36:55 +0100
There seems to be some confusion here I guess, when you create a file
mapping you effectively allocate a memory segment backed by a file (regular
or paging file).
That means that the size of the memory segment is the size specified when
calling CreateFileMapping.
Now, when you map a view of the file, by calling MapViewOfFile, you
effectively reserve a number of bytes starting at a location relative to the
beginning of the segment (offset), with a size specified by the last
argument. That means that, nor the size of the 'file' reflects the presence
of any data, nor do you have an idea of the size of the mapped view(s).
So your only option is to include the size of the mapped view into the data
stored in the buffer, like this:
....
Marshal.WriteInt32(new IntPtr((int)pBuf), (int)ms.Length);
byte[] buff = ms.GetBuffer();
// marshal managed byte array to unmanaged memory buffer (backed by a
file).
for (int i = 0; i < ms.Length; i++ )
Marshal.WriteByte(new IntPtr((int)pBuf + sizeof(int) + i), buff[i]);
..
// signal "data available" to readers....
Here, pBuf is the pointer to a mapped view returned by MapViewOfFile, and ms
is a MemoryStream object reference.
The reading side can read the data back using something like this:
pBuf = MapViewOfFile(handle, access , 0, 0, 0))
int len = Marshal.ReadInt32(pBuf, 0);
byte[] buff = new byte[len];
for (int i = 0; i <len; i++ )
buff[i] = Marshal.ReadByte(new IntPtr((int)pBuf + sizeof(int)), i);
MemoryStream ms = new MemoryStream(buff);
BinaryFormatter bf = new BinaryFormatter();
Object o = bf.Deserialize(ms);
//use object o
....
Another option is to marshal a structure which include size and possible
other objects properties, but in general above is all you need.
Willy.
"Dave" <Dave@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:0095AC0B-2F87-43B7-87C7-0DDEE6D468E6@xxxxxxxxxxxxxxxx
| Willy
| Further to my previous reply, I have solved problem 1: use OpenFileMapping
| not CreateFileMapping - fairly simple!
| However I have scoured the web for a solution to Problem 2 - how to find
the
| size of the file created and written in a different process, and there
simply
| does not seem to be a way of doing it (other than somehow embedding the
size
| in the written data). Several people have posted the question on several
| forums, but no one has managed to provide an answer. If you know of one
I'd
| certainly like to hear it.
| --
| Dave
|
|
| "Willy Denoyette [MVP]" wrote:
|
| >
| > "Dave" <Dave@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
| > news:1D5DB32A-6069-4C59-91C4-14A927D80BF3@xxxxxxxxxxxxxxxx
| > |I am serialising an object to a memory mapped file (using the
| > | CreateFileMapping and MapViewOfFile p/invoke calls). These need to
know
| > the
| > | maximum size of the "file". I can put in a "good guess" ie it won't be
| > more
| > | than, say, 1K, but it would be tidier to use the actaul size. Is it
| > actually
| > | possible to find out how big an object (or even better, a class) would
be
| > | when it is serialised (I suppose one way would be to separately
serialise
| > it
| > | to a memory stream and get the size of the memory stream, but would
that
| > be
| > | accurate? It's certainly not very efficient.)
| > | --
| > | Dave
| >
| > You'll have serialize the object to a MemoryStream anyway, why do you
| > consider this as being inefficient?
| >
| > Willy.
| >
| >
| >
.
- References:
- Re: Size of a class
- From: Willy Denoyette [MVP]
- Re: Size of a class
- Prev by Date: Re: Help Needed! Really Stuck. C# dvents & null delegates
- Next by Date: Re: saving rich text box data!
- Previous by thread: Re: Size of a class
- Next by thread: Programming databases in .NET - please help
- Index(es):
Relevant Pages
|