Chapter 6 Gaining Support for the Console Application

Figure 6.1 Changing code pages is relatively easy using Win32 API calls. Figure 6.2 Use the settings on the Layout tab to change the output from the GetLargestConsoleWindowSize function. Figure 6.3 The example application demonstrates the usefulness ofthe console mode information. Figure 6.4 The Environmental Variables dialog box contains a set of system and user strings. Figure 6.5 Retrieving command-line arguments and environment strings is relatively easy with .NET. Figure 6.6 Setting...

GetMessage and PeekMessage

We've discussed the Windows message pump and several of the messages that can appear within the message queue. You know that whenever an application sends a message, Windows will place the message in the recipient's message queue, which is essentially an In Box for Windows messages. However, we haven't discussed how the recipient actually receives the message so it can act on it. The GetMessage and the PeekMessage functions provide the means for retrieving a message from the Windows message...

Using the DirectX Control Panel Application

You'll find the DirectX Control Panel Application in the Control Panel after installing the DirectX SDK. This applet has the usual DirectX icon. What it does for developers is nothing less than amazing. Once you begin using this tool, you'll wonder why Microsoft didn't include it in previous versions of the product. Note For whatever reason, the DirectX Control Panel Application doesn't seem to install properly into the Windows XP System32 folder. If this problem occurs on your system, simply...

Using a Mesh Example

Now that you've seen how a mesh is constructed and how to test the content of an X file, it's time to see what you've learned in action. The example in this section performs the simple task of loading an X file and rendering it. The example code will demonstrate that some of the DirectX code is the same as you've used in the past but other code is different because of the managed environment. In fact, this example contains a few surprises that you might not have expected. Note A lot of...

Appendix A FiftyTwo Tips for ErrorFree Win32 API Access

We all like the little bits of information that say a lot in a short space, especially when working with complex ideas or concepts. These tips are especially worthwhile because they often represent hours of work. They will help you create a better environment in which to access the Win32 API. Many of them are significant and show the hours of work required for discovery others are subtle suggestions exposed in a moment of clear thinking. A few of the tips simply consist of good-to-know...

Function Return Values

You might wonder why there's a separate section on return values in this chapter considering the amount of time we've spent looking at them in the past. DirectDraw relies on a common Visual C macro that we haven't discussed yet to create most of the error codes that it uses. I say most of the error codes, but some don't use the macro they're based on something else. Confused yet Don't be we'll discuss the origins of the various error codes. The first error code we'll discuss is DD_OK. This is...

Where Do You Go from Here Ujx

This chapter has shown you how to use three new DirectX tools. You've also seen a coding example that shows how to use the MeshView utility with your application. What you've received is a general overview of the extended capabilities of DirectX we haven't really explored the depths of this complex API. You should leave this chapter knowing that the tools do indeed work, that they reduce the effort required to code certain types of DirectX applications, and that everything works with the...

Overview Lju

We've spent a lot of time looking at what DirectX can do and how to make it perform tasks within the managed environment. This chapter helps you put together all of the skills you've learned so far to create more complex applications. We'll also begin using some of the extended features that DirectX can provide. The chapter will combine tools with examples. There are three tools that we'll explore the GraphEdit utility, DirectPlay Network Simulator, and the MeshView Tool. You'll learn about...

Writing the MMC Wrapper DLL

This section describes the portions of the MMCHelper.DLL that we'll use for every MMC snap-in that you create. You'll learn about some essential functions, the interface descriptions, enumerations, and structures. Each element of the MMCHelper.DLL will appear in a separate file to make it easier to view and modify the code later. This is a good practice to follow when you create large DLLs designed to act as helpers to components. Each file is named to reflect the content of one of the sections...

Manipulating the Console Screen Buffer

What precisely is a screen buffer It's the area of memory set aside to represent the contents of the screen. Windows applications don't write directly to video memory, so they require some area of regular memory in which to place their data. When Windows updates that actual video memory, it considers the content of the screen buffer for each application. Consequently, knowing something about the console screen buffer can help you create a better user environment for your application. Sometimes...

Learning the DirectX Capabilities of the Host Machine Example

Sometimes you need to know how many display devices the host machine has available for drawing. In addition, you need to know the GUIDs of any ancillary devices so that you can access them as needed. The DirectDrawEnumerateEx function we created for the DirectXHelper library works perfectly for this purpose. The callback continues to receive input until DirectX has listed all of the devices. Of course, this isn't the only method for checking out a host system. DirectX also comes with the...

Creating DirectX Callback Function Prototypes

Like many newer parts of the Win32 API, DirectX relies on callback functions to return information to the calling application in an asynchronous manner. Using this technique ensures that the application will continue receiving information rather than get blocked waiting for the information to arrive in one chunk. As you might imagine, all of these callback functions tell you something about DirectX. In fact, the DDEnumCallback and DDEnumCallbackEx functions are used with the DirectDrawCreate...

Demonstrating the Windows Message Handler

In older versions of Windows you simply told the operating system that you wanted to shut down, and that was the end of the process. Newer versions of Windows require a little more information, and Windows XP makes it downright impossible to shut down unless you have a good reason. For this reason, the code for initiating a Windows shutdown is a bit long. Listing 4.6 provides you with the essentials. Listing 4.6 Using the ExitWindowsEx Function to Shut Windows Down Used to send a message that...

Using Cursors and Fonts

Many people think that they're stuck with the cursor and font originally presented in the console window others simply don't think too much about the option of changing either item. However, it pays to know about both cursor and font manipulation for those times when you want to emphasize something on screen. The GetConsoleCursorInfo and SetConsoleCursorInfo functions help you with the console cursor. The GetConsoleFontSize function enables you to work with the font. You can specify the index...

Balloon Help Example

Balloon ToolTips are relatively new. They're the ToolTips that you see when Windows wants you to visit the Windows Update site or the modem connects or when Windows needs to provide some other informational message. Balloon ToolTips have rounded edges and accommodate a little more text than a standard ToolTip. The .NET Framework provides good support for most types of ToolTips. However, if you want to create a balloon ToolTip, the Win32 API is the only option. The reason you need to use the...

A Case of Missing Functionality

As previously mentioned, the .NET Framework lacks functionality for some basic calls such as Beep . This means that a C developer who needs to create a sound within an application has to find some other means to do it. There's no doubt that the functionality is missing, but the technique used to create the desired functionality varies by language capability, environment, and flexibility. For example, when working with Visual Basic, you already have access to a basic Beep function, so no...

Using the MeshView Tool

A mesh is a description of a 3D object. The description relies on a series of triangles that are expressed using vertices the angles in the object and faces the flat surfaces . The mesh also includes ancillary information in the form of materials and textures that determine how DirectDraw will skin the 3D object. All of this information is stored in memory when used to draw an image on screen. The information can also appear in an X or an M file for permanent storage. You'll find the X files...

Shutting the System Down Remotely Example

You'll run into situations in which you need to shut a system down from a remote location. Most people associate this action with servers the server might reside in a closet and not even include a monitor and keyboard. However, remote shutdown becomes more important for desktop computers on a daily basis. For example, when a user logs in from a remote location, part of the login process could turn the user's desktop computer on and prepare it for use. When the user session ends, it's good...

Opening a File

Opening a multimedia file is actually one of the harder parts of using it. You'll find that once you've successfully opened and read the file, playing it and interacting with it in other ways is relatively easy. Part of the problem is the complexity of the file configuration the file contains several components that you need to interact with as part of opening the data for use. Listing 11.3 shows the minimum code you'll need to open a multimedia file. Note that the listing doesn't include any...

Handling Memory Leaks and Other Resource Problems

Some developers are under the impression that .NET applications can't leak memory and will never have problems with resources because the Garbage Collector handles all of these problems. The fact is that management of unmanaged memory or resources always falls on the developer. The Garbage Collector will only manage memory and resources that it knows about that it was involved in allocating on behalf of the application. Because of the amount of misinformation on various Web sites and...

Consolidated Objects

One of the issues that Microsoft addressed in DirectX 8.1 was object creep. In DirectX 7, it seems that you have an object for everything and that each of those objects requires a separate creation step. If you want to create a DirectDraw and a Direct3D object in DirectX 7, you'll likely have to perform two separate steps and create two separate objects. The problem with this approach is that it's hard getting the two objects to work together, so you end up writing some odd code to do it....

RegisterWindowMessage

You'd think that with all of the messages that Windows supports natively, you'd never need to create a message of your own. Actually, applications commonly create custom messages for intra-application communication. Sometimes an application will spawn other processes and need to communicate with those processes using a special set of messages. Because the messages are sent publicly with SendMessage or PostMessage , Windows needs to know about them and you need to provide a unique name for them....

PostMessage PostThreadMessage and PostQuitMessage

The PostMessage and the SendMessage functions perform essentially the same task they both send a message to the specified thread. However, the PostMessage function returns from the call immediately, while the SendMessage function waits for the recipient to respond. Both functions accept essentially the same arguments, so anything you can do with a SendMessage call, you can do with a PostMessage call. You might wonder why Microsoft would include two calls with essentially the same functionality....

Working with Theme Support Example

Themes are one of the more innovation additions to Windows XP. While Windows XP does support the older themes used in previous versions of Windows, it also provides support for a new class of themes. This new theme class enables the user to configure the operating system environment in ways not possible in the past. This technology is loosely based on the skins technology used for Microsoft utilities such as the Media Player, but there are differences that make the two technologies...

An Overview of the Functions

Now that you have a better idea of how token-based security works, let's look at some of the functions we'll use later in the chapter to create example applications. Table 8.1 contains a list of the various API functions that you'll commonly use to change the user's access token. This list provides only an overview, not a detailed description, of each API function. Table 8.1 Common User Access Token Function Overview Function Name Description AdjustTokenGroups Allows you to adjust one or more...

A C LIB Wrappers Access Example

There are many times when you'll need to write a wrapper DLL to gain access to a Win32 API call. We've discussed many of the scenarios for using this technique earlier in the chapter, so let's look at the example. You'll run into more than a few situations when you must gain access to one or more types of security information that the .NET Framework doesn't provide. For example, you might need to know the security information for the local user. Unfortunately, the functions required to access...

What Is a Callback Function

As previously mentioned, callback functions provide two-way communication. However, a callback function is more than a messaging technique it's the Win32 API version of the asynchronous call. Your application makes a request and supplies the address of a callback function within your application. Windows will use this address as a communication point for the responses for your request. In many cases, Windows will call this function more than once some callback functions are called once for each...

A Simple DirectSound Example

We saw in the previous section that DirectSound supports a number of DMOs that can add effects to sound files. In this section, we see how one of those DMOs works in practice. Listing 15.1 shows the code for a basic DirectSound example. You'll find the source for this example in the Chapter 15 C DirectSound and Chapter 15 VB DirectSound folders of the CD. Listing 15.1 Playing a Sound Using DirectSound private void btnPlay_Click object sender, System.EventArgs e private void btnPlay_Click object...

Working with IDataObject

The IDataObject interface is the place where you can make the most mistakes in creating an MMC snap-in. In fact, implementing this interface with even slight errors can cause MMC to simply exit without an error message of any kind. Even if MMC does present an error message, it's likely that this message will fail to tell you anything that you can use to debug the MMC snap-in. The GetDataHere method is the one mandatory portion of the interface that you have to implement. In addition, this is...

Understanding the Windows Message Types

Windows is literally packed with messages. There are thousands of messages to which your application can respond. Even the user interface messages number over a thousand. There are messages for mouse movement, keyboard clicks, system messages, user messages, all kinds of messages. One of the best places to view these messages in action is Spy . Figure 4.1 shows the Messages tab of the Message Options dialog box. As you can see, there are a number of ways to group just the messages that appear...

Viewing the Second Level of Printer Information

If you thought the level 1 printer information was sparse, you'll be pleased by the level 2 information. You can find out almost every piece of information about the printer using the level 2 version of GetPrinter . In fact, it's easy to go into information overload because this level provides everything from the printer name, to its security settings, to the printer statistics. For example, you can find out the current form type and the size of the paper use, as well as the number of pages the...

Developing the MMC SnapIn Example

The MMC example we'll look at is basic it includes just the essentials required to get MMC to recognize the component as a snap-in. This means implementing specific interfaces and ensuring that MMC will recognize the interfaces. We won't concentrate too hard on any serious content in this example because getting the snap-in to work is difficult enough for a first try. The example will show you how to use the wrapper DLL, create an About dialog box, display basic information about the snap-in in...

Obtaining Device Capabilities Example

It's important to know the capabilities of the devices installed on your system. Of course, Windows provides a myriad of ways to find this information. For example, in Chapter 7 you learned the techniques for discovering the capabilities of the parallel and serial devices attached to a system. However, one function stands out from the rest as providing a little more in the way of generic information, the GetDeviceCaps function. This function helps you obtain information about any device with a...

Displaying a Configuration Dialog Example

Many of the multimedia devices on your system have configuration dialogs. The configuration data affects the quality of the device output, as well as its capabilities in many situations. In some cases, you might want the user to configure a device before using it to play a sound or perform some other multimedia task. The configuration is important because you want to provide the user with the best possible multimedia experience. When you want to perform this type of task, you need to work with...

DirectDrawCreateClipper

This function creates a DirectDrawClipper object, which describes a clipping area on the screen. DirectX supports two types of DirectDrawClipper objects dependent and independent. Using this function creates an independent DirectDrawClipper object. A developer would use the IDirectDraw7.CreateClipper method to create a dependent object. The advantage of using an independent DirectDrawClipper object is that you can clip an area in any DirectDraw object. The disadvantage is that you have to...

SendMessageCallback

The SendMessageCallback function has two main purposes. First, it sends a message to another process just like the other message-related functions we've discussed so far. Second, it registers a callback function with the message recipient. A callback function is a special function used by the message recipient to return message results to the message sender. In short, this is the first function to provide a two-way communication path for messages. The first four arguments for the...

Using the DirectX Texture Tool

If a development team were to create every aspect of the art that goes into many applications today, it would take substantially longer to create them. The race to create a better way to draw computer images began almost as soon as there were computers that could display graphical data. The earliest computers came with nothing more than ways to position a cursor and draw a dot. The next generation of computers came with a set of functions that created graphics primitives that a developer could...

Creating the Wrapper DLL

The wrapper DLL does most of the work of retrieving the printer information from the Win32 API in this case. There are times when you'll need to write much of your Win32 API access code with the wrapper, instead of the application, in mind. In this case, the functionality needed to access the printer appears in a library, so it makes sense to create a function that will perform a specific sequence of steps. Listing 7.3 shows the code to access the first-level printer information. The...

Determining Device Capabilities

Waveoutgetdevcaps Szpname

Before we make a sound, let's look at a simpler example. Listing 11.2 shows the technique you can use to determine the characteristics of the wave devices. The example doesn't go through the usual flag machinations because we've seen that technique in many examples in the past. Rather, this example exposes you to some new techniques that we haven't explored. You'll find the flag definitions in the MMSystem.H file and the manufacturer and product identifiers in the MMReg.H file. The source code...

Importing the DirectX COM Library

One of the first steps you'll need to perform for any application that uses the COM method of accessing DirectX is to import the DirectX library into the application. The .NET environment will perform all of the work necessary to create a bridge between the managed environment and the COM library. If you've already worked with COM libraries in the past, you can import the library and skip the remainder of this section. Note It might be tempting to think that the DirectX COM library is complete....

Viewing the First Level of Printer Information

The reason that so many developers use the GetPrinter function is that it's very versatile, and you choose how much information to retrieve. The Platform SDK documentation specifies nine levels of information retrieval for this function each of which requires a unique data structure. For the .NET developer this means that you'll need a minimum of nine overrides for the GetPrinter function to create a full implementation. The example code shown in Listing 7.4 isn't quite that ambitious. It shows...

Playing a File

Once you have the file open, the rest of the process is easy. The previous section showed how the application opened a WAV file, determined its format, opened a wave device, and finally read the data from the WAV file into memory. At this point, everything is ready to actually play the data contained in the WAV file. Listing 11.4 shows one method for playing the wave data. This source code appears in the same files as Listing 11.3. Listing 11.4 Technique for Playing Wave Data This function...

Converting Variables to Visual Basic Types

Many of the same rules that you observe when converting a variable from the unmanaged environment to C also apply for Visual Basic. However, the actual details of the conversion will often take a different shape in the two environments. For example, Visual Basic is more sensitive to numeric values than C in that it uses native numeric formats easier than those supplied in CLR. This sensitivity means that it's easier to use an Integer in Visual Basic than it is to use a UInt32. The Integer...

Using the DirectX Caps Viewer Tool

The DirectX Caps Viewer tool is one of the first utilities you should learn how to use after installing the DirectX SDK on your system. The data provided by this utility can save considerable time in locating hardware- and driver-related problems on your system. In fact, this is a tool that every administrator should learn how to use as well. The following sections discuss how you can use the DirectX Caps Viewer tool to improve the DirectX development experience. An Overview of the DirectX Caps...

SendMessage

SendMessage is the simplest function you can use to send messages to Windows or to other applications. One of the best ways to test this function is to work with the system commands. You'll find a complete test program for the system commands in the Chapter 04 C SysCommand and Chapter 04 VB SysCommand folders of the CD. This example shows the full set of SC commands in action at least those that are documented . Figure 4.4 shows the dialog for this example, which includes a list of the SC...

Determining the Console Window Title

Windows will assign a default title to the console window when you create it normally the name of the execution including path information. In some cases, the default title works just fine. However, there are times when you might want to personalize the window title to reflect the current application. To check the current title you'll use the GetConsoleTitle function the SetConsoleTitle function enables you to change the current title into something more appropriate. Listing 6.8 shows how to...

Understanding the Effects of Unsafe Code

Unsafe Code

The term unsafe code is somewhat ambiguous because it doesn't really tell you anything about the code. A better way to view unsafe code is unmanaged code that appears within a managed environment. Any code that relies on the use of manual pointers symbol or addresses amp symbol is unsafe code. Whenever you write code that uses these symbols, you also need to use the unsafe keyword in the method declaration as shown below. This example appears in the Chapter 01 C Unsafe folder of the CD. unsafe...

An Overview of the Data Structures

DirectX uses a total of 19 specific data structures. Many of these data structures perform multiple tasks and the content depends on the task they're performing at the moment. Some of the data structures weren't used in the past, so the documentation Microsoft provides with Visual Studio .NET reflects this fact. Newer versions of DirectX do use more of the functions and data structures. The following list provides a short overview of these data structures DDBLTBATCH DirectX uses this structure...

Working with Code Pages

As previously mentioned, code pages are the character codes mapped to individual characters in the standard 256-character set. Code pages enable a developer to represent information in a variety of languages. The four code page-specific functions include GetConsoleCP , SetConsoleCP , GetConsoleOutputCP , and SetConsoleOutputCP . Listing 6.2 shows how to use these four functions. The example code appears in the Chapter 06 C CodePage and Chapter 06 VB CodePage folders on the CD. Listing 6.2 Using...

Function Declarations

At this point, you have a good idea of how the various DirectX functions work, so it's time to see how to declare them. Listing 14.2 shows typical DirectX function declarations. I say typical because some of the functions will require more than one implementation to satisfy some development needs. In fact, some functions require at least two declarations for a minimal implementation. Listing 14.2 The DirectX Specific Function Declarations This function creates an instance of a DirectDraw...