Implementing C + + calling C # code interaction through the CLR API

Source: Internet
Author: User

For some cross-platform programs, this means that you can only use Windows under the

But lately. NET open source may later cross-platform

Before it took some time to study COM mode calls, too cumbersome not recommended. Com Method Invocation Summary

Later try to use CLR C + +, although it can be used. NET interface directly, but he is only a layer of C + + skin, and finally failed to apply in the actual project.

Finally, using the MS provided CLR API interface, complete the call to MSDN CLR interface introduction

=======================================================

There are a few points to note:

1. The signature format that the official has been emphasizing is static int pwzmethodname (String pwzargument), not a C # attribute, but the function format must be a String parameter that returns an int type. Otherwise, the method is not found at the time of the call.

In 2.ExecuteInDefaultAppDomain, DLL paths support relative path formats, and "Xx.dll" can be identified.

3.GetRuntime method to obtain the. NET version number, need to go to c:/windows/microsoft.net, 64-bit corresponding to the Framework64, the normal version of the corresponding framework. If the target machine does not have this folder, the corresponding version of. NET is not installed. Then the program will get an error. The third digit of the version number appears to be dead, and should be a unified distribution.

4. When creating a test project, you need to point out the precompiled header option, or there will be many inexplicable errors.

5. Need to use under Administrator privileges

6. A class can be static or non-static, but the function must be a static function

Reference code excerpt from GitHub:

Https://github.com/entice/client/blob/42f388d68b1954ef244c379560eeb4867ae80b7e/Loader/Loader/DllMain.cpp

Specific on the code

C#

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Text;namespaceclasslibrary1{ Public classClass1 { Public Static intEntryPoint (stringArg) {File.appendalltext ("C:/xx.txt","qweqwe"); return 0; }    }}
View Code

C++

//Reference from GitHub entice:Https://github.com/entice/client/blob/42f388d68b1954ef244c379560eeb4867ae80b7e/Loader/Loader/DllMain.cpp #include<Windows.h>#include<stdio.h>#include<iostream>#include<mscoree.h>#include<metahost.h>#include<assert.h>#pragmaComment (lib, "Mscoree.lib")#defineBuffer_size 500#include"stdafx.h"voidStartthedotnetruntime (lpcwstr runtimeversion, Lpcwstr DllPath, Lpcwstr Startclass, Lpcwstr StartMethod, LPCWSTR startargument) {ICLRMetaHost*pmetahost =NULL; ICLRMetaHostPolicy*pmetahostpolicy =NULL; ICLRDebugging*pclrdebugging =NULL; CLRCreateInstance (Clsid_clrmetahost, Iid_iclrmetahost, LPVOID*) &pmetahost); CLRCreateInstance (Clsid_clrmetahostpolicy, Iid_iclrmetahostpolicy, LPVOID*) &pmetahostpolicy); CLRCreateInstance (clsid_clrdebugging, iid_iclrdebugging, LPVOID*) &pclrdebugging); DWORD dwversion=0; DWORD dwimageversion=0; ICLRRuntimeInfo*Pruntimeinfo;    HRESULT result; Result= Pmetahost->getruntime (RuntimeVersion, Iid_iclrruntimeinfo, (lpvoid*) &pruntimeinfo);    ASSERT (SUCCEEDED (result)); ICLRRuntimeHost* Pruntimehost =NULL; Result= Pruntimeinfo->getinterface (CLSID_CLRRuntimeHost, Iid_iclrruntimehost, (lpvoid*) &pruntimehost);    ASSERT (SUCCEEDED (result)); Result= pruntimehost->Start ();    ASSERT (SUCCEEDED (result)); DWORD Dwretcode=0; Result= Pruntimehost->executeindefaultappdomain (DllPath, Startclass, Startmethod, Startargument, &Dwretcode);    ASSERT (SUCCEEDED (result)); Pruntimehost-Stop (); Pruntimehost-Release (); Pruntimeinfo-Release (); Pclrdebugging-Release (); Pmetahostpolicy-Release (); Pmetahost-Release ();}voidLoader () {wchar_t* Runtimeversionspace =NewWchar_t[buffer_size]; wchar_t* Dllpathspace =NewWchar_t[buffer_size]; wchar_t* Currentdir =NewWchar_t[buffer_size];    Getcurrentdirectoryw (Buffer_size, Currentdir); Startthedotnetruntime (L"v4.0.30319", L"ClassLibrary1.dll", L"Classlibrary1.class1", L"entrypoint", L"");    Free (runtimeversionspace);    Free (dllpathspace); Free (currentdir);}int_tmain (intARGC, _tchar*argv[]) {HANDLE HANDLE= CreateThread (NULL,0, (Lpthread_start_routine) Loader, NULL,0, NULL);  for(size_t i =0; I <Ten; i++) {Sleep ( -); } terminatethread (Handle,0); return 0;}
View Code

The DLL generated by C # is then placed in the C + + build directory and then executed with administrator privileges.

Finally, a xx.txt file is created under the C packing directory. Description Call C # succeeded

Implementing C + + calling C # code interaction through the CLR API

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.