C ++ and C # compile and call COM components

Source: Internet
Author: User

When I was working on a project a while ago, I encountered the call and use of the COM component. At that time, I had a good research, so I had to get through the intermediate link. Now I wrote it to help you, there are four types:

1. In vs2005, C # compiles a DLL and calls it using C ++.

2. Use vc6.0 to call the COM component written in C # In vs2005

3. Compile COM components in vc6.0 and use vs2005 C # To call

4. Compile COM components in vc6.0 and use vc6.0 to call

Two programs are written for each type. One is a COM component program and the other is a calling program.

Program Implementation:

1. In vs2005, C # compiles a DLL and calls it using C ++.

(1) C # compile the DLL Program

Create the C # DLL program adddll. The project type is class library.

Program code:

using System;using System.Collections.Generic;using System.Text;namespace AddDll{    public class Add    {        public int iadd(int a, int b)        {            int c = a + b;            return c;        }    }}      

(2) c ++ compile the calling program

Create the C ++ Win32 console application usedll. The project type is Win32 console application.

Configuration: Right-click usedll in Solution Explorer and select "properties". Set the support for the Common Language Runtime Library to "Support for the Common Language Runtime Library (/CLR )"

Program code:

#include "stdafx.h"#include "stdio.h"#using "../debug/AddDll.dll"using namespace AddDll;int _tmain(int argc, _TCHAR* argv[]){        int result;        Add ^add = gcnew Add();        result = add->iadd(10,90);        printf("%d",result);        scanf("%s");        return 0;}      

2. Use vc6.0 to call the COM component written in C # In vs2005

(1) Use C # To compile COM components in vs2005

Create a COM component compiled by C #. The project type is class library.

Configuration: Right-click addcom in Solution Explorer, select "properties", select "generate", and select "register for com InterOP (_ p )"

Open the assemblyinfo. CS file and set [Assembly: comvisible (true)]

This allows you to generate the addcom. TLB file.

Program code:

Using system; using system. Collections. Generic; using system. Text; using system. runtime. interopservices; namespace addcom {// you can use the "tool/GUID generation" in the // menu ". // Select define guid {....} Format, and save it in full //. Save it wherever it is, notepad or something. // Because it is used in the VC program. [GUID ("298d881c-e2a3-4638-b872-73eade25366c")] public interface addcominterface {[dispid (1)] int iadd (int A, int B); [dispid (2)] float add (float, float B);} [GUID ("2c5b7580-4038-4d90-babd-8b83fce5a467")] [classinterface (classinterfacetype. none)] public class addcomservice: addcominterface {public addcomservice () {} public int iadd (int A, int B) {int C = 0; C = A + B; return C;} public float Ladd (float a, float B) {float C = 0; C = a + B; return C ;}}}

(2) write the calling program using vc6.0

Use vc6.0 to write and create the MFC Application usecom. The project type is MFC Appwizard (exe)

Add the following in stdafx. h:

# Import "addcom. TLB" using namespace addcom;
Program code:
Void cusecomdlg: onbuttonuse () {// todo: add your control notification handler code here int dresult; float fresult; cstring strresult; coinitialize (null ); // if the value of null is 0, addcom: addcominterfaceptr p_add (_ uuidof (addcomservice); dresult = p_add-> iadd (1.2); fresult = p_add-> FADD, 2.3); strresult. format ("int: % d/nfloat: % F", dresult, fresult); MessageBox (strresult, "calculation result", mb_ OK); couninitialize ();}
3. Compile COM components in vc6.0 and use vs2005 C # To call

(1) Write COM in vc6.0

Use vc6.0 to create a COM component. Project type: atl com Appwizard

Program code:

Interface:
Interface iadd: idispatch {[ID (1), helpstring ("method iadd")] hresult iadd ([in] int A, [in] int B, [out] int * C); [ID (2), helpstring ("method FADD")] hresult FADD ([in] float a, [in] float B, [out] float * C); [ID (3), helpstring ("method isub")] hresult isub ([in] int A, [in] int B, [out] int * C );};
Implementation:
STDMETHODIMP CAdd::iadd(int a, int b, int *c){        // TODO: Add your implementation code here        *c = a + b;        return S_OK;}STDMETHODIMP CAdd::fadd(float a, float b, float *c){        // TODO: Add your implementation code here        *c = a + b;        return S_OK;}STDMETHODIMP CAdd::isub(int a, int b, int *c){        // TODO: Add your implementation code here        *c = a - b;        return S_OK;}      

(2) vs2005 uses C # To compile a calling program (website Program)

Use vs2005 to create website usecom

Configuration: Right-click the home directory in Solution Explorer, select Add reference, select com, and add the created addcom 1.0 Type Library

COM component to be compiled by using in the program: Using addcomlib;

Program code:

Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using addcomlib; public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {} protected void buttoncom_click (Object sender, eventargs e) {Add add = new add (); int iresult; float fresult; int sresult; add. iadd (10, 20, out iresult); add. FADD (float) 1.2, (float) 2.3, out fresult); add. isub (100, 10, out sresult); textboxresult. TEXT = iresult. tostring (); textboxre2.text = fresult. tostring (); textboxre3.text = sresult. tostring ();}}
4. Compile COM components in vc6.0 and use vc6.0 to call

(1) Compile COM components in vc6.0

Use vc6.0 to create a COM component. Project type: atl com Appwizard

Program code:

Interface:
Interface iadd: idispatch {[ID (1), helpstring ("method iadd")] hresult iadd ([in] int A, [in] int B, [out] int * C); [ID (2), helpstring ("method FADD")] hresult FADD ([in] float a, [in] float B, [out] float * C); [ID (3), helpstring ("method isub")] hresult isub ([in] int A, [in] int B, [out] int * C );};
Implementation:
Stdmethodimp CADD: iadd (int A, int B, int * c) {// todo: add your implementation code here * c = a + B; return s_ OK;} stdmethodimp CADD:: FADD (float a, float B, float * c) {// todo: add your implementation code here * c = a + B; return s_ OK;} stdmethodimp CADD :: isub (int A, int B, int * c) {// todo: add your implementation code here * C = A-B; return s_ OK ;}

(2) write the calling program using vc6.0

Use vc6.0 to create the MFC Application usecom and call the COM component just created

Put the addcom. dll generated by the above program addcom into the project directory and program generation directory of the program.

Add the following to stdafx. h:

# Import "addcom. dll" no_namespace

Program code:

Void cusecomdlg: onbuttonuse () {// todo: add your control notification handler code here cstring strresult; coinitialize (null); // if the value is null, it can be changed to 0 or iaddptr m_add = NULL; hresult hR = s_ OK; HR = m_add.createinstance (_ uuidof (ADD); int d_a = 90; int d_ B = 10; int d_c; int D_d; float F_a = 1; float f_ B = 2; float f_c; m_add-> _ iadd (d_a, d_ B, & d_c); m_add-> FADD (F_a, f_ B, & f_c ); m_add-> isub (d_a, d_ B, & D_d); strresult. format ("returned result: % d; % F; % d", d_c, f_c, D_d); MessageBox (strresult, "result", mb_ OK); m_add.release (); m_add = NULL; couninitialize ();}

Conclusion: I hope to help you!

Related Article

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.