dll")]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool GetConsoleScreenBufferInfoEx
(IntPtr handle, ref CONSOLE_SCREEN_BUFFER_INFOEX info);
unsafe static void Main()
{
IntPtr handle = GetStdHandle(StdOutputHandle);
CONSOLE_SCREEN_BUFFER_INFOEX info;
info = new CONSOLE_SCREEN_BUFFER_INFOEX();
info.StructureSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
GetConsoleScreenBufferInfoEx(handle, ref info);
for (int i=0; i < 16; i++)
{
Console.WriteLine ("{0:x6}", info.ColorTable[i]);
}
}
}
Listing 7.11 Demonstration of fixed-size buffers to obtain console color information
201 Exposing internal members to selected assemblies
Listing 7.11 uses fixed-size buffers for the table of colors. Before fixed-size buffers, we
could still have used the API either with a field for each color table entry or by marshaling
a normal array as UnmanagedType.ByValArray. However, this would have created
a separate array on the heap instead of keeping the information all within the
structure. That??™s not a problem here, but in some high-performance situations it??™s
nice to be able to keep ???lumps??? of data together. On a different performance note, if
the buffer is part of a data structure on the managed heap, you have to pin it before
accessing it. If you do this a lot, it can significantly affect the garbage collector.
Pages:
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393