15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Transactions with Windows 7 <strong>and</strong> Windows server 2008 ❘ 633<br />

}<br />

SetH<strong>and</strong>le(preexistingH<strong>and</strong>le);<br />

}<br />

}<br />

[ResourceExposure(ResourceScope.Machine)]<br />

[ResourceConsumption(ResourceScope.Machine)]<br />

protected override bool ReleaseH<strong>and</strong>le()<br />

{<br />

return NativeMethods.CloseH<strong>and</strong>le(h<strong>and</strong>le);<br />

}<br />

code snippet FileSystemTransactions/SafeTransactionH<strong>and</strong>le.cs<br />

All native methods used from .<strong>NET</strong> are defined with the class NativeMethods shown here. With the<br />

sample, the native APIs needed are CreateFileTransacted() <strong>and</strong> CloseH<strong>and</strong>le(), which are defined<br />

as static members of the class. The methods are declared extern because there is no <strong>C#</strong> implementation.<br />

Instead, the implementation is found in the native DLL as defined by the attribute DllImport. Both of these<br />

methods can be found in the native DLL Kernel32.dll. With the method declaration, the parameters<br />

defined with the Windows API call are mapped to .<strong>NET</strong> data types. The parameter txH<strong>and</strong>le represents a<br />

h<strong>and</strong>le to a transaction <strong>and</strong> is of the previously defined type SafeTransactionH<strong>and</strong>le:<br />

using System;<br />

using System.Runtime.ConstrainedExecution;<br />

using System.Runtime.InteropServices;<br />

using System.Runtime.Versioning;<br />

using Microsoft.Win32.SafeH<strong>and</strong>les;<br />

namespace Wrox.ProCSharp.Transactions<br />

{<br />

internal static class NativeMethods<br />

{<br />

[DllImport("Kernel32.dll", CallingConvention = CallingConvention.StdCall,<br />

CharSet = CharSet.Unicode)]<br />

internal static extern SafeFileH<strong>and</strong>le CreateFileTransacted(<br />

String lpFileName,<br />

uint dwDesiredAccess,<br />

uint dwShareMode,<br />

IntPtr lpSecurityAttributes,<br />

uint dwCreationDisposition,<br />

int dwFlagsAndAttributes,<br />

IntPtr hTemplateFile,<br />

SafeTransactionH<strong>and</strong>le txH<strong>and</strong>le,<br />

IntPtr miniVersion,<br />

IntPtr extendedParameter);<br />

[DllImport("Kernel32.dll", SetLastError = true)]<br />

[ResourceExposure(ResourceScope.Machine)]<br />

[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]<br />

[return: MarshalAs(UnmanagedType.Bool)]<br />

internal static extern bool CloseH<strong>and</strong>le(IntPtr h<strong>and</strong>le);<br />

}<br />

}<br />

code snippet FileSystemTransactions/NativeMethods.cs<br />

The interface IKernelTransaction is used to get a transaction h<strong>and</strong>le <strong>and</strong> pass it to the transacted<br />

Windows API calls. This is a COM interface <strong>and</strong> must be wrapped to .<strong>NET</strong> by using COM interop<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!