C ++ calls the C # com component (1): A complete small example

Source: Internet
Author: User

We know that C # development efficiency is very high, especially for some beautiful pages. if you want to develop a desktop application with better performance and page highlights, you can use C ++ and C # together. now that NetFramework is integrated in Windows 7, you don't have to worry about installing a C # application and then installing the entire NetFramework. you can use C # To make a com component, and then C ++ calls it. the opposite is true. C ++ can be made into com, and then C # will call it.
Here is a simple example. I use VS 2008 and the operating system is Win 7.
 
First, create a C # com component. The implementation function is very simple, that is, the sum of the two numbers is returned.
 
1. Create a new C # Project and select Class Library for the type. The name is ArwenAddCom.
2. Add an interface in the project named IComTest. C ++ to call all functions of the com component, which can only be called through the interface. classes or functions cannot be called directly.
Code:
Using System. Runtime. InteropServices; // remember to add this namespace
Namespace ArwenAddCom
{
[Guid ("DA07B88D-29F0-41cf-B3D3-611010E6F3FF")] // guid generation can be achieved through the Tools that come with vs. Click Tools-> Create GUID on the menu bar, and then select the 4th option. Registry Format.
// Click the New GUID button, and then click Copy. in this way, you can copy the generated guid and paste it to this place. of course, the braces must be removed. the guid is also used in the following scenarios.
[ComVisible (true)]
Public interface IComTest // remember to use the modifier public
{
[DispId (1)] // if you want to add other functions, add [DispId (2)] and [DispId (3)] before the function.
Int Plus (int one, int two );
}
}
 
3. Add a class ComTest and inherit the IComTest interface. The specific code
Using System. Runtime. InteropServices;
Namespace ArwenAddCom
{
[Guid ("04F4DC83-8883-4a03-BDBC-92D8630ECC1F")]
[ClassInterface (ClassInterfaceType. None)]
Public class ComTest: IComTest
{
Public int Plus (int a, int B)
{
Return a + B;
}
}
}
 
4. set AssemblyInfo. change [assembly: ComVisible (false)] In cs to [assembly: ComVisible (true)]. right-click the project name to open the Properties window, click Build, and select Register for COMinterop.
 
If you do not want to share the generated dll, put it in the GAC assembly. this completes the work. click build and an ArwenAddCom. dll file. this file is the com component we need. in addition, the ArwenAddCom file is displayed. tlb, which should be normal. if you do not have this tlb file, you can use some commands to call the dll file to generate it. in C ++ calls, The tlb file cannot be small.
If you want to put the dll in GAC, You Have To Do step 5 before building it.
 
5. generate an snk file and add it to the project (optional)
 
Open the VS command window first, which is a bit like the dos window opened with cmd. At the beginning, find vs 2008. Under visual studio tools, There Is A Visual studio commandprompt. Click to open it.
C: \ windows \ system32by default. You can switch to another window first. for example, enter D: Press enter to switch to the directory of drive D, and then enter cd D: \ temp to switch to the temp directory. cd and any path can be switched to any location on the d disk.
Then enter the command sn-k ArwenAddCom. then press enter to generate an snk file in the temp directory. set ArwenAddCom. copy the snk to the root directory of the project. the same directory as the csproj file.
Open the Project Properties window, click the tag Signing, select Sign the assembly, and click browse in the drop-down list below. Then a dialog box is displayed, and select ArwenAddCom. snk.
Click build again to generate the dll file.
 
C ++ calls the C # com Component
 
1. register the com component before calling.
If you just call the dll and build the dll on the same computer, you do not need to Register it. Because Register for COMinterop is selected for the build C # project, it is automatically registered for you.
If you are not on the same computer, you have to register another one. If you want to develop a desktop development program, you must register it again. in this case, you cannot register manually by writing code. for the sake of simplicity, I will only talk about how to manually register
The vs command window is used like generating an snk file. if the snk file dll registration is not used. switch the path to the directory where the generated dll is located in the vs command window. the dll can be stored in the same directory. You only need to find the directory.
If an snk file is used, use gacutil/I ArwenAddCom. dll to put the dll into GAC.
You can use REGASM ArwenAddCom. dll/tlb: ArwenAddCom. tlb to register the dll and generate the tlb file.
Supplement:
If a tlb file is already registered, it is REGASM ArwenAddCom. dll. If you want to generate a tlb file separately, it is tlbexp/out: ArwenAddCom. tlb ArwenAddCom. dll.
You can also use REGASM/regfile: ArwenAddCom. reg ArwenAddCom. dll to generate a reg registration script file. You can double-click this script file to register it.
 
2. After registration, you can use it in the Code. First, create a new C ++ project and select the Win32 Console Application as the type.
Project name: UseCsharpCom. Click finish on the Wizard Page. set ArwenAddCom. copy the tlb file to any directory of the project. I put it in D: \ UseCsharpCom. we will see a UseCsharpCom. double-click the cpp file to delete all the generated code by default. enter the following code:
# Include "stdafx. h"
# Include <iostream>
# Import ".. \ UseCsharpCom \ CsharpComTest. tlb" named_guids raw_interfaces_only
Void _ tmain (int argc, _ TCHAR * argv [])
{
CoInitialize (NULL );
CsharpComTest: IComTestPtr; // a pointer similar to Mingzhi pointing to an interface
Ptr. CreateInstance (CsharpComTest: CLSID_ComTest); // instantiate a class
// This is a bit strange. I haven't understood it yet. the function in C # Is int Plus (int, int ). however, the type is converted to long here. in addition, we cannot directly
// A long a = ptr-> Plus (1, 2); in this case, a = 3 is not obtained, and an error occurs. here, the Plus parameter is changed to three long Plus (long, long, long *). the result of adding two numbers to the last pointer
Long a = 1;
Long * lPtr = &;
Ptr-> Plus (1, 2, lPtr );
Std: cout <;
}
If you put the dll in GAC, you can run it correctly. Expected result 3 is displayed.
If the dllfile is not stored in gac, the dllfile is stored in the directory where usecsharpcom.exe is located.

 


From smart dummies

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.