Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Chapter01/Chapter01.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Import Project="..\Chapter.props" />
<ItemGroup>
<Compile Remove="Listing01.02.SampleNETCoreConsoleProjectFile.cs" />
<Compile Remove="Listing01.20.SampleCILOutput.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
Expand All @@ -21,5 +22,6 @@
</ItemGroup>
<ItemGroup>
<None Include="Listing01.02.SampleNETCoreConsoleProjectFile.cs" />
<None Include="Listing01.20.SampleCILOutput.cs" />
</ItemGroup>
</Project>
75 changes: 75 additions & 0 deletions src/Chapter01/Listing01.20.SampleCILOutput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter01.Listing01_20
{
/*
#region INCLUDE
.assembly extern System.Runtime
{
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )
.ver 4:2:0:1
}


.assembly extern System.Console
{
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )
.ver 4:1:0:1
}


.assembly 'HelloWorld'
{
.custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = (01 00 08 00 00 00 00 00 )
.custom instance void [System.Runtime]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = (01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )
.custom instance void [System.Runtime]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = (01 00 18 2E 4E 45 54 43 6F 72 65 41 70 70 2C 56 65 72 73 69 6F 6E 3D 76 33 2E 30 01 00 54 0E 14 46 72 61 6D 65 77 6F 72 6B 44 69 73 70 6C 61 79 4E 61 6D 65 00 )
.custom instance void [System.Runtime]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = (01 00 0A 48 65 6C 6C 6F 57 6F 72 6C 64 00 00 )
.custom instance void [System.Runtime]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = (01 00 05 44 65 62 75 67 00 00 )
.custom instance void [System.Runtime]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = (01 00 07 31 2E 30 2E 30 2E 30 00 00 )
.custom instance void [System.Runtime]System.Reflection.AssemblyInformationalVersionAttribute::.ctor(string) = (01 00 05 31 2E 30 2E 30 00 00 )
.custom instance void [System.Runtime]System.Reflection.AssemblyProductAttribute::.ctor(string) = (01 00 0A 48 65 6C 6C 6F 57 6F 72 6C 64 00 00 )
.custom instance void [System.Runtime]System.Reflection.AssemblyTitleAttribute::.ctor(string) = (01 00 0A 48 65 6C 6C 6F 57 6F 72 6C 64 00 00 )
.hash algorithm 0x00008004
.ver 1:0:0:0
}


.module 'HelloWorld.dll'
// MVID: {05b2d1a7-c150-4f20-bd96-c065e4f97a31}
.imagebase 0x00400000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003 // WindowsCui
.corflags 0x00000001 // ILOnly


.class private auto ansi beforefieldinit HelloWorld.Program extends[System.Runtime] System.Object
{


.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 13
.maxstack 8
IL_0000: nop
IL_0001: ldstr "Hello. My name is Inigo Montoya."
IL_0006: call void
↪ System.Console]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: ret
} // End of method System.Void HelloWorld.Program::Main(System.String[])


.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
// Code size 8
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [System.Runtime]
System.Object::.ctor()
IL_0006: nop
IL_0007: ret
} // End of method System.Void HelloWorld.Program::.ctor()
} // End of class HelloWorld.Program
#endregion INCLUDE
*/
}

This file was deleted.

16 changes: 15 additions & 1 deletion src/Chapter18/Listing18.11.SpecifyingAReturnAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
using System;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_11
{
public class Program
{
[return: Description(
"Returns true if the object is in a valid state.")]
public bool IsValid()
public static bool IsValid()
{
// ...
return true;
}
}

public class DescriptionAttribute : Attribute
{
// TODO: Update listing in Manuscript
private readonly string _Description;
public string Description { get { return _Description; } }

public DescriptionAttribute(string description)
{
this._Description = description;
}
}
}
22 changes: 22 additions & 0 deletions src/Chapter23/Chapter23.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,31 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<Import Project="..\Chapter.props" />
<ItemGroup>
<Compile Remove="Listing23.02.TheVirtualAllocExAPI.cs" />
<Compile Remove="Listing23.03.DeclaringTheVirtualAllocExAPI.cs" />
<Compile Remove="Listing23.05.DeclaringTypesFromUnmanagedStructs.cs" />
<Compile Remove="Listing23.07.ManagedResourcesUsingSafeHandle.cs" />
<Compile Remove="Listing23.08.EncapsulatingTheAPIsTogether.cs" />
<Compile Remove="Listing23.09.WrappingTheUnderlyingAPI.cs" />
<Compile Remove="Listing23.10.DesignatingAMethodForUnsafeCode.cs" />
<Compile Remove="Listing23.14.FixedStatement.cs" />
<Compile Remove="Listing23.16.AllocatingDataOnTheCallStack.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Shared\Program.cs">
<Link>Program.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="Listing23.02.TheVirtualAllocExAPI.cs" />
<None Include="Listing23.03.DeclaringTheVirtualAllocExAPI.cs" />
<None Include="Listing23.05.DeclaringTypesFromUnmanagedStructs.cs" />
<None Include="Listing23.07.ManagedResourcesUsingSafeHandle.cs" />
<None Include="Listing23.08.EncapsulatingTheAPIsTogether.cs" />
<None Include="Listing23.09.WrappingTheUnderlyingAPI.cs" />
<None Include="Listing23.10.DesignatingAMethodForUnsafeCode.cs" />
<None Include="Listing23.14.FixedStatement.cs" />
<None Include="Listing23.16.AllocatingDataOnTheCallStack.cs" />
</ItemGroup>
</Project>
12 changes: 12 additions & 0 deletions src/Chapter23/Listing23.01.DeclaringAnExternalMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter23.Listing23_01
{
#region INCLUDE
using System;
using System.Runtime.InteropServices;
public class VirtualMemoryManager
{
[DllImport("kernel32.dll", EntryPoint = "GetCurrentProcess")]
internal static extern IntPtr GetCurrentProcessHandle();
}
#endregion INCLUDE
}
20 changes: 20 additions & 0 deletions src/Chapter23/Listing23.02.TheVirtualAllocExAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter23.Listing23_02
{
#region INCLUDE
using System;
using System.Runtime.InteropServices;
public class VirtualMemoryManager
{
[DllImport("kernel32.dll")]
internal static extern IntPtr GetCurrentProcess();

[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr VirtualAllocEx(
IntPtr hProcess,
IntPtr lpAddress,
IntPtr dwSize,
AllocationType flAllocationType,
uint flProtect);
}
#endregion INCLUDE
}
14 changes: 14 additions & 0 deletions src/Chapter23/Listing23.03.DeclaringTheVirtualAllocExAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter23.Listing23_03
{
#region INCLUDE
public class VirtualMemoryManager
{
// ...
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool VirtualProtectEx(
IntPtr hProcess, IntPtr lpAddress,
IntPtr dwSize, uint flNewProtect,
ref uint lpflOldProtect);
}
#endregion INCLUDE
}
12 changes: 12 additions & 0 deletions src/Chapter23/Listing23.04.UsingrefAndoutRatherThanPointers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter23.Listing23_04
{
#region INCLUDE
using System;
using System.Runtime.InteropServices;
public class VirtualMemoryManager
{
[DllImport("kernel32.dll", EntryPoint = "GetCurrentProcess")]
internal static extern IntPtr GetCurrentProcessHandle();
}
#endregion INCLUDE
}
24 changes: 24 additions & 0 deletions src/Chapter23/Listing23.05.DeclaringTypesFromUnmanagedStructs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter23.Listing23_05
{
#region INCLUDE
[StructLayout(LayoutKind.Sequential)]
struct ColorRef
{
public byte Red;
public byte Green;
public byte Blue;
// Turn off the warning about not accessing Unused
#pragma warning disable 414
private byte Unused;
#pragma warning restore 414

public ColorRef(byte red, byte green, byte blue)
{
Blue = blue;
Green = green;
Red = red;
Unused = 0;
}
}
#endregion INCLUDE
}
88 changes: 88 additions & 0 deletions src/Chapter23/Listing23.06.Win32ErrorHandling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Runtime.InteropServices;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter23.Listing23_06
{
#region INCLUDE
public class VirtualMemoryManager
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr VirtualAllocEx(
IntPtr hProcess,
IntPtr lpAddress,
IntPtr dwSize,
AllocationType flAllocationType,
uint flProtect);

// ...
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool VirtualProtectEx(
IntPtr hProcess, IntPtr lpAddress,
IntPtr dwSize, uint flNewProtect,
ref uint lpflOldProtect);

[Flags]
private enum AllocationType : uint
{
#region EXCLUDE
Reserve,
Commit
#endregion EXCLUDE
}

[Flags]
private enum ProtectionOptions
{
#region EXCLUDE
PageExecuteReadWrite
#endregion EXCLUDE
}

[Flags]
private enum MemoryFreeType
{
// ...
}

public static IntPtr AllocExecutionBlock(
int size, IntPtr hProcess)
{
IntPtr codeBytesPtr;
codeBytesPtr = VirtualAllocEx(
hProcess, IntPtr.Zero,
(IntPtr)size,
AllocationType.Reserve | AllocationType.Commit,
(uint)ProtectionOptions.PageExecuteReadWrite);

if (codeBytesPtr == IntPtr.Zero)
{
throw new System.ComponentModel.Win32Exception();
}

uint lpflOldProtect = 0;
if (!VirtualProtectEx(
hProcess, codeBytesPtr,
(IntPtr)size,
(uint)ProtectionOptions.PageExecuteReadWrite,
ref lpflOldProtect))
{
throw new System.ComponentModel.Win32Exception();
}
return codeBytesPtr;
}

public static IntPtr AllocExecutionBlock(int size)
{
return AllocExecutionBlock(
size, GetCurrentProcessHandle());
}

#region EXCLUDE
public static IntPtr GetCurrentProcessHandle()
{
throw new NotImplementedException();
}
#endregion EXCLUDE
}
#endregion INCLUDE
}
Loading