Chapter 19. Interop with COM and Win32

Programmers love a clean slate. The thought of throwing away all the code we’ve ever written and starting over can seem alluring, but this typically isn’t a viable option for most companies. Many development organizations have made a substantial investment in developing and purchasing COM components and ActiveX controls. Microsoft has made a commitment to ensure that these legacy components are usable from within .NET applications, and (perhaps less important) that .NET components are easily callable from COM. The ability to mix managed .NET code with unmanaged code from the older worlds of Win32 and COM is called interoperability, or as it’s usually abbreviated, interop.

This chapter describes the support .NET provides for using ActiveX controls and COM components into your application, exposing .NET classes to COM-based applications, and making direct calls to Win32 APIs. You’ll also learn about C# pointers and keywords for accessing memory directly, which can be necessary for using some unmanaged APIs.

Importing ActiveX Controls

ActiveX controls are COM components designed to be dropped into a form. They usually have a user interface, although you may come across nonvisual controls. When Microsoft developed the OCX standard, which allowed developers to build ActiveX controls in C++ and use them with VB (and vice versa), the ActiveX control revolution began. That was way back in 1994, and since then thousands of such controls have been developed, ...

Get Programming C# 4.0, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.