Simple 2D Direct Draw Example

As previously mentioned, there are two ways to access DirectX in the managed environment. You can use either the function call approach (as we did in the previous example) or the pure COM approach. This section shows how to work with DirectX using the pure COM approach. The example will demonstrate a simple 2D drawing. Of course, before we can use the COM approach, we'll need to import the DirectX COM object. The following sections show everything needed to create this example. You'll find the...

Determining the Operating System Version Example

It's important to know which version of Windows an application is running on if you plan to use special operating system features. The GetVersionEx() function is easy enough to understand it might seem as if you could use it directly within the .NET Framework application using PInvoke. However, it's not as easy you might think to make this function work. There are several problems to overcome when using the GetVersionEx() function. The first is that the GetVersionEx() function can use one of...

Dependency Walker

The Dependency Walker (also called Depends for the name of the executable file) enables you to view the dependencies between various DLLs. For example, you might know that User32.DLL contains the MessageBoxEx() function, but may not realize that User32.DLL also relies on other DLLs to make the function work. The interdependencies between DLLs are the cause of a number of problems with older, unmanaged applications, which is why Microsoft is now promoting the .NET Framework. However, whenever...

Writing the Code

This is an extremely simple 2D drawing example. All it does it read a file from disk and place it on screen. However, when running it, you'll notice some significant differences from performing the same task using native managed applications. Using DirectX begins by initializing the environment. Listing 14.7 shows how to perform this task. Listing 14.7 Initializing DirectX in the Managed Environment These contants control the application configuration. private const int SCREENWIDTH 800 private...

Implementing a Callback from the Managed Environment

It's time to look at the first callback example. This example is designed to break the callback creation process down into several discrete steps. In this case, we'll discuss what you need to do to enumerate the current windows. Enumerating the windows is the first step in discovering windows that you might want to communicate with an important part of the messaging process. The source code for the example appears in the Chapter 05 C EnumWindows and Chapter 05 VB EnumWindows folders of the CD....

Using Icon Forge

I've used IconForge from CursorArts Company for quite a few of the examples in the book. After using it for a while and failing to exhaust the number of features it contains, I can say that it's probably the best special-purpose drawing program I've tried, especially for the low price ( 37.95 plus shipping and handling at the time of writing). While you can create certain types of icons and cursors using the Visual Studio .NET IDE, you can't create all of the types that IconForge provides, and...

Implementing a Callback from a Wrapper DLL

There are going to be times when you use a callback function so often that placing it into each of your applications individually doesn't make sense. However, creating a lot of duplicate code isn't the only reason to use the wrapper DLL. The following list provides some additional reasons you should use this technique in your next application. Packaging Issues Using a wrapper DLL enables you to package the calling details in a way that you can't do normally. Using a DLL becomes a matter of...

Using PCLint for C Development

We've looked at more than a few wrapper DLLs so far in the book. None of them involve heavy-duty Visual C programming, but there's enough code involved that it would be nice to get a little help especially if you aren't as familiar with Visual C as you are with other languages. PC-Lint is a source code analysis tool. It helps you find programming errors that the compiler won't find. There are a number of Visual C analysis tools on the market, but many look just as complex as Visual C itself....

Get Message and Peek Message

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 Fifty Two Tips for Error Free 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...

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...

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

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...

Using the Mesh View 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...

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....

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...

Simple Direct Sound 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 IData Object

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...

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...

Direct DrawCreate Clipper

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...

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...

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...

Using the DirectX Caps Viewer Tool

Fungsi Directx

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...

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...

Gaining Access to Console Mode Information

Screen Buffer Size Oracle

There are a number of environmental considerations when working with the console. The most important considerations are the mode in which the console is operating at any given time. For example, you might want to display data differently when the console is in full screen mode, than when it operates as a window. The two console mode-specific functions are GetConsoleDisplayMode and GetConsoleMode . You might need GetLargestConsole-WindowSize function on occasion, to learn how big you can make...

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...

Developing an About Dialog

The About dialog is one of the easier parts of the MMC snap-in to create, and it's also the only part that works independently of the rest of the MMC snap-in. It helps to implement this part of the MMC snap-in first because you can quickly see that the MMC snap-in is registered correctly and has the ability to interact with MMC. Sometimes, creating the required interaction is harder than any other part of the development process because Microsoft doesn't make it easy to perform this task. We'll...

Looking Up an Account SID Example

Sometimes you know the name of a well-known account, such as Administrators, but you don't know anything else about it. The Win32 API provides an answer to this dilemma. You can create a SID for the account without knowing anything about it and then look up the information for that account. This technique proves handy for a number of uses. For example, if you know that you want to create a new user that has starting rights that are the same as those used for a well-known account, you can begin...

File Compression

Microsoft doesn't provide an easy method for compressing files as part of the documented Win32 API. In fact, compressing a file is so difficult under Windows that most developers turn to third-party libraries to perform the task. However, it's possible to decompress files and this support isn't found in the .NET Framework. Tip There are a number of good third-party libraries available for compressing and decompressing files in the ZIP format. One of the better libraries is The Zip GZip...

Direct DrawCreate and Direct DrawCreateEx

There are two functions you can use to create a DirectDraw object if you don't want to use the pure COM method DirectDrawCreateO and DirectDrawCreateEx . The latest version of the DirectX API supports both methods, but Microsoft recommends using the DirectDrawCreateEx function because it returns an object with full Direct3D support. The DirectDrawCreateO function provides support for 2D drawing only. Both functions require a globally unique identifier GUID as input for the first argument. The...

Working with ACEs Example

So far, we've looked at examples of how to work with the access token and the security descriptor and the vagaries of working with specific objects such as files. This example completes the tour of security support for the Win32 API by looking at the ACEs that make up the SACL and the DACL. Because you're most likely to work with the DACL, this example emphasizes access over auditing. However, working with the ACEs in either structure is about the same. Listing 8.3 shows how you'd access the...

Simple Direct Input Example

DirectInput works with a number of devices. However, one element is true for all of them. Generally, you'll want to know the device's current status. The example application for this section deals in part with that scenario. However, before you can work with the device, you need to know that it exists. The code shown in Listing 15.2 shows how to collect the DirectInput device information for your system. It also contains some of the initialization code for this example. You'll find the source...

Direct DrawEnumerate and Direct DrawEnumerateEx Direct DrawEnumerate and Direct DrawEnumerateEx

Both of these functions initiate an enumeration sequence similar to other enumerations we've discussed in the book. The enumeration provides information about the DirectX-capable devices that are installed on the host system. The DirectDrawEnumerate function relies on the DDEnumCallback function to handle the callback. Likewise, the DirectDrawEnumerateEx function relies on the DDEnumCallbackEx function to handle the callback. We discuss both callback functions in the section entitled Creating...

Using the Get FileSecurity Function Example

Getfilesecurity

One of the problems that many developers have noted with the .NET Framework security is a lack of access to file and other object security information. For example, it's hard to tell who owns a file without using the Win32 API calls. That's where the GetFileSecurity function comes into play. It enables you to retrieve file security information in the form of a security descriptor. From the theoretical discussion earlier in the chapter, you know that the security descriptor contains just about...