Prev | Current Page 380 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"


One downside of this feature is that it??™s only available within unsafe code: the
enclosing structure has to be declared in an unsafe context, and you can only use the
fixed-size buffer member within an unsafe context too. This limits the situations in
which it??™s useful, but it can still be a nice trick to have up your sleeve at times. Also,
fixed-size buffers are only applicable to primitive types, and can??™t be members of
classes (only structures).
There are remarkably few Windows APIs where this feature is directly useful. There
are numerous situations where a fixed array of characters is called for??”the TIME_
ZONE_INFORMATION structure, for example??”but unfortunately fixed-size buffers of characters
appear to be handled poorly by P/Invoke, with the marshaler getting in the way.
200 CHAPTER 7 Concluding C# 2: the final features
As one example, however, listing 7.11 is a console application that displays the colors
available in the current console window. It uses an API function GetConsoleScreen-
BufferEx that is new to Vista and Windows Server 2008, and that retrieves extended console
information. Listing 7.11 displays all 16 colors in hexadecimal format (bbggrr).
using System;
using System.Runtime.InteropServices;
struct COORD
{
public short X, Y;
}
struct SMALL_RECT
{
public short Left, Top, Right, Bottom;
}
unsafe struct CONSOLE_SCREEN_BUFFER_INFOEX
{
public int StructureSize;
public COORD ConsoleSize, CursorPosition;
public short Attributes;
public SMALL_RECT DisplayWindow;
public COORD MaximumWindowSize;
public short PopupAttributes;
public int FullScreenSupported;
public fixed int ColorTable[16];
}
static class FixedSizeBufferDemo
{
const int StdOutputHandle = -11;
[DllImport("kernel32.


Pages:
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392