Any good tutorials on using COM from C#? [closed]

2019-01-21 01:27发布

问题:

For one of a side-projects i need to write a C# app that required to use a third-party INPROC COM object. Unfortunately, C# is not my primary programming language, so my knowledge is a bit limited. Is it any good tutorials available on how to access COM object from C#? The usage of this third-party COM object requires me to create implementation of specified COM interface and supply that implementation into COM object in order for it to function.

回答1:

There is a good book called .NET and COM: The Complete Interoperability Guide that I am about to start reading as I want a better understanding of COM interop. Maybe it would be useful for you too, check it out.

The focus of the book is on COM Interoperability (since it's a much larger subject), and the heart of the discussion is broken down into four parts:

* Using COM Components Within the .NET Framework
* Using .NET Framework Components from COM
* Designing Good .NET Framework Components for COM Clients
* Designing Good COM Components for .NET Framework Clients

The scope of the book is just about everything related to using "unmanaged code" in the .NET Framework. Technologies built on top of COM Interoperability are also covered-Interoperability of Windows Forms Controls and ActiveX controls, Interoperability with COM+, and Interoperability with Distributed COM (DCOM). Although Platform Invocation Services is a separate technology from COM Interoperability, there are many areas of overlap, so including in the book is a natural fit. All of these technologies are a core part of the Common Language Runtime and .NET Framework, and will likely be used not only as the path of migration for existing software projects, but for brand new software development for the next several years

Also have a look at this (legally) free pdf ebook COM and .NET Interoperability which you can download from TheServerSide.com. You have to register before you can download, but you can just bugmenot if you don't feel like it.



回答2:

I have quite a large .NET <-> COM bookmark collection that I'm dumping below.

The problem is that no one tutorial/article covers all the basics and gotacha's. So you need to go through a couple of them and mix and match, depending on what you want to do.

Here's the best of what I've found so far...

General .NET COM Articles/Tutorials

  • .NET - COM Interoperability.
  • COM Interoperability in .NET Framework: Part I.
  • Understanding Classic COM Interoperability With .NET Applications.
  • Exposing .NET Components to COM.
  • Building COM Objects in C#.
  • Calling Managed .NET C# COM Objects from Unmanaged C++ Code.
  • Native & Managed Interop Made Simple.
  • Using .NET Assembly with COM Client.
  • Moserware: Using Obscure Windows COM APIs in .NET
  • COM Interoperability in .NET Part 1 Part 2 Part 3
  • COM Interop Part 1: C# Client Tutorial Part 2: C# Server Tutorial
  • Calling a COM Component From C# (Late Binding)
  • Building COM Objects in C#

MSDN on .NET & COM

  • COM Interop Tutorials (C#)
  • Exposing .NET Framework Components to COM
  • Interop Marshaling
  • Advanced COM Interoperability
  • How to: Register a Component for COM Interop

ActiveX in .NET

  • Internet Explorer ActiveX Control C# Class Library
  • Create ActiveX in .NET Step by Step.
  • Dynamically adding ActiveX controls in managed code.
  • Exposing Windows Forms Controls as ActiveX controls.
  • Importing and Extending ActiveX Controls in .NET.
  • C# Tutorials - ActiveX With C#
  • CodeGuru: Extensible OLE Property Pages in .NET
  • Writing an ActiveX Control in .NET
  • Hosting ActiveX Controls in the .NET Compact Framework 2.0
  • Hosting Windows Forms Controls in COM Control Containers - O'Reilly Media


回答3:

Maybe this is useful for you: http://msdn.microsoft.com/en-us/library/aa645736%28VS.71%29.aspx



回答4:

The basics are pretty straightforward, and MSDN has good reference information.

In your C# project, use "Add Reference" to add a reference to the COM component.

If you need to implement a COM interface, create a class that inherits from it:

class MyClass : IMyComInterface
{
   ...
}


标签: c# com