C ++ and C # compile and call COM components

Source: Internet
Author: User

C ++ and C # compile and call COM components

Source: http://bbs.gameres.com/showthread.asp? Threadid = 122865

Abstract: COM components are increasingly widely used.ProgramI think everyone has encountered this article.ArticleThe compilation and calling of COM components mainly include compiling and calling COM components using vc6.0, and compiling and calling COM components using C # In vs2005, and call COM components between vc6.0 and vs2005.

Body:

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.

ProgramCode:

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 )"

Figure 1 settings of the Public Language Runtime Library

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.

Figure 2 com generation settings

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 Ladd (float a, 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); // you can change the value of null to 0.

Addcom: addcominterfaceptr p_add (_ uuidof (addcomservice ));
Dresult = p_add-> iadd (1, 2 );
Fresult = p_add-> FADD (1.2, 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;

Figure 3 Reference com

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); // you can change the value of null to 0.
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 ();

}< br>
conclusion: 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.