The process of managed code injects the &CLR host

Source: Internet
Author: User
Tags garbage collection

In the previous several blogs about CLR boarding (CLR boarding (upper) Mscoree. DLL,CLR hosted (medium) hosted EXE files are loaded and executed, and hosted by the CLR hosted (under) hosting, the common host interfaces are described. Host interface, which allows us to create a CLR host using unmanaged code to start the CLR, run managed code, and control garbage collection ... and a series of functions. This blog post is about a scenario that uses a CLR host--process injection.

Process injection is a technique that injects code into an existing process address space and executes it. There are many techniques for process injection, which is based on loaddll&createremotethread technology.

Generally, we compile the code to be executed into a DLL file and then load it into the target process. It is OK to load and execute an unmanaged DLL directly, but it would be a struggle to load a managed DLL into a process, because managed code cannot be executed directly, passing through the CLR two of times. How to solve this problem?

Because of the impact of the environment on the process injection, I will first list my experimental environment, and then specific explanation.

System: Windows 7, 64-bit

. net:4.0

Development tools: VS2010 SP1

Test program: All 32-bit programs

1.1 Implementing unmanaged code calls managed code

Here, a picture of a foreigner describes how our managed code is executed within the target process.

First, an unmanaged c++dll is injected into the target process using a program with an injection function, and the unmanaged DLL starts the CLR, loads the managed DLL to execute, and finally invokes the CLR to execute managed code.

The process looks simple, and the first problem to solve here is to create a c++dll as a CLR host.

Open VS2010 and select C + + Win32project project.

After you determine the next step, apply the type selection DLL and select the empty item in the additional options.

The project name I created is: Managecodeinvoker, as shown below:

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.