C + + calls methods in the C#dll class library (non-explicit COM)

Source: Internet
Author: User

Generally on-line search C + + How to call C # functions, the results are made of COM components, but this method DLL installation trouble, need to register COM components, need administrator rights, debugging trouble, often need to restart the machine, anyway, there are many inconvenience.

Then look at the CLR via C # and see a way to eliminate this distress. Less nonsense, first on the code.

C # class Library:

namespace classlibrary1{    publicclass  Class1    {        publicstatic  int  pwzmethodname (String pwzargument)        {            //Thread.Sleep ( 10000);             Console.WriteLine (pwzargument);            Console.WriteLine (Thread.getdomain (). FriendlyName);     }}}

C + + calling code:

#include <metahost.h>#include<fstream>using namespacestd;#pragmaComment (lib, "Mscoree.lib")int_tmain (intARGC, _tchar*argv[]) {ICLRMetaHost*pmetahost =nullptr; ICLRMetaHostPolicy*pmetahostpolicy =nullptr; ICLRRuntimeHost*pruntimehost =nullptr; ICLRRuntimeInfo*pruntimeinfo =nullptr; HRESULT HR= CLRCreateInstance (Clsid_clrmetahost, Iid_iclrmetahost, (lpvoid*) &pmetahost); HR= Pmetahost->getruntime (L"v4.0.30319", Iid_ppv_args (&pruntimeinfo)); if(FAILED (HR)) {MessageBox (0, L"Startup Error", L"Error", mb_ok|mb_iconerror); Gotocleanup; } HR= Pruntimeinfo->getinterface (CLSID_CLRRuntimeHost, Iid_ppv_args (&pruntimehost)); HR= pruntimehost->Start (); DWORD Dwret=0; HR= Pruntimehost->executeindefaultappdomain (L"ClassLibrary1.dll",//no new processes are generatedL"Classlibrary1.class1", L"Pwzmethodname", L"Nimei",         &Dwret); HR= pruntimehost->Stop (); Cleanup:if(Pruntimeinfo! =nullptr) {Pruntimeinfo-Release (); Pruntimeinfo=nullptr; }     if(Pruntimehost! =nullptr) {Pruntimehost-Release (); Pruntimehost=nullptr; }     if(Pmetahost! =nullptr) {Pmetahost-Release (); Pmetahost=nullptr; } }

The code for C # does not explain that it is a way to provide an int ClassLibrary1.Class1.pwzMethodName (String).

Focusing on C + +, the idea is to manipulate the CLR environment through functions in metahost.h.

The first sentence, CLRCreateInstance (Clsid_clrmetahost, Iid_iclrmetahost, (lpvoid*) &pmetahost), initializes the CLR environment and gets the return value pmetahost.

In the second sentence, Pmetahost->getruntime (L "v4.0.30319", Iid_ppv_args (&pruntimeinfo)) to specify the runtime, the first parameter. NET version number must be compatible with the version number of your C#dll, and This parameter must be displayed in C:\Windows\Microsoft.NET\Framework or C:\Windows\Microsoft.NET\Framework64 Matches the. NET Framework version directory name under the. For example, your DLL is compiled with 4.0, and your framework directory has folders:v2.0.50727 v3.0 v3.5 v4.0.30319, so the parameter here is "v4.0.30319", and V is a must.

In the third sentence, Pruntimeinfo->getinterface (CLSID_CLRRuntimeHost, Iid_ppv_args (&pruntimehost)) loads the CLR into the current process, and returns the run-time interface pointer pruntimehost.

In the Forth sentence, Pruntimehost->start () initializes the common language runtime (CLR) into the process.

Sentence five,pruntimehost->executeindefaultappdomain

HRESULT executeindefaultappdomain (    [in] lpcwstr Pwzassemblypath,    [in ] LPCWSTR Pwztypename,     [in] lpcwstr pwzmethodname,    [in] LPCWSTR pwzargument,    [out] DWORD *preturnvalue); 

Pwzassemblypath is the path and file name of the C # class library, Pwztypename is the namespace and class name, Pwzmethodname is the name of the method to invoke, Pwzargument is the parameter name to pass in, and pReturnValue is the function return value.

All that follows is the release statement.

In general, this method can only invoke functions such as int function (String para), but in fact LPCWSTR is WCHAR *, since it is a pointer that can pass an object, the same return value can be a struct or an array.

This example address: Http://files.cnblogs.com/files/chihirosan/CPPCS%E4%BA%A4%E4%BA%92.rar

C + + calls methods in the C#dll class library (non-explicit COM)

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.