Use. NET components in COM applications

Source: Internet
Author: User

Reprinted: original address: http://blogs.msdn.com/silverlightshanghai/archive/2008/07/23/com-net.aspx


In the previous blog, we used Visual Studio to convert the. NET Component (managed component) into a COM server. This topic continues to show you how to use the. NET toolkit to complete relevant functions.

First, let's take a look at the relationship between today's topic and CLR InterOP. In our group, the term InterOP contains four categories: P/invoke, reverse P/invoke, com InterOP and reverse com InterOP. The first two concepts implement interoperability in the native world and in the. NET world through the dynamic connection library (DLL); the last two concepts, as the name suggests, are dealing with COM. Among them, com InterOP uses COM components in. NET applications; reverse com InterOP refers to the use of. NET components in COM applications. The concept is a bit of a tongue twister. Don't rush to look at it.

Some may ask why. Net programmers need to deal with COM technology for a long time? The answer is the word "component. For example, a cool man wrote a program several years ago with excellent scalability. He used com to clearly define the interface of the plug-in, and we wanted to use. NET as the plug-in...

Write a. NET component for com. You can refer to the following Trilogy

1. Define the. NET interface and write. Net class

2. Deploy. NET components

3. Write a COM Client

The first step is very familiar to developers who often work in. net. Here is an example.

CSC/Target: Library A. CS

Second, to deploy the. NET component, there are two aspects:

Regasm A. dll/TLB

1. Export the Type Library. For a com application, it only knows the Type Library and is a binary "standard" followed by COM components ". The/TLB option tells regasm to export the Type Library. We can use oleview to view the content of TLB, as shown in. Net interfaces and classes previously defined are in it.

 

2. Place the DLL generated in step 2 to the Registry. COM does not understand concepts such as GAC. Instead, it queries hkey_classes_root/cls_id/commandid -0000-0000-ffff-000000000004 through the Registry. In inprocserver32, the dynamic link library of COM points to mscoree. DLL, which is the legendary Shim. It will load the common language runtime and find the real. NET Component ----. DLL.

 

With the above two steps, we can use the. NET Component in the com application. Writing COM components is beyond the scope of this article. If you are interested, refer to the comments in the code.

CL client. CS

  1. // Client. CS
  2. # DEFINE _ win32_com
  3. # Include <stdio. h>
  4. # Include <wtypes. h>
  5.  
  6. # Import "A. TLB" no_namespace named_guids raw_interfaces_only
  7. // A. TLB is exported in step 2
  8. Int main ()
  9. {
  10. Iunknown * punk = NULL;
  11. Ia * Pia = NULL;
  12. Hresult;
  13.  
  14. // 0. initialize the COM Component
  15. Hresult = coinitializeex (null, coinit_multithreaded );
  16. If (failed (hresult ))
  17. {
  18. Printf ("error: cannot initialze COM: 0x % x/N", hresult );
  19. Return-1;
  20. }
  21.  
  22. // 1. Obtain the iunknown Interface
  23. Hresult = cocreateinstance (clsid_a, null, clsctx_inproc_server, iid_iunknown, (void **) & punk );
  24. If (failed (hresult ))
  25. {
  26. Printf ("error: cannot get the iunkown interface: 0x % x/N", hresult );
  27. Return-1;
  28. }
  29.  
  30. // 2. Obtain the IA Interface
  31. Hresult = punk-> QueryInterface (iid_ia, (void **) & PIA );
  32. If (failed (hresult ))
  33. {
  34. Printf ("error: cannot convert to idispatch interface: 0x % x/N", hresult );
  35. Punk-> release ();
  36. Return-1;
  37. }
  38.  
  39. Punk-> release ();
  40.  
  41. // 3. Call the. NET Component
  42. Hresult = Pia-> Hello ();
  43. If (failed (hresult ))
  44. {
  45. Printf ("error: invoke failed: 0x % x/N", hresult );
  46. }
  47.  
  48. // 4. Clear
  49. Pia-> release ();
  50. Couninitialize ();
  51. Return 0;
  52. }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.