.NET technology FAQ (Nine) ----- About COM

xiaoxiao2021-03-06  62

9. Did it done about COM9.1 COM? My understanding is: COM contains a lot of content, and it is different for different people. But for me, COM is basically about how a small code is found to find another small code, and how to communicate with each other after they find each other. COM accurately indicates how this positioning and communication is going. In the "pure" .NET world consisting entirely of .NET objects, the small code is still looking for each other and talks with each other, but they don't use COM. They use a model that is very similar in certain places and COM - for example, type information saved in and component encapsulated together, which is very similar to the package in COM components. But it is not COM. So, is there any problem? Ok, I don't care about most COM disappeared - I don't care about the component is no longer related to the registry, I don't use IDL to define my excuse. But there is something I don't want it to disappear - I don't want to lose this idea based on the interface-based development. According to my opinion, COM's most powerful power is it insisted on erecting iron wall between interfaces and implementations. Unfortunately, it seems that .NET is no longer in that - it allows you to do an interface-based development, but it doesn't stick to it. Some people may argue that there is always a bad thing, maybe they are right, but I can't think that this may be a retreat. Did the DCOM died? Almost, especially for .NET developers. The .NET frame has a new remote model that is not DCOM. Of course, DCOM will also be used in interoperability. 9.3 MTS / COM done? Do not. The first .NET version considers the provision of existing COM services (via an interoperability layer) instead of using .NET's own services to replace them. Many tools and properties are used to achieve as smooth transition as possible. The PDC version of .NET SDK includes support for core services (JIT activities, transactions), but excludes some high-level services (such as COM events, queue components). In a period of time, interoperability can be expected to be seamless integration - this means that some services will become part of the CLR, and / or means that some services will be override and run in CLR in the form of manageable code. Top floor. 9.4 Can I use COM components in .NET? can. You can access the COM components from the .NET via the Runtime Callable Wrapper (RCW). It allows the COM interface to be accessed by mapping COM components to interfaces with .NET compatible interfaces. For the OldAutomation interface, you can automatically generate from a type library. For non-OleAutomation interfaces, a custom RCW can be developed to manually map the type of COM interface to the type with .NET compatible. For readers who are familiar with ATL, there is a simple example. First, create an ATL component to implement the following IDL: import "oaidl.idl"; import "ocidl.idl"; [Object, UUID (EA013F93-487A-4403-86EC-FD9FEE5E6206), Helpstring ("icpname interface", Pointer_Default (unique), oleautomation]

Interface icpname: IUNKNOWN {[Helpstring ("Method SetName")] HRESULT SETNAME ([IN] BSTR Name); [Helpstring ("Method getName"] HRESULT GETNAME ([OUT, RETVAL] BSTR * PNAME);

[Uuid ​​(F5E4C61D-D93A-4295-A4B4-2453D4A4484D), version (1.0), helpstring ( "cppcomserver 1.0 Type Library")] library CPPCOMSERVERLib {importlib ( "stdole32.tlb"); importlib ( "stdole2.tlb"); [UUID (600CE6D9-5ED7-4B4D-BB49-E8D5D5096F70), Helpstring ("CPPName Class")] CoClass cppname {[default] interface icpname;};}; after the component is established, you will get a TypeLibrary. Run the TLbimp utility on TypeLibrary, like this: TLBIMP CPPCOMSERVER.TLB If you succeed, you will get information like this: Typelib Imported successful to cppcomserverlib.dll Now you need a .NET client - we create one with C # Create a following .cs code file: using System; using CPPCOMSERVERLib; public class MainApp {static public void Main () {CppName cppname = new CppName (); cppname.SetName ( "bob"); Console.WriteLine ( "Name is" cppname .Getname ());}} Note We use TypeLibrary name as named space, the name of the COM class as class name. We can also choose to use cppcomserverlib.cppname as class names and do not need statements for CPPCOMSERVERLIB. Like this compiled above C # code: CSC /R: CppcomServerLib.dll Csharpcomclient.cs Note that compiling is notified, quote We just used Tlbimp from Typelibrary DLL. Now you should run csharpcomclient.exe, and get the following output from the console: Name is Bob 9.5 can use the .NET component in COM? can. The .NET component can be accessed from the COM via a COM CALLABLE WRAPER (CCW). This is similar to RCW (see the previous issue), but work in the opposite direction. Similarly, if it cannot be automatically generated by .NET development tool, or do not want to automatically generate behavior logic, you can develop a custom CCW. To enable COM to "see" .NET components, .NET components must be registered in the registry. Here is a simple example. Create a C # file called TestComServer.cs and enter the following code: use system;

namespace AndyMc {public class CSharpCOMServer {public CSharpCOMServer () {} public void SetName (string name) {m_name = name;} public string GetName () {return m_name;} private string m_name;}}

Then compile

.CS

file:

CSC / TARGET: Library TestcomServer.cs

You will get a DLL

Let it register it:

Regasm testcomserver.dll /tlb:TestcomServer.tlb

Now you need to create a client program to test your

.NET COM

Components.

VBScript

can

-

Put the following in a name

Comclient.vbs

In the file:

DOTNETOBJ SET DOTNETOBJ = CreateObject ("Andymc.csharpcomserver") DotNetobj.setName ("bob") msgbox "name is" & dotNetobj.getname ()

Run this script:

WScript Comclient.vbs

Hey there! You get a display text

"Name is Bob"

Message box.

(

Note that when writing this program, it seems to pass several paths.

.NET

Class

COM

Component Access

-

In order to avoid problems,

TestcomServer.dll

Running in the same directory

Comclient.vbs

.

An alternative method is to use

Jason Whittington

with

DON BOX

developing

DM.NET MONIKER

.

9.6

in

.NET

In the world

ATL

Is it extra?

Yes it is. If you are writing

.NET

Application within the framework. Of course, many developers want to continue to use

ATL

To write

.NET

Frame

C COM

Components, but when you are

.NET

You are almost always want to use in the frame.

C #

. in

.NET

In the world, the original

C (

And based on it

ATL)

Not too many status

-

It is too direct and provides too much adaptability, so that the runtime cannot manage it.

转载请注明原文地址:https://www.9cbs.com/read-119081.html

New Post(0)