Common AppData Registry

The CommonAppDataRegistry property returns a reference to an object of class RegistryKey. The returned RegistryKey object references a key in the Registry that the application can use to store Registry data that should be available to all users of the application. This property is read-only and cannot be set. The RegistryKey class is a part of the .NET Framework and is available in a namespace called Microsoft.Win32. It represents a specific key in the Registry and contains methods that enable...

Handling Interop Errors

In the .NET Framework, the CLR reports errors by throwing exceptions when things go wrong. In COM, HRESULTs are the avenue in which errors are reported, so the RCW needs to be able the map the HRESULT for a given error to the equivalent .NET exception. Table 34-2 maps the standard HRESULTs in COM to their counterparts as .NET exceptions. Table 34-2 maps the standard HRESULTs in COM to their counterparts as .NET exceptions. Table 34-2 HRESULTs to .NET Exceptions COR E ARITHMETIC or...

Using Simple Object Access Protocol SOAP

SOAP is the XML dialect used by Web services. It specifies which server-side action you want to invoke to pass the information (that is, the parameters) to the Web service. SOAP also specifies how the information is returned from the Web service (both return values and exceptions). SOAP messages follow a standard format an outer envelope that identifies the message as being a SOAP message, a body section that contains the main payload, and an optional header section for additional information...

Enumerating Registry keys

Enumerating Registry keys is a lot like the Find Files feature in Windows. It enables you to scan from any point in the Registry and retrieve all subkeys and values below that starting point. No methods are currently incorporated into .NET to enumerate Registry keys it is up to you to build functions to support your needs. Knowing the structure of the keys you want to enumerate makes things much easier, as you can simply use a loop. If the structure of the Registry entries is unknown, you have...

Using the Tlbimp utility

The Tlbimp utility is a standalone console application that creates the .NET Interop assembly based on the COM DLL that you specify. It is located in the Framework SDK directory in Program Files. The following code snippet demonstrates the syntax of Tlbimp The command-line options for tlbimp.exe are described in Table 34-1. The command-line options for tlbimp.exe are described in Table 34-1. Specifies the version of the assembly to create Tells Tlbimp to sign the assembly using delayed signing...

Generating XML documentation from comments

An interesting feature of the C compiler is that it can read specially formatted comments and generate XML documentation from the comments. You can then display this XML on the Web to provide an extra level of documentation to developers who need to understand the structure of your applications. To use this feature, you must do two things Use three slashes for comments. The C compiler does not generate any XML documentation for any comments that do not begin with three slashes. Nor does the C...

Writing Registry keys

Creating and writing Registry keys is also accomplished using the RegistryKey object. Several methods in the RegistryKey class are useful when writing keys. Table 24-4 describes the purpose of some of the more prevalent members. Table 24-4 Common RegistryKey Members Table 24-4 Common RegistryKey Members This property retrieves a count of the subkeys for the current key. This property retrieves a count of the number of values for the current key. This method closes the current key. If changes...

Building an Age Calculator

After you install the Microsoft Mobile Internet Toolkit, create a new C project. You see a new option titled Mobile Web Application, as shown in Figure 30-1. _ Vi ji 6J t Prcrtthi JV uiClFWl _J Viuaj - Raacl .J ard efJhoittls _ Oil* PUJITUP ,J VI-j J Sliplii iktot it pqrci Am awhng an -ippfcdton tr, PQAi . fetanm. and oSh enoHe dfrieta Rasa wi bt CTMM Ns iiirii uwV MjAjp Figure 30-1 The new Mobile Web Application option was added when the Mobile Internet Toolkit was installed. This example...

Binary Reader

The BinaryReader class, much like the BinaryWriter class, relies on a FileStream object for accessing files. To demonstrate the BinaryReader class, examine the application in Listing 24-2, which reads the binary header information from a bitmap file. From this header information, you can determine both the horizontal and vertical size of the image file as well as the color depth in bits. Listing 24-2 BitmapSize Application long bmpWidth 0 long bmpHeight 0 int bmpPlanes 0 int bmpBitCount 0...

Remove MessageFilter

The RemoveMessageFilter method removes a message filter installed by the AddMessageFilter method. It removes the message filter from the message pump of the application. The RemoveMessageFilter method takes one argument an implementation of an interface called IMessageFilter. This argument should reference a class that implements IMessageFilter and has already been used in a call to AddMessageFilter . Listing 21-6 shows how this method works. Listing 21-6 Removing an Installed Message Filter...

Output parameters

Output parameters are parameters whose values are not set when the method is called. Instead, the method sets the values and returns the values to the caller through the output parameter. Suppose, for example, that you want to write a method to count the number of records in a database table. Suppose that you'd also like to specify whether or not the operation was successful. The operation may be unsuccessful, for instance, if the database table is not available. You now have two pieces of...

Add MessageFilter

The AddMessageFilter method adds a message filter to your application to monitor Windows messages as they are routed to their destinations. The message filter that you install into your application receives the Windows messages before they are sent along to your form. A message filter installed by AddMessageFilter can choose to handle a message that is sent to it, and the filter can choose whether the message should be forwarded to your form. Listing 21-4 shows how a message filter can be used...

Using iteration statements to execute embedded statements

Iteration statements execute embedded statements multiple times. The expression associated with the iteration statements controls the number of times that an embedded statement executes. The while statement executes an embedded statement list as long as the while expression evaluates to true. The Boolean expression that controls the while statement is enclosed in parentheses that follow the while keyword. The statements to be executed while the Boolean expression evaluates to true follow the...

Using special characters in strings

C enables you to use a special syntax to embed special characters in your string. These special characters are listed in Table 3-6. The special characters t embed a tab into the string. A string defined as hello tthere is stored in memory with a tab character between the words hello and there The special characters r embed a carriage return into the string. A string defined as hello rthere is stored in memory with a carriage return character between the words hello and there. The carriage...

User AppData Path

The string UserAppDataPath property references a path on the file system that the application can use to store file-based data that should be available to the network user currently logged into the machine. This property is read-only and cannot be set. It is used for local users with operating system profiles on the network. Users who have local machine profiles that are not used across the network use a separate property, called LocalUserAppDataPath, to specify where their application data...

Common AppData Path

The string CommonAppDataPath property references a path on the file system that the application can use to store file-based data that should be available to all users of the application. This property is read-only and cannot be set. The application data path is stored beneath the Windows documents folder path used for all users, which is usually a path such as C Documents and Settings All Users Application Data. The actual application path points to a folder beneath this all users document path...

Understanding Ole Db and SQL Server Support

ADO.NET contains two sets of similar classes. One set is a generic set of classes that can be used to access all databases that have OLE DB providers. A second set of classes has been fine-tuned for Microsoft's flagship database, SQL Server. The names of the generic classes all start with OleDb. The SQL Server-specific classes all begin with Sql. Each generic class has a corresponding SQL Server-specific class. For example, the class you use to execute SQL statements against SQL Server is...

Working with Graphics

When working with GDI in .NET, the main object that you need to work with is the Graphics object. The Graphics object is the actual surface that you use to paint shapes, work with images, or display text. Visual Basic 6 and earlier offered limited built-in support for working with graphics, making it difficult for VB developers to write custom graphics applications. The one thing that VB did do was keep track of how forms and the objects on forms were painted on the screen. The AutoRedraw...

Understanding Application properties

The Application class supports several properties that you can use in your C applications. The following sections describe how to use each of these properties. The Boolean AllowQuit property specifies whether your code can programmatically end the application. The property returns True if your code can instruct the application to exit and False otherwise. This property is read-only and cannot be set. Ordinarily, users can end applications by closing the application's main form. Some...

Safe TopLevel CaptionFormat

The string SafeTopLevelCaptionFormat property references a format string that the runtime applies to top-level window captions when the applications execute from an unsafe context. Security is an integral part of the .NET Framework and the Common Language Runtime CLR . The CLR honors the different security zones set up in Internet Explorer Internet, Local Intranet, Trusted Sites, and Restricted Sites , and restricts runtime services to applications that run in untrusted zones. WindowsForms...

Exit Thread

The ExitThread method exits the message loop and closes all forms on the current thread. The method does not take any arguments and does not return a value. If your WindowsForms application contains only one thread and most do , then calling ExitThread is the same as calling Exit . If, however, your application uses multiple threads, then the two methods behave differently. The ExitThread method shuts down one thread but allows the other threads to continue running. The Exit method, however,...

Using constructor methods

A structure may include a special method called a constructor. A constructor method executes when a variable declaration using the structure type is executed at runtime. Structures may have zero or more constructors. Structure constructor declarations are much like class method declarations, with the following exceptions Constructors do not return any values. Do not use a return type keyword when writing a structure constructor not even void. Constructor identifiers have the same name as the...

JustInTime Activation

The JustInTimeActivation attribute enables or disables just-in-time JIT activation for a class. The attribute accepts a Boolean as a parameter, which should be True if JIT activation should be enabled for the class, and False otherwise. JIT activation must always be enabled for objects that participate in transactions. The JustInTimeActivation attribute must be applied at the class level, as in the following code snippet The JustInTimeActivation attribute does not accept any parameters. False...

OnThread Exception

The OnThreadException method raises a ThreadException event. The event can be caught by an OnThreadException event handler installed into the Application object. Listing 21-5 shows how a thread exception can be used in a WindowsForms application. Listing 21-5 Working with Thread Exceptions public class BlockLeftMouseButtonMessageFilter IMessageFilter public bool PreFilterMessage ref Message m LeftButtonDownException new Exception The left mouse button was pressed. return true public class...

Assembly Culture

The AssemblyCulture attribute enables you to specify culture information for the assembly. Culture information specifies the language and country information that the assembly uses to do its work. The attribute takes a string parameter in its constructor that specifies the culture information, as shown in the following example assembly AssemblyCulture us-en Culture strings are defined by an Internet standard called RFC1766. The standard is titled Tags for the Identification of Languages and is...

Local UserApp DataPath

The string LocalUserAppDataPath property references a path on the file system that the application can use to store file-based data that should be available to the user currently logged into the machine. This property is read-only and cannot be set. It is used for local users with operating system profiles on the local machine. Users who have roaming profiles that are used across the network use a separate property, called UserAppDataPath, to specify where their application data should be...

Using the Pen class

Draw Arrow Net

The Pen class enables you to draw lines and curves on a graphics surface. The namespace that contains the features used by the Pen and Brush classes is the System.Drawing.Drawing2D namespace, so be sure to add this with the using statement in your class files. By setting various properties on an instance of a Pen, you can alter the outcome of the pen display. By calling methods in the Graphics class, you can dictate the type of shape you want to output. The following code sets the Color and...

Assembly Trademark

The AssemblyTrademark attribute enables you to specify trademark information for the assembly. The attribute takes a string parameter in its constructor that specifies the trademark information, as shown in the following example assembly AssemblyTrademark Windows is a trademark of Microsoft Corporation. The assembly product name is written to the assembly's manifest it is also available in the Legal Trademarks field of the compiled file's version information block.