Use managed C ++ to call the C # exe program

Source: Internet
Author: User

Use the hosted C ++ to call the C # exe program.

(It turns out that reflector is invalid .) Ah.

Add a CLR console application. Place the C # exe program in the "resource file" folder, and set the item type of the right-click attribute to the compiled managed resource.

The main function of the main program code is rewritten as follows:

1 // X. cpp: Main project file. 2 3 # include "stdafx. H "4 5 using namespace system; 6 7 [stathreadattriction] 8 int main (array <system: String ^> ^ ARGs) 9 {10 system: Reflection :: assembly ^ A; 11 A = system: Reflection: Assembly: getexecutingassembly (); 12 system: IO: stream ^ stream; 13 stream = A-> getmanifestresourcestream ("csharp.exe"); 14 array <system: byte> ^ BS = gcnew array <system: byte> (Stream-> length ); 15 stream-> Read (BS, 0, (I NT) (Stream-> length); 16 system: Reflection: Assembly ^ EXE; 17 EXE = system: Reflection: Assembly: load (BS ); 18 system: Reflection: methodinfo ^ Info; 19 info = Exe-> entrypoint; 20 array <system: Reflection :: parameterinfo ^> ^ parameters = Info-> getparameters (); 21 if (parameters! = Nullptr) & (parameters-> length> 0) 22 {23 Info-> invoke (nullptr, (array <system: Object ^> ^) ARGs ); 24} 25 else26 {27 Info-> invoke (nullptr, nullptr); 28} 29 return 0; 30}

The corresponding C # code is as follows:

1 static class Program 2 {3 /// <summary> 4 // main entry point of the application. 5 /// </Summary> 6 [stathread] 7 static void main (string [] ARGs) 8 {9 var ASM = assembly. getexecutingassembly (); 10 stream = ASM. getmanifestresourcestream ("csharp.exe"); 11 byte [] BS = new byte [stream. length]; 12 stream. read (BS, 0, (INT) stream. length); 13 var EXE = assembly. load (BS); 14 methodinfo info = exe. entrypoint; 15 parameterinfo [] parameters = info. getparameters (); 16 if (parameters! = NULL) & (parameters. length> 0) 17 info. invoke (null, (object []) ARGs); 18 else19 info. invoke (null, null); 20 // application. enablevisualstyles (); 21 // application. setcompatibletextrenderingdefault (false); 22 // application. run (New form1 (); 23} 24}

 

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.