C # can embed assembler in my eyes C # as a middle-upper language is impossible not to
Why is it that I think of the upper-middle language when I place the assembly code? Keeping pointers from C # can see that I know
There are a lot of people who won't believe C # can use assembler code but C # will be troublesome C # can not directly
Inline assembly (INLINE-ASM) is accurate that C # can only be used (AUTO-ASM) to dynamically assemble this technology is not
C # unique language, VB, C + + three languages can be but dynamic assembly I have seen the most is to be
The use of plug-in and remote assembly injection is actually a kind of extension of dynamic assembly technology but it's hard to
Said JIT after compiling the code is through the remote assembly code written to the managed process execution or a
Parasitic in the shell running technology and "memory run" too lazy to discuss these thoughts on the head big.
From there you can see a simple x86/call assembly embedded in C # and called
Perform a look you'll find it's not too hard I wrote a lot of crap on one of my blog posts, which means
This thing is just a language http://blog.csdn.net/u012395622/article/details/46400569.
We know that when the software runs, all the code is put in virtual memory and the executable code is in memory
Memory protection is generally page_execute_read and 32 but after I studied. Net
The executable code should be page_execute_readwirte and 64 if it's p/invoke.
The protection in the Execute DLL is 32 we can not use read-only protection in inline assembly
If we need to use by. NET to delegate to call so must be readable writable if passed Win32
API to call so use 32 can be a little different, I have studied the easy language on the byte set in memory
How much of the memory protection in the end is consistent with C # 4/page_readwrite but why
Easy language can call and C # can not call has always been a confusing thing to me may be
The difference between the managed heap and the unmanaged heap but I'd rather have a great God come out and help me out.
Because the X86 assembly first needs to switch the target platform to x86 so that it does not cause the C # call Assembly
Code error must not omit this step
First you need to define a parameterized delegate with the emphasis on having such a sentence in the Assembly
Call DWORD Ptr[ebp+8]//Call parameter One
[CSharp]View Plaincopy
- [Unmanagedfunctionpointer (CALLINGCONVENTION.CDECL)]
- Public Delegate IntPtr Callmethod (IntPtr ptr);
Because it is in VC under the inline assembly finally ported to C # General in the VC under the function of the call mode is Cdcel
Moreover, the following is done according to the Cdcel export function format, so you can not use the __stdcall way
[CSharp]View Plaincopy
- [STAThread]
- static void Main (string[] args)
- {
- byte[] Buf_asm = {
- //Push EBP
- //MOV ebp,esp
- //Sub esp,0c0h
- //Push EBX
- //push ESI
- //Push EDI
- //Lea edi,[ebp-0c0h]
- //MOV ecx,30h
- //MOV eax,0cccccccch
- //Rep stos dword ptr Es:[edi]
- 85, 139, 236, 129, 236, 192, 0, 0, 0, 83, 86, 87, 141, 189, 64,
- 255, 255, 255, 185, 48, 0, 0, 0, 184, 204, 204, 204, 204, 243, 171,
- //Call DWORD Ptr[ebp+8]
- 255, 85, 8,
- //Pop EDI
- //Pop esi
- //Pop ebx
- //MOV esp,ebp
- //Pop EBP
- //RET
- 95, 94, 91, 139, 229, 93, 195
- };
- IntPtr ptr_asm = Sethandlecount (buf_asm);
- VirtualProtect (Ptr_asm, Buf_asm. Length);
- Callmethod Call_method = Marshal.getdelegateforfunctionpointer (Ptr_asm, typeof (Callmethod)) as CallMethod;
- Call_method (Marshal.getfunctionpointerfordelegate (new Action (hello_x86)));
- }
First write the assembly you need to embed in a byte array format and pass
The Sethandlecount function is used to fetch the address pointer
[CSharp]View Plaincopy < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
- static void VirtualProtect (IntPtr ptr, int size)
- {
- int outmemprotect;
- if (! VirtualProtect (PTR, size, outmemprotect, out ))
- throw New Exception ("Unable to modify memory protection.");
- }
The above function is used to modify memory protection simply to allow delegates to interact, including assembly code that can be called inter-
[CSharp]View Plaincopy
- static void Hello_x86 ()
- {
- Console.title = ((new StackFrame ()). GetMethod ()). Name;
- Console.WriteLine ("I was x86 assembly call a test function.");
- Console.readkey (false);
- }
The function above is a test function that does not make much sense just to show the use of assembly calls
This function then outputs a response to the information used to prompt the function to be written to the memory Assembly called
Dependent external functions
[CSharp]View Plaincopy
- [DllImport ("kernel32.dll", CharSet = CharSet.Auto)]
- Public static extern IntPtr Sethandlecount (byte[] value);
- [DllImport ("kernel32.dll", SetLastError = true)]
- Public static extern bool VirtualProtect (IntPtr lpaddress, int. dwsize, int flnewprotect, out C13>int lpfloldprotect);
namespaces that depend on
[CSharp]View Plaincopy
- Using System;
- Using System.Runtime.InteropServices;
- Using System.Diagnostics;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C # INLINE-ASM/EMBED x86 assembly