Few New Keywords

The ATL library is based on C++ templates. Template parameters can be used by the developer to provide code for a templated class. The ATL classes typically have default values for their parameters so that there is a default implementation. One pattern that ATL uses is to allow the developer to provide code for its classes as a mix-in class. The developer provides this class through a template parameter native C++ from atlcom.h template < class Base> class CComObjectGlobal public Base...

Figure 924 The Atlmfc Trace Tool dialog

I- Category & Function Names (* Inherit from Module C Enabled C Disabled I- Category & Function Names (* Inherit from Module C Enabled C Disabled When you change settings via the trace tool, you should click on the Apply button for each change you make. You can also use the trace tool to persist all the settings to a file and to load them from a persisted file. In addition, you can create your own trace category, which you do by creating a global instance of the CTraceCategory class,...

Tracing and Debugging

I will cover debugging and use of the Visual Studio.NET debuggers in greater depth in Chapter 9. In this section I will outline the classes that the Framework Class Library provides. The System.Diagnostics namespace has classes that can be split into five groups tracing, debugging, performance monitor, event log, and process information. The performance monitor and event log are specific to NT I have delayed description of the Framework Class Library classes for these services until later in...

Figure 33 Context and domainbound objects The use of proxies in these situations is summarized in Table

Note that marshaling occurs only when code accesses instance members static members are always agile and can be accessed without marshaling. This means that if you have a static data member, the data will exist in the context where the member is accessed. Can be used in any context within the domain through a direct reference. May useSerializable may also derive from State flows to another domain, where the object is accessed through a direct reference. There is no distributed identity. Object...

Summary

Creating an assembly or application file involves more than merely writing the code. Your code may depend on other code, and you may use resources. A project contains the files that will be compiled, and a solution contains the dependencies of each project, so that when you build a solution only the files that have been changed will be compiled. The Visual Studio.NET IDE allows you to identify these dependencies through the solution properties. In addition, the IDE integrates with the source...

Interop with Native Code

The C++ compiler allows you to call native code linked in from static libraries for example, as follows C++ compiled with clr using < mscorlib.dll> include < stdio.h> void SayHello() puts(hello) Hello* hello new Hello hello-> x 0 hello-> SayHello() delete hello This looks like ordinary C++ that you are used to compiling to native code. I have written it like this deliberately because I want to indicate that normal ANSI C++ can be compiled by the managed C++ compiler. The interesting...

Common Language Specification

The Common Language Specification (CLS) is a set of suggestions that allow types written in one language to be used by code in other languages. On the surface the mere existence of the CLS may appear to imply that .NET is not language neutral because, the argument goes, if it were language neutral, the IL created by any language compiler would be executed by the runtime. In fact, that statement is true, with or without the CLS, because as I have already mentioned, the .NET Runtime executes only...

Figure 18 Reference types value types and the managed heap

Size of csPerson, including 16 bits for the ushort member and a pointer to the string member Size Of string, including space for the 7 characters In C there are two categories of value types, enum and struct for example public enum csSex byte unknown, Male, Female public Color hairColor public Color eyeColor Enums are implicitly derived from System.Enum, whereas structs are implicitly derived from System.ValueType. Note that you are allowed to specify the base type for the enum this facility is...

Figure 111 Sample manifest contents

Operating System and Processor Support Hash of File containing manifest, signed by private key When you use a type in your assembly that is defined in another assembly, the compiler creates a static link to the assembly of that type. The manifest of your assembly contains a table that has the identity (name, version, culture, strong name) of all the assemblies to which your assembly is statically linked, to allow the system to load the correct assembly at runtime. Every assembly must link to...

COM Events

COM events are different from .NET events in that they are interface based. That is, for a COM object to generate a single event, the developer has to create a whole interface just for that event and the event handler has to implement the entire interface. If an existing interface has the specific event, it can be used, but the client will have to implement the entire interface just to support the single event. One solution is to use a dispinterface as the event interface so that late binding...

Reading Attributes

Attributes are read with the reflection API. There are several ways to read the attributes on a type, depending on whether you are reading the attribute on an instance of the type or through type metadata. If you have an instance, you can create an instance of the System.Reflection.TypeDelegator class and call GetCustomAttributes to get an array of attributes MyAttributedClass myclass new MyAttributedClass TypeDelegator td new TypeDelegator myclass.GetType foreach object o in...

COM Callable Wrapper

Figure 4.2 shows the COM callable wrapper in action. The COM client calls coCreateInstance with the appropriate CLSID which I will talk about later and gets the COM callable wrapper with the interface that the client requested. The CCW conceptually has two parts a managed part and a native part. The COM client accesses the native part, which implements iunknown and hence has a COM identity. The managed part of the CCW holds a managed reference to the component and hence keeps the component...

Figure 923 Schematic of trace settings

PszFileName NULL const pszFileName NULL const This information can be changed at runtime by calls to the methods on the global allocator instance. In addition, you can use this global object to persist the settings to disk SaveSettings or to load them from a disk file LoadSettings . If you call LoadSettings without a file name, the name of the current module is used appended with .trc. However, you cannot call SaveSettings without a file name. This information can be changed at runtime by calls...

Performance Counting

I have already mentioned that the event log API is arcane, and that .NET could have improved on it significantly but instead has merely provided a weak alternative. The Win32 performance monitor API is also arcane, and it is also complicated, but in this case the .NET designers have provided a much better solution. To provide even the simplest performance monitor counter, you have to write a sharable DLL that manipulates the system registry and creates some complicated, multinested structures....

Unmanaged Exceptions

When your code calls unmanaged code, any SEH structured exception handling exception thrown by this code will be caught by the runtime, which will attempt to convert the exception into an exception type it understands. The following code, for example will throw an exception of the type System.NullReferenceException in the managed code that called the function. In some cases the runtime cannot convert the exception type e.g., if you throw a C exception , so...

Explicit Allocation of Buffers

The Marshal class in the System.Runtime.InteropServices namespace has methods to allow you to allocate buffers, read and write data in unmanaged buffers, access COM objects, and access exception and error values from unmanaged code. In this section I will cover unmanaged buffers. The Marshal class allows you to allocate buffers using two general-purpose allocators the Win32 hglobal allocator and the COM IMalloc allocator. In addition, it allows you to allocate bstr values, which use the bstr...

Marshaling Objects between Contexts

When you pass an object across an application domain or a context boundary, you have to explicitly tell the runtime how the object should be marshaled Is the most important point of the object its location or its state If the location is important that is, the object must be accessed from a specific context then the object should be marshaled by reference. If the location is not important, the object can be marshaled by value. Of course, there are other criteria to take into account the size...

Figure 922 Web Dbg

WebDbg allows you to specify the name of the pipe on which it is listening and to define the accounts that are allowed to access this pipe. This means that you can have more than one instance of WebDbg running on a machine, which is particularly useful if an application is made up of more than one process. You can choose whether all processes send assertions to the same pipe, or whether different processes report to different pipes. webDbg is also used to gather trace messages from processes...

ATL Projects

ATL has undergone a total revamp since VS6. There are essentially three main changes VC Visual C attributes, the ATL7 library, and ATL Server. The ATL7 library consists of many new utility classes and owes its existence partially to ATL Server and partially to the tighter integration between ATL and MFC. Indeed, many MFC features, such as CString, are now implemented by ATL classes. So rather than having to import MFC to use those classes in an ATL project, as the ATL3 developer must do, the...

Startup Options

If the project is a DLL, you can use the Debugging properties Command property C to identify the process to start for the DLL. This can be any process that you choose, or it can be one of the options in Table 9.5. You get these options whether the DLL is managed C or unmanaged C , an ATL or MFC control, an ATL in-process server, or an MFC DLL. If you create an ATL Server project, the Debugging properties page for the ATL Server DLL will give the URL to the .srf file for the HTTP URL property...

The Vsnet Command Line

The Visual Studio application is devenv.exe. You can run devenv as a command-line application, which is particularly useful for building solutions as part of an automated build process. The build command-line switches are summarized in Table 5.10. You have the option of building a project within a solution or the entire solution. If you choose to build just a project in a solution, then you need to use the project switch to indicate which project. Each of the various build command-line switches...

Figure 915 The VSNET debuggers

Two debuggers are provided with the .NET Framework cordbg.exe and DbgCLR.exe. The former is a command-line application, and the latter is a GUI debugger that is essentially a cut-down version of the VS.NET managed debugger. If you select a new instance of VS.NET, VS7Jit starts the IDE by launching it as a COM local server. The IDE has the following CLSID BC104 6A0-CB4 4-42 41-B8D9-0 68 82 4 3AFBC3 If you look at the LocalServer32 key for this server, you'll see that the development environment...

Deployment Solutions

To deploy a solution, you copy appropriate files to a specific location, register those files in the registry, or maybe make configuration changes to another file , create shortcuts and Start menu entries, and possibly make changes to a log file that can be viewed later. However, it does not end there because well-behaved code will also provide uninstall information so that the user can completely remove the code from her system. Removing code is often far more difficult than installing it...

Class Declaration

The_gc and_value keywords have a profound effect on C classes. Without either of these, a C class will be allocated according to normal C rules When you declare the variable as an auto variable, it will be allocated on the stack and when you explicitly allocate it with the C new operator, the object will be created on the C heap. This is true even if the code is compiled with clr. Applying_gc indicates that instances of the class are allocated on the GC-managed heap, which means that you can...

Boxing

Console WriteLine Sthe value is 0 , 42 This seemingly innocuous code will not compile. The reason is that the static method Console WriteLine has many overloaded forms, but none that take a string and an integer. The nearest form looks like this csAppearance Color.Wheat, Color.Blue , csAppearance Color.LightYellow, Color.Blue , csAppearance Color.Black, Color.Brown , csAppearance Color.Brown, Color.Green static void WriteLine String , Object The format string is passed in the string parameter,...

ATL Server Project and ATL Server Web Service

The C C category of the Project wizard includes the following two entries for creating wizards ATL Server Project and ATL Server Web Service. In fact, these wizards are the same, which is why I am covering both of them in this section. Figure 7.1 shows the relationship between the ATL Server ISAPI and IIS. When an HTTP request comes into IIS, it is handled by a thread taken from a pool maintained by IIS. This thread will locate the ISAPI DLL that has been registered in the IIS metabase to...

CRT and ATL Memory Debugging

ATL utilizes the CRT memory debugging routines to allow you to detect leaks and damage to memory caused by buffer overruns. The APIs to do this can be found in atldbgmem.h. To use this file you should include it before any included file that uses ATL, so typically you will include it before you include stdafx.h. However, because the debugging routines use the conversion routines in atlconv.h, which itself uses types defined in wtypes.h, you have to include these two files as well. In addition,...

Calling Managed Code

Your unmanaged code is allowed to host the .NET Runtime 21 . That is, Microsoft has provided the code that allows you to load and initialize the runtime, create an application domain, and load .NET objects. This code is naturally provided through a COM API. The prototypes for these functions and interfaces can be found in mscoree .h, and they are exported from mscoree.dll. 2 For a more complete description of this mechanism, see Steven Pratschner's article Implement a Custom Common Language...

Figure 35 Context sink chains

The context sinks are used to process messages as they enter or leave the context. Context sinks implement the IMessageSink interface and are created in response to a call to a context property. To create a context property, you derive an attribute from ContextAttribute and implement either IContributeClientContextSink or IContributeServerContextSink or both , and then apply this attribute to the class that needs to use the custom context. The runtime calls GetClientContextSink or...

Figure 921 The failedassertion dialog

Program File c temp asserttest asserttest.cpp Line 8 For information on how your program can cause an assertion failure, see the Visual C documentation on asserts. Press Retry to debug the application The failed-assertion dialog has three buttons. If you click the Abort button, the program will be aborted immediately. If you click the Ignore button, processing will continue, but you should be aware that something is wrong in the process, so an exception could occur later. The final button,...

Hosting Remote Objects in IIS

Earlier in this section I showed the minimal code for a remote server. It looked rather sparse, with just a call to RemotingConfiguration.Configure and a call to Console.ReadLine to prevent the server from finishing early. Because the configuration file has all the information about the channels to register and the objects that will be available as remote objects, it seems that there ought to be a standard server process that exists simply to call RemotingConfiguration.Configure . Such a...

Figure 98 The Locals window showing a linked list

Public List next null name null public string name public List next static public List AddLink List node, string s if node null node.next current public List next null name null public string name public List next static public List AddLink List node, string s if node null node.next current This class has a member that is a reference to another node in the linked list. The class could be used as follows List beatles null List current null beatles current List.AddLink null, John current...

ATL Object

There is one nagging issue that I need to address at this point the object map. In the old ATL3 days, the object map was a map implemented in the main .cpp class. The Object wizard would update this map when it added a class to a project, but if you changed the class or indeed removed it from your project, the onus was on you to change the class's entry in the object map. This meant that a piece of information about the class was stored in a location completely separate from the class. This...