C # managed code and C + + unmanaged code call each other __c++

Source: Internet
Author: User
C # managed code and C + + unmanaged code call each otherhttp://www.cnblogs.com/Jianchidaodi/archive/2009/03/11/1407270.html#1473515
Http://www.cnblogs.com/Jianchidaodi/archive/2009/03/11/1408661.html
C # managed code and C + + unmanaged code are called one another (C # calls C + + code &.net code security)
In recent projects, project source code confidentiality issues are involved, because the code is written in C #, easy to decompile, so the decision to extract the core algorithm part of the use of C + + writing, C + + so far seems to have not been very good decompile, of course, if you are a disassembly master, perhaps it is possible to decompile. This means that C # managed code and C + + unmanaged code call each other, and then we investigate some of the information and share it with you: source code download

A. Static call C + + dynamic link in C #
1. VC Project Cppdemo, the establishment of the time to select Win32 Console (DLL), select the DLL.
2. Add the code to the DllDemo.cpp file.
[CPP] View plain copy extern "C" __declspec (dllexport) int Add (int a,int b) {return a+b; } 3. Compile the project.
4. Create a new C # project, select Console application, set up test program Interopdemo
5. Add references in Program.cs: using System.Runtime.InteropServices;
6. Add the following code to the Pulic class program:

[cpp]  View Plain  copy using system;      using  system.collections.generic;      using system.text;      using system.runtime.interopservices;           Namespace  InteropDemo      {          class  Program          {               [dllimport ("CppDemo.dll", entrypoint =  "Add",  EXACTSPELLING = FALSE, CALLINGCONVENTION = CALLINGCONVENTION.CDECL)]               public static extern int  add (int a, int b)  //dllimport please refer to msdn                 &NBSp;  static void main (String[] args)                {                   console.writeline (ADD (1, 2));                   console.read ();               }          }      }         OK, now you can test the add program, it is not possible to call the C + + dynamic link in C #, of course this is static call, You need to place the Cppdemo compiled DLL in the Dlldemo program's Bin directory  

Two. Dynamic call C + + dynamic link in C #


In the first section, we talk about static invocation of C + + dynamic link, because the DLL path is not convenient to use, in C # We often configure dynamic invocation of managed DLLs, such as some commonly used design patterns: Abstract Factory, Provider, Strategy mode and so on, then it is also possible to dynamically invoke C + + dynamic links. As long as you remember in C + +, through the LoadLibrary, getprocess, FreeLibrary These functions can dynamically invoke dynamic link (they are included in the Kernel32.dll), then the problem is solved, the next step we experiment

1. The kernel32 of several methods in the package cost to call the class Nativemethod

[CPP] View plain copy using System;      Using System.Collections.Generic;      Using System.Text;           Using System.Runtime.InteropServices; Namespace Interopdemo {

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.