350 ❘
CHAPTER 16 ExtEnding ironPython Using C#
Many of the Win32 API functions require you to know specific integer or hexadecimal values. Even
C++ developers can’t remember these numbers. Normally, a C++ developer relies on
define
statements
that put the numbers into human-readable form. The P/Invoke code used in this chapter does the same
thing, but sometimes it places the numbers in an enumeration to make them even easier to use. The
StdHandleEnum
class provides a list of standard handles (pointers) for Windows devices: input, output,
and error. However, these aren’t the actual handles.
In order to get the standard Windows handle, an application must call the
GetStdHandle()
function.
This function is in
kernel32.dll
. The
[DllImport()]
attribute tells the compiler where to look for
an external Win32 API function that you want to use in your code. In this case, the attribute also
tells the compiler that you want any error information that the Win32 API can provide. The use of
extern
before the function name tells the compiler that the requested DLL contains a function of the
name that follows. You can now call this function directly and CLR will automatically perform any
required marshaling for you.
Many of the Win32 API calls provide coordinates — x and y locations that tell where something is
or how large it is. The
COORD
structure provides a means of transferring this kind of information
between the .NET environment and the Win32 API environment. Windows uses a very basic view
of structures. Unfortunately, .NET often causes problems by trying to optimize the data structures
and causes P/Invoke calls to fail even though they should succeed. The
[StructLayout()]
attribute
tells the compiler how to create a data structure in memory, which overrides the normal optimiza-
tion process.
You may create applications that need to run in full-screen mode, if for no other reason
than that they require the additional screen real estate to present information to the user. The
GetConsoleDisplayMode()
function tells you what mode the console is currently in. If the con-
sole is in the wrong mode, you can ask the user to change the mode or simply stop the applica-
tion before the screen mode causes any problems. This function returns flags, not an enumerated
value. At least one of the flags is always set, but the return value can have multiple flags set. The
ConsoleDispMode
enumeration makes it easier to work through the flag settings and provide
usable output. The section “Defining the GetCurrentDisplayMode() Method” later in this chap-
ter provides more information about this function.
In some cases, you need to know the largest size console window that the system will support. The
GetLargestConsoleWindowSize()
function provides this information. You can use other Win32
API functions to adjust the size of the window to meet application requirements (which is a topic for
another book). The section “Defining the GetConsoleWindowSize() Method” provides more infor-
mation about this function.
It’s also handy to know what kinds of operations the console window will support. For example, it’s
good to know whether the console window will respond to the mouse. The
GetConsoleMode()
func-
tion provides this kind of information. The output is in the form of flags that you must interpret in
your code. The
GetConsoleMode()
function is special in that the output you receive depends on the
kind of device handle you provide. The output differs when you provide an input handle, versus an
output handle. The section “Defining the GetConsoleInfo() Method” provides additional information
about how this technique works.
548592c16.indd 350
2/24/10 12:49:27 PM
www.finebook.ir