Platform Interoperability and Unsafe Code






Summary

This chapter's introduction outlined the low-level access to the underlying operating system that C# exposes. To summarize this, consider the Main() function listing for determining whether execution is with a virtual computer (see Listing 17.20).

Designating a Block for Unsafe Code

        using System.Runtime.InteropServices;

        class Program
        {
           unsafe static int Main(string[] args)
           {
                // Assign redpill
                byte[] redpill = {
                    0x0f, 0x01, 0x0d,       // asm SIDT instruction
                    0x00, 0x00, 0x00, 0x00, // placeholder for an address
                    0xc3};                  // asm return instruction
                unsafe                         
                {                              
                    fixed (byte* matrix = new byte[6],
                        redpillPtr = redpill)
                    {
                        // Move the address of matrix immediately
                        // following the SIDT instruction of memory.
                        *(uint*)&redpillPtr[3] = (uint)&matrix[0];

                        using (VirtualMemoryPtr codeBytesPtr =
                            new VirtualMemoryPtr(redpill.Length))
                        {
                            Marshal.Copy(
                                redpill, 0,
                                codeBytesPtr, redpill.Length);

                               MethodInvoker method =

             (MethodInvoker)Marshal.GetDelegateForFunctionPointer(
                                    codeBytesPtr, typeof(MethodInvoker));
                                 method();
                             }
                             if (matrix[5] > 0xd0)
                             {
                                Console.WriteLine("Inside Matrix!\n");
                                return 1;
                             }
                             else
                             {
                                Console.WriteLine("Not in Matrix.\n");
                                return 0;
                             }
                      } // fixed
               } // unsafe                                        
           }
      }

The results of Listing 17.20 appear in Output 17.5.

Output 17.5.

Inside Matrix!                            

In this case, you use a delegate to trigger execution of the assembler code. The delegate is declared as follows:

 delegate void MethodInvoker();

This book has demonstrated the power, flexibility, consistency, and fantastic structure of C#. This chapter demonstrated the ability, in spite of such high-level programming capabilities, to perform very low-level operations as well.

Before ending the book, the next chapter briefly describes the underlying execution platform and shifts the focus from the C# language to the broader platform in which C# programs execute.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows