Scott hanselman
Corillian Corporation
May 28,200 4
Summary:Does a solution written for Microsoft. NET have to be 100%. Net? Scott hanselman looks at how hybrid managed-unmanaged solutions are really the norm. (9 printed pages)
Related books
It's spring of 2004. the Microsoft. NET framework is currently supported with Microsoft Windows Server 2003, and we know that combination of the rich services in the upcoming release of Microsoft Windows code-named "Longhorn" will be built with managed APIs. some folks have asked the question, "Is Longhorn managed? "Implying that it wocould be a great thing if huge parts of the operating system were implemented in managed code. When they hear that not all of it is managed, they are somehow disappointed.
Don box put it simply: "It doesn' t matter. what does matter is that... the primary access mode... is managed. "Do I care if my device driver is written in managed code or in C? No, I care that it works, and works well.
There is an increasing amount of discussion around the topic of ". Net purity" in development circles. When selling an application, the question often arises, "is your application 100 percent. Net? "Or," how much of your application is. Net? "There is an implied qualitative judgment behind these questions, and it's usually pejorative.
I 've heard it said by using a CTO in using a technical briefing that, "we're planning to port our whole system. net. "Why spend 18 months converting your application, so you can arrive at the endpoint you're already?
The implication is that an application entirely written in. net, presumably without any interoperation with COM or direct callto the Win32 API, is superior to an application that is a combination of technologies. the tragic irony of the goal of "as little InterOP as possible" is that the very. net Framework you're building upon is itself a fantastic example of interoperability.
. Net represents a fantastic leap in developer productitasks, and puts a clean, consistent face on the services that the Windows platform provides. for policyears, the set of interfaces provided by the Windows platform-collectively known as the windows SDK-have been exposed to developers as exported "C"-style functions in DLLs, and in recent years, through the Component Object Model (COM ). and things got more complicated, and new access modes and synchronized actions were introduced.
MFC wrapped Win32 in a way that was different from the Microsoft Visual Basic 6.0 view, or the Windows Template Library (wtl) view. classic Microsoft ASP had no eventing model at all build on top of HTTP, while Visual Basic encouraged folks to double-click and get their events wired upAutomagically. With. net Framework, includifferent "Access Modes" have been uniied under one programming model. there is one access mode; it's. net Framework managed APIs sitting on windows, "the platform."
Figure 1. the. NET "Platform"
. NET Framework library
The Windows platform has dozens and dozens of high-level system services exposed by literally thousands of APIs. this large library of functionality encompasses varous levels of richness. A low-level API may open a file off a disk, while a high-level one might play an audio file. the designers of. net Framework wanted to create a consistent object-oriented face on a rich legacy of platform functionality. the Clr and. NET framework work together to expose the capabilities within the Windows platform, including those that may have previusly been hidden away in difficult or little known APIs.
While the CLR provides a new paradigm for application development, it does not close the door on existing libraries. the CLR provides InterOP services to the developer, but the biggest consumer of these services is certainly. net class libraries themselves, which unlock existing windows platform abilities through. net APIs.
Figure 2. The platform invoke model
For example, when sending e-mail using the. NET Framework library class,System. Web. Mail. smtpmail, The class library uses a helper class that implements acts the existing CDO (Collaboration Data Objects) com library. this is just one example of thousands where. net Library developer chose to rely on a production-ready and reliable existing library rather than write something from scratch. this example and dozens of others with the library not withstanding, the common language runtime still at some point needs to work with the Windows internal APIs. as the saying goes, no matter what you're working on, eventually someone has to callLoadlibrary ().
If Microsoft were to truly implements alize the machine, they wowould have marginalized their investigation in the Windows platform.
Certainly it behooved the designers to make transitions to existing libraries as painless as possible. they have enabled this with net/COM InterOP through both runtime-and com-callable wrappers, the ability to tap into standard Win32 platform APIs through a technology called P/invoke (short for platform invoke), as well as other options. when writing code hosted in the CLR, the vast resources of platform are just sitting under the developer-the runtime is transparent rather than virtual. this marks a fundamentally different view of the platform than other implements alizing machine implementations.
GDI, the graphics device interface, is an excellent example of an underlying unmanged service that has been seamlessly brought into the managed world almost entirely with P/invoke. it's useful to list out the current "three pillars of Windows" in a table to remind readers where most of the unmanaged work is still happening, no matter how pure you are.
DLL |
Description of its contents |
Kernel32.dll |
Contains low-level operating-system functions for unmanaged memory management and handling of resources. |
Gdi32.dll |
Graphics Device Interface (GDI) functions for drawing, font management, and general device output. |
User32.dll |
Windows management functions for message handling, menus, and communications. |
If I wanted to use the internal unmanagedFindwindowMethod in user32.dll, I 'd add a declaration to my C # application like this:
[Dllimport ("user32.dll")] public static extern int findwindow (string strclassname, string strwindowname );
After I 've made this declaration (which notably differs from a typical. net function definition only byDllimportAttribute and perhaps the useStatic extern), I can happily call this method like any other. certainly there are tricks and tribulations around und grouping alling between unmanaged and managed code, but for the most part, it just works. the proof that it works is in the call stack of your own code, which is constantly jumping into unmanaged support DLLs with you rarely having to think about it.
While creating a fresh application using only. NET may offer some benefits in the arenas of deployment or marketing architecture ("marketecture"), these benefits may not be worth the trouble when weighed against the cost of rewriting non -. NET components in. net when those legacy components cocould have been leveraged. A "pure ". net solution can only make use of either those pieces of functionality that can be achieved entirely within the runtime, or those functions that have been exposed by the base class library-which itself uses com InterOP and P/invoke.
The. NET Framework library itself isn't "Pure. net," as it uses every opportunity to take full advantage of the underlying platform primitives.
The whole concept. net purity is rendered specious in this new light. the. net Framework is certainly the best way to create business components on the Windows platform, but any applications written with. net Framework are only lifted as high as the underlying Windows operating system services.
Why write managed code at all?
Why shocould one move up a step in the language and environment ladder? In order to live and work at another level of each action. certainly. net CLR provides an improved object-oriented experience over Visual Basic 6.0 and even possibly C ++, but the framework's real value is in its ability to define tively hide the underlying operating system and system APIs and then expose the new operating action layer to a multitude of versions. of course you won't write a real-time device driver in managed code (yet), but I believe towards the middle of this decade, nearly all new business-focused code of any significance will be written in a managed environment. (if this is not already true .)
The Clr and managed runtimes aim to lift the developer out of the drudgery of memory management, low-level Io and storage, and even wire protocols. in the days of C ++, we talked a good talk about objects being "closer to the business person" and "easier to design with" but our object-oriented SiON was shattered when it came time to serialize the object, remote its data easily, or save it to a database. service-Oriented Architecture (SOA) begins to reconcile the responsibilities of a logical service versus the objects (messages) It acts upon, but this move forward wouldn't have been feasible without extensible, complete, and pervasive metadata, and a uniied type system. we 'd have been too mired in the details. that's why writing managed code is a no-brainer.
Common Language Runtime or virtual machine?
Often. NET Common Language Runtime, or CLR, is directly compared to the Java Virtual Machine. initially, there are using clear parallels: both are "managed" environments that provide a component container, both consume a "partially chewed" intermediate language, both provide low-level services like garbage collection and threading conveniences.
While these parallels are semantically correct on a superficial level, these two implementations differ fundamentally in philosophy. comparing the CLR to the virtual machine is reasonable only to a certain point-their specified tural goals are ultimately different.
Sun Microsystems promotes a marketing program called 100% pure Java, which is certainly appropriate if code portability and underlying operating system transparency is a desirable endpoint. however, incluthird-party Java application servers create a competitive advantage by judicious use of "C" function CILS directly down (through Java Native Interface or JNI) into their host operating systems value-added services that are not exposed by the Java application platform (the Java class library ). calling into the core platform is the only way to make use of base functionality that is only presented through a Native Interface.
Figure 3. the Java "stack"
the Java Virtual Machine is truly a "Virtual Machine" the ultimate goal of which is to abstract (abstralize) away the underlying operating system and provide an idealized (not necessarily ideal, but idealized) environment for development. the Java virtual machine is also intimately united with the API, the Java application platform, with services provided by the Virtual Machine implementation. regardless of where you run your compiled Java code, you will run within the context of the Virtual Machine and ostensibly link with supplied Java platform APIs.
The. NET Common Language Runtime is well named, as it is used more as a Language Runtime than a virtual machine. while it successfully tries acts away aspects of underlying hardware through its use of an intermediate language, when the CLR is combined with. net Framework library of APIS, It is married to the underlying platform, which is windows. the CLR provides all the facilities of the Windows platform to any. net-enabled language.
It's also worth noting that all code running in. net managed environment is in fact running compiled (JIT 'ed) as native instructions, thus providing a balance between flexibility and performance, cannot acted away from the hardware, but at the same time running "close to the metal."
"Hybrid" solutions provide real solutions
Extends large existing applications are written in Microsoft Visual C ++ and COM. they are written "close to the metal" to take full advantage of Native Windows multi-threading and fine-grained (not automatic) memory management. however, new business components may also be written in. NET language such as C # Or Visual Basic. net. the existing system then hosts. NET Common Language Runtime within its process space and "interops. "an interface that uses com InterOP only incurs minimal overhead of between 10 and 40 processor instructions per in-Proc call (mileage may vary ).
. NET components hosted within the legacy application can take advantage of that application's existing services. lower-level developer features, such as memory management, object lifetime, and object orientation, are provided by the CLR, while higher-level, vertical-specific business functionality is exposed through the legacy application.
More usefully, an application's existing unmanaged services can be presented with a fresh perspective as a managed and possibly service-or object-oriented API. the fact that the Service's internal workings are ultimately unmanaged is just that-an implementation detail.
This "hybrid" model can (and does) provide a best-of-breed solution on the Windows platform, exploiting both the high-performance low-level APIs through C ++, and the highly componentized features of. net Framework. these solutions can work very successfully while companies shift their focus to developing entirely. net.
Http://msdn2.microsoft.com/en-us/library/ms953313.aspx