C ++ project calls C # DLL

Source: Internet
Author: User

My first contact with managed C ++ is my personal feeling. Please point out the incorrect content.

 

When I used C # To write a DLL project, I thought I had to use a dllexport or something like that. Later I realized that I only needed to declare the exported function as public.

However, after the DLL is created, you need to use managed C ++ to call the functions in the DLL, but native C ++ cannot. In addition, when using the DLL of C #, I thought I would add a header file and a library file. Later I only needed # using "... DLL. In this case, the managed C ++ project functions as an adapter. I used the managed C ++ project to wrap the DLL generated by C # And then export it, so that the native C ++ project can call the packaged DLL.

Of course, here I would like to recommend a buddy's blog:

Http://www.cppblog.com/mzty/archive/2007/12/25/39555.html

By the way, if you want to use managed C ++, You need to select the common language runtime support in the project configuration file (C ++.

 

Later, I did an experiment on my own. The DLL was successfully generated, but it was annoying to have a compilation error during the project compilation of managed C ++, later I compared my buddy's code with my program and found that there was no difference. So I began to suspect the project configuration file.

The compilation error is as follows:

>. /Dllmain. CPP (32): Error c3624: 'System: Windows: forms: usercontrol': Use of this type requires a reference to assemb' system. windows. forms'

This error is a little difficult for beginners. I think it is probably because the usercontrol I used does not correspond to the. NET class library. However, adding using system. Windows. forms to the source file does not work at all. Later, the code was compared and there was no error.

In this case, you only need to check the project configuration file.

The buddy project configuration file is as follows:

<? XML version = "1.0" encoding = "gb2312"?>
<Visualstudioproject
Projecttype = "Visual C ++"
Version = "8.00"
Name = "cppcliproxydllforcsharpdll"
Projectguid = "{4f30e449-c625-4a10-8a12-59f09f923f1f }"
Rootnamespace = "cppcliproxydllforcsharpdll"
Keyword = "win32proj"
>
<Platforms>
<Platform
Name = "Win32"
/>
</Platforms>
<Toolfiles>
</Toolfiles>
<Deployments>
<Configuration
Name = "Debug | Win32"
Outputdirectory = "$ (solutiondir) $ (configurationname )"
Intermediatedirectory = "$ (configurationname )"
Configurationtype = "2"
Characterset = "1"
Managedextensions = "1"
>
<Tool
Name = "vcprebuildeventtool"
/>
<Tool
Name = "vccustombuildtool"
/>
<Tool
Name = "vcxmldatageneratortool"
/>
<Tool
Name = "vcwebserviceproxygeneratortool"
/>
<Tool
Name = "vcmidltool"
/>
<Tool
Name = "vcclcompilertool"
Optimization = "0"
Preprocessordefinitions = "Win32; _ debug; _ windows; _ usrdll; cppcliproxydllforcsharpdll_exports"
Minimalrebuild = "false"
Basicruntimechecks = "0"
Runtimelibrary = "3"
Useprecompiledheader = "2"
WarningLevel = "3"
Detect64bitportabilityproblems = "true"
Debuginformationformat = "3"
/>
<Tool
Name = "vcmanagedresourcecompilertool"
/>
<Tool
Name = "vcresourcecompilertool"
/>
<Tool
Name = "vcprelinkeventtool"
/>
<Tool
Name = "vclinkertool"
Linkincremental = "2"
Generatedebuginformation = "true"
Assemblydebug = "1"
Subsystem = "2"
Targetmachine = "1"
/>
<Tool
Name = "vcalinktool"
/>
<Tool
Name = "vcmanifesttool"
/>
<Tool
Name = "vcxdcmaketool"
/>
<Tool
Name = "vcbscmaketool"
/>
<Tool
Name = "vcfxcoptool"
/>
<Tool
Name = "vcappverifiertool"
/>
<Tool
Name = "vcwebdeploymenttool"
/>
<Tool
Name = "vcpostbuildeventtool"
/>
</Configuration>
<Configuration
Name = "Release | Win32"
Outputdirectory = "$ (solutiondir) $ (configurationname )"
Intermediatedirectory = "$ (configurationname )"
Configurationtype = "2"
Characterset = "1"
Wholeprogramoptimization = "1"
>
<Tool
Name = "vcprebuildeventtool"
/>
<Tool
Name = "vccustombuildtool"
/>
<Tool
Name = "vcxmldatageneratortool"
/>
<Tool
Name = "vcwebserviceproxygeneratortool"
/>
<Tool
Name = "vcmidltool"
/>
<Tool
Name = "vcclcompilertool"
Preprocessordefinitions = "Win32; ndebug; _ windows; _ usrdll; cppcliproxydllforcsharpdll_exports"
Runtimelibrary = "2"
Useprecompiledheader = "2"
WarningLevel = "3"
Detect64bitportabilityproblems = "true"
Debuginformationformat = "3"
/>
<Tool
Name = "vcmanagedresourcecompilertool"
/>
<Tool
Name = "vcresourcecompilertool"
/>
<Tool
Name = "vcprelinkeventtool"
/>
<Tool
Name = "vclinkertool"
Linkincremental = "1"
Generatedebuginformation = "true"
Subsystem = "2"
Optimizereferences = "2"
Enablecomdatfolding = "2"
Targetmachine = "1"
/>
<Tool
Name = "vcalinktool"
/>
<Tool
Name = "vcmanifesttool"
/>
<Tool
Name = "vcxdcmaketool"
/>
<Tool
Name = "vcbscmaketool"
/>
<Tool
Name = "vcfxcoptool"
/>
<Tool
Name = "vcappverifiertool"
/>
<Tool
Name = "vcwebdeploymenttool"
/>
<Tool
Name = "vcpostbuildeventtool"
/>
</Configuration>
</Deployments>
<References>
<Assemblyreference
Relativepath = "system. Windows. Forms. dll"
Assemblyname = "system. Windows. Forms, version = 2.0.0.0, publickeytoken = b77a5c561934e089, processorarchitecture = msil"
/>
<Assemblyreference
Relativepath = "system. dll"
Assemblyname = "system, version = 2.0.0.0, publickeytoken = b77a5c561934e089, processorarchitecture = msil"
/>
</References>
<Files>
<Filter
Name = "source files"
Filter = "CPP; C; CC; cxx; def; odl; IDL; HPJ; BAT; ASM; asmx"
Uniqueidentifier = "{4fc737f1-c7a5-4376-a066-2a32d752a2ff }"
>
<File
Relativepath = "./cppcliproxydllforcsharpdll. cpp"
>
</File>
<File
Relativepath = "./stdafx. cpp"
>
<Fileconfiguration
Name = "Debug | Win32"
>
<Tool
Name = "vcclcompilertool"
Useprecompiledheader = "1"
/>
</Fileconfiguration>
<Fileconfiguration
Name = "Release | Win32"
>
<Tool
Name = "vcclcompilertool"
Useprecompiledheader = "1"
/>
</Fileconfiguration>
</File>
</Filter>
<Filter
Name = "header files"
Filter = "h; HPP; hxx; HM; INL; Inc; XSD"
Uniqueidentifier = "{93995316-89bd-4b04-88eb-625fbe52ebfb }"
>
<File
Relativepath = "./cppcliproxydllforcsharpdll. H"
>
</File>
<File
Relativepath = "./stdafx. H"
>
</File>
</Filter>
<Filter
Name = "resource files"
Filter = "RC; ICO; cur; BMP; DLG; RC2; RDBMS; bin; RGS; GIF; JPG; JPEG; jpe; resx; tiff; TIF; PNG; WAV"
Uniqueidentifier = "{67da6ab6-f800-4c08-8b7a-83bb121aad01 }"
>
</Filter>
<File
Relativepath = "./readme.txt"
>
</File>
</Files>
<Globals>
</Globals>
</Visualstudioproject>

Based on the Compilation error information, combined with the configuration file, you can lock yourself and do not properly configure the configuration file.

In the common properties of the configuration file, click Add new references and add system. Windows. forms. Of course, another simple method is to write a sentence in the. h file: # using <system. Windows. Forms. dll>

 

When I went home at night, I occasionally searched the internet. Books on C ++/CLI (good ):

Http://www.visionopen.com/smf/index.php? PHPSESSID = pmbdio79cuko8hl7nublknjbg3 & topic = 317.0 this website introduces the following urls:

Http://msdn.microsoft.com/en-us/library/ms379617.aspx

Standard ECMA-372 C ++/CLI language specification!
Http://www.ecma-international.org/publications/standards/Ecma-372.htm
Http://www.gotw.ca/publications/C++CLIRationale.pdf

 

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.