Calling Unmanaged Code
OBJECTIVES
This chapter covers the following Microsoft-specified objective for the "Creating and Managing Microsoft Windows Services, Serviced Components, .NET Remoting Objects, and XML Web Services" section of Exam 70-320, "Developing XML Web Services and Server Components with Microsoft Visual C# .NET and Microsoft .NET Framework":
Access unmanaged code from a Windows service, a serviced component, a .NET Remoting object, and an XML Web service.
Although the .NET Framework can handle nearly all your application development needs, most organizations have already accumulated a large amount of useful code before they begin using the .NET Framework. It doesn't make sense to simply throw away this legacy code and rewrite everything from scratch. Fortunately, if you've followed recommendations to encapsulate your code into components over the years, you don't need to abandon old code to start getting the benefits of the .NET Framework. Instead, you can make use of the .NET Framework's interoperability features to use the following types of legacy code in Windows services, serviced components, .NET remoting objects, and XML Web services: Component Object Model (COM) components can be instantiated and invoked by .NET code. The .NET Platform Invoke capability (usually referred to as PInvoke) can be used to call the Windows application programming interface (API).
By using these interoperability features, you can ease your migration to .NET development. Making use of legacy components from .NET code means that you can migrate an application piece by piece rather than try to do it all at once.
OUTLINE
Introduction Using COM Components Understanding Runtime Callable Wrappers Building a COM DLL Registering a COM DLL Using the Type Library Importer Tool (tlbimp.exe) Using COM Components Directly
Using Platform Invoke Chapter Summary Apply Your Knowledge
STUDY STRATEGIES
If you have an existing COM object to work with, create a runtime callable wrapper (RCW) for the object to investigate the conversion process. If you don't have any existing objects, you can build one with an older version of Visual Basic or Visual C++. Experiment with PInvoke to invoke some common Windows API calls.
|