<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6698224</id><updated>2011-04-21T19:26:46.281-05:00</updated><title type='text'>Daniel Vasquez Lopez</title><subtitle type='html'>Anything I can share about .NET programming.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://danielvl.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>13</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6698224.post-109725766455245616</id><published>2004-10-08T13:32:00.000-05:00</published><updated>2004-10-08T13:09:26.773-05:00</updated><title type='text'>The ExecutionContext class in the .NET Framework 2.0</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;The following example shows a basic use of the new ExecutionContext class. This class allows you to capture the current context where the code is executing in (e.g. security, syncronization, call context, etc.)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;If you use both ThreadPool.UnsafeQueueUserWorkItem and ThreadPool.UnsafeRegisterForWaitForSingleObject methods, you will notice that in your callback method you don't have access to a bunch of context data like the the security context. If you impersonate an User before calling any of the ThreadPool.Unsafe* methods, the identity that will use your callback method will be the process identity, not the impersonated one. &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;This example shows you how to access the impersonated identity from the callback executed by ThreadPoolUnsafeQueueUserWorkItem.&lt;/span&gt;&lt;span style="font-family:Trebuchet MS;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;"&gt;&lt;/span&gt;&lt;span style="font-family:Trebuchet MS;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:78%;"&gt;// Author: danielvl(removethis)@microsoft.com&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Threading;&lt;br /&gt;using System.Security.Principal;&lt;br /&gt;using System.Runtime.InteropServices;&lt;br /&gt;class Program&lt;br /&gt;{&lt;br /&gt;  [DllImport("advapi32.dll")]&lt;br /&gt;  public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);&lt;br /&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;  WindowsImpersonationContext ctxt = null;&lt;br /&gt;  try&lt;br /&gt;  {&lt;br /&gt;    // Impersonate other user&lt;br /&gt;    IntPtr userToken;&lt;br /&gt;    LogonUser("SimpleUser", "MyLocalMachine", "T.hisIsMy.Secret0", 2, 0, out userToken);&lt;br /&gt;    // At this point I am MyDomain\danielvl&lt;br /&gt;    ctxt = WindowsIdentity.Impersonate(userToken);&lt;br /&gt;    // At this point I am MyLocalMachine\SimpleUser&lt;br /&gt;    // Save my current context (MyLocalMachine\SimpleUser)&lt;br /&gt;    ExecutionContext savedContext = ExecutionContext.Capture();&lt;br /&gt;    // Call the unsafe version of ThreadPool.QueueUserWorkItem&lt;br /&gt;    ThreadPool.UnsafeQueueUserWorkItem(new WaitCallback(MyUserWorkItem), savedContext);&lt;br /&gt;  }&lt;br /&gt;  finally&lt;br /&gt;  {&lt;br /&gt;    // Revert identity to MyDomain\danielvl&lt;br /&gt;    if (ctxt != null)&lt;br /&gt;       ctxt.Undo();&lt;br /&gt;  }&lt;br /&gt;  Console.Read();&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Method invoked from ThreadPool.UnsafeQueueUserWorkItem&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;static void MyUserWorkItem(object state)&lt;br /&gt;{&lt;br /&gt;  // Restore the ExecutionContext object&lt;br /&gt;  ExecutionContext context = (ExecutionContext)state;&lt;br /&gt;  // Get the curren username where ThreadPool.UnsafeQueueUserWorkItem is executing&lt;br /&gt;  string userNameFromUnsafeContext = WindowsIdentity.GetCurrent().Name;&lt;br /&gt;  // Execute myContextCallbackHandler in the context saved&lt;br /&gt;  ExecutionContext.Run(context.CreateCopy(), myContextCallbackHandler, userNameFromUnsafeContext);&lt;br /&gt;}&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// The handler to the ContextCallback delegate&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;static ContextCallback myContextCallbackHandler = new ContextCallback(MyContextCallback);&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// Method invoked from ExecutionContext.Run&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;static void MyContextCallback(object state)&lt;br /&gt;{&lt;br /&gt;  string userNameFromUnsafeContext = (string)state;&lt;br /&gt;  string userNameFormSavedContext = WindowsIdentity.GetCurrent().Name;&lt;br /&gt;  Console.WriteLine("Username from unsafe context is {0}", userNameFromUnsafeContext);&lt;br /&gt;  Console.WriteLine("Username from context saved is {0}", userNameFormSavedContext);&lt;br /&gt;}&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:78%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;font-size:78%;"&gt;- Output -&lt;br /&gt;Username from unsafe context is MyDomain\danielvl&lt;br /&gt;Username from context saved is MyLocalMachine\SimpleUser&lt;/span&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-109725766455245616?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/109725766455245616'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/109725766455245616'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/10/executioncontext-class-in-net.html' title='The ExecutionContext class in the .NET Framework 2.0'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-109215065941082081</id><published>2004-08-10T08:50:00.000-05:00</published><updated>2004-08-11T10:09:29.440-05:00</updated><title type='text'>The OVERLAPPED structure in C#</title><content type='html'>&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;The OVERLAPPED structure is widely used in the Win32 functions (some examples are the functions ReadFile, WriteFile, DeviceIOControl, etc.). This structure is used to hold information about asynchronous operations.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;The OVERLAPPED structure is defined as follows:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:78%;"&gt;&lt;span style="font-family:courier new;"&gt;typedef struct _OVERLAPPED &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{ &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;ULONG_PTR Internal; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;ULONG_PTR InternalHigh; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;DWORD Offset; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;DWORD OffsetHigh; &lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;HANDLE hEvent;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;} OVERLAPPED;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Courier New;"&gt;typedef OVERLAPPED* LPOVERLAPPED;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;If you want to P/Invoke a Win32 function that has defined a parameter as LPOVERLAPPED (a pointer to a OVERLAPPED structure), I recommend you to use the already defined and documented &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingnativeoverlappedclasstopic.asp"&gt;&lt;strong&gt;System.Threading.NativeOverlapped&lt;/strong&gt; structure&lt;/a&gt; instead of define your own OVERLAPPED structure in C# (or any other .NET-language).&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;See some examples at &lt;a href="http://www.pinvoke.net/"&gt;http://www.pinvoke.net/&lt;/a&gt;, I already changed all references to the OVERLAPPED structure to System.Threading.NativeOverlapped instead. Be aware that System.Threading.NativeOverlapped is not included in the Microsoft .NET Compact Framework.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-109215065941082081?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/109215065941082081'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/109215065941082081'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/08/overlapped-structure-in-c.html' title='The OVERLAPPED structure in C#'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-108731228766454365</id><published>2004-06-15T10:10:00.000-05:00</published><updated>2004-08-11T10:49:17.503-05:00</updated><title type='text'>How to Ping in C# using System.Management </title><content type='html'>&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;If you are using Windows XP, Windows Server 2003 or above perhaps you might want to avoid using unsupported code for emulating the Ping.exe utility that comes with Windows. Microsoft has introduced the new &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp"&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;Win32_PingStatus&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt; WMI class. You can use the System.Management classes to access an Win32_PingStatus object directly or generate a wrapper by using the &lt;/span&gt;&lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpgrfmanagementstronglytypedclassgeneratormgmtclassgenexe.asp"&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;Management Strongly Typed Class Generator Tool&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;.&lt;br /&gt;Following the second option, open the Visual Studio .NET Command Prompt and type:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:arial;"&gt;C:\&gt;mgmtclassgen.exe Win32_PingStatus&lt;/span&gt;&lt;br /&gt;&lt;/spam&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;This will generate the file "PingStatus.CS". Create a new C# project and add the "PingStatus.CS" file. Use the PingStatus class as follows:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:arial;font-size:78%;"&gt;using System;&lt;br /&gt;using ROOT.CIMV2.Win32;&lt;br /&gt;sealed class MyPing {&lt;br /&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;if(args.Length == 0)&lt;br /&gt;{&lt;br /&gt;Console.Error.WriteLine("Usage: myping");&lt;br /&gt;return;&lt;br /&gt;}&lt;br /&gt;PingStatus ping = Ping(args[0]);&lt;br /&gt;// Check if we got an answer&lt;br /&gt;if(ping.PrimaryAddressResolutionStatus == 0)&lt;br /&gt;{&lt;br /&gt;Console.WriteLine("Resolved: {0}", ping.ProtocolAddress);&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;Console.Error.WriteLine("Error: '{0}' not resolved.", ping.Address); &lt;/span&gt;&lt;span style="font-family:arial;font-size:78%;"&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;static PingStatus Ping(string address)&lt;br /&gt;{&lt;br /&gt;string condition = string.Format("Address='{0}'", address);&lt;br /&gt;foreach(PingStatus ping in PingStatus.GetInstances(condition))&lt;br /&gt;{&lt;br /&gt;return ping;&lt;br /&gt;}&lt;br /&gt;return null;&lt;br /&gt;}}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;The C# code above pings an address (IP or host name) and checks if it got an answer. Please check the &lt;strong&gt;Win32_PingStatus&lt;/strong&gt; WMI class documentation on the &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_pingstatus.asp"&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;MSDN&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt; for property descriptions and status codes.&lt;br /&gt;&lt;/spam&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-108731228766454365?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108731228766454365'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108731228766454365'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/06/how-to-ping-in-c-using.html' title='How to Ping in C# using System.Management '/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-108429343257499076</id><published>2004-05-11T11:24:00.000-05:00</published><updated>2004-08-11T09:53:01.266-05:00</updated><title type='text'>P/Invoke Add-in for Visual Studio.NET</title><content type='html'>&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;This is a very, very useful tool that can help you to find quickly the P/Invoke definition for almost any Win32 API function into the Visual Studio .NET. If the P/Invoke definition does not exist, but you did make it work, you can share your definition with other developers in the globe!.&lt;br /&gt;This tool was developed by &lt;/span&gt;&lt;a href="http://weblogs.asp.net/adam_nathan/archive/2004/05/06/127403.aspx"&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;Adam Nathan&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;, a P/Invoke expert here at Microsoft that wrote a great book &lt;/span&gt;&lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/067232170X/002-7448052-7481611?v=glance"&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;Interoperability in .NET&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;.&lt;br /&gt;This tool communicates with a &lt;/span&gt;&lt;a href="http://www.pinvoke.net"&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;P/Invoke WiKi Web Site&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt; through a &lt;/span&gt;&lt;a href="http://www.pinvoke.net/pinvokeservice.asmx"&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;Web Service&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;.&lt;br /&gt;Try it out, it might save you several minutes debugging...&lt;/span&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-108429343257499076?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108429343257499076'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108429343257499076'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/05/pinvoke-add-in-for-visual-studionet.html' title='P/Invoke Add-in for Visual Studio.NET'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-108413583376487181</id><published>2004-05-09T15:47:00.000-05:00</published><updated>2004-08-11T10:56:37.370-05:00</updated><title type='text'>How to clear the Downloaded Cache programatically?</title><content type='html'>&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;The most important thing that you must considerate before clear the Downloade Cache programmatically it's that the .NET Framework has a default 50MB quota for this cache, so that you don't have to worry about the growing without a size controlled. Additionally, this API won't work for further versions of the .NET Framework. Please check the &lt;/span&gt;&lt;a href="http://blogs.gotdotnet.com/alanshi/CategoryView.aspx/Assembly%20Download"&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;Alan Shi's blog&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt; for more information.&lt;br /&gt;The following C# code clears the Downloaded Cache:&lt;br /&gt;&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-family:arial;"&gt;&lt;span style="font-size:78%;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-family:arial;"&gt;&lt;span style="font-size:78%;"&gt;using System.Runtime.InteropServices;&lt;br /&gt;sealed class App&lt;br /&gt;{&lt;br /&gt;  [DllImport("fusion.dll")]&lt;br /&gt;  static extern void NukeDownloadedCache();&lt;br /&gt;  static void Main()&lt;br /&gt;  {&lt;br /&gt;     NukeDownloadedCache();&lt;br /&gt;  }&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-108413583376487181?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108413583376487181'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108413583376487181'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/05/how-to-clear-downloaded-cache.html' title='How to clear the Downloaded Cache programatically?'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-108215348329302531</id><published>2004-04-16T16:52:00.000-05:00</published><updated>2004-08-11T10:01:45.183-05:00</updated><title type='text'>Use Buffer.BlockCopy instead of Array.Copy for better performance</title><content type='html'>&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;Buffer.BlockCopy is faster than Array.Copy when you are copying primitive arrays (arrays of type System.Int32, System.Char and so forth).&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:Trebuchet MS;font-size:85%;"&gt;The reason is because (with base on &lt;/span&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;the &lt;/span&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=3A1C93FA-7462-47D0-8E56-8DD34C6292F0&amp;displaylang=en"&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;SSCLI implementation&lt;/span&gt;&lt;/a&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;) both Buffer.Blockcopy and Array.Copy, call the method m_memmove (declared in comsystem.cpp) that copies data byte per byte from one array to another, &lt;strong&gt;but&lt;/strong&gt; the major difference is that Array.Copy performs more validations and operations than Buffer.BlockCopy before calling m_memmove.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;Buffer.BlockCopy just checks if bonderies are valid and if the array's data type is primitive, but Array.Copy checks thinks like type compatibility, casting, boxing, unboxing, among other things.&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-108215348329302531?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108215348329302531'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108215348329302531'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/04/use-bufferblockcopy-instead-of.html' title='Use Buffer.BlockCopy instead of Array.Copy for better performance'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-108178644441926150</id><published>2004-04-12T11:03:00.000-05:00</published><updated>2004-08-11T10:02:55.036-05:00</updated><title type='text'>How to Break Software Security Book</title><content type='html'>&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;It focuses on find bugs that cause security vulnerabilities in your software. I really think that you should read this book if you are involved in a testing or software engineering role in a project. Check this &lt;/span&gt;&lt;a href="http://www.amazon.com/exec/obidos/tg/detail/-/0321194330/qid=1081785919/sr=1-1/ref=sr_1_1/002-7387988-5478463?v=glance&amp;amp;s=books"&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;"&gt;link&lt;/span&gt;&lt;/a&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt; to find out more details.&lt;br /&gt;This book this is widely recommended by Michael Howard [MSFT], co-author of Writing Secure Code.&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-108178644441926150?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108178644441926150'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108178644441926150'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/04/how-to-break-software-security-book.html' title='How to Break Software Security Book'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-108078679563692343</id><published>2004-03-31T19:12:00.000-06:00</published><updated>2004-08-11T10:05:29.516-05:00</updated><title type='text'>The action being performed on this control is being called from the wrong thread.</title><content type='html'>&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="color:#000000;"&gt;&lt;em&gt;"The action being performed on this control is being called from the wrong thread. You must marshal to the correct thread using Control.Invoke or Control.BeginInvoke to perform this action."&lt;/em&gt;&lt;br /&gt;&lt;strong&gt;What does that Exception mean?&lt;/strong&gt;&lt;br /&gt;Window Forms Controls were not designed to be thread-safe. To be sure of that, check the member property Control.InvokeRequired of any control like the System.Windows.Forms.TreeView. If it is true, then you require to call the control from the thread where it was created by using Control.Invoke.&lt;br /&gt;You will need to use &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;a href="http://msdn.microsoft.com/library/en-us/csref/html/vcrefTheDelegateType.asp"&gt;&lt;span style="font-family:trebuchet ms;font-size:85%;color:#000000;"&gt;delegates&lt;/span&gt;&lt;/a&gt;&lt;span style="color:#000000;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;. The following code declares a delegate that is used to marshal the call to the main thread:&lt;br /&gt;&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-family:arial;font-size:85%;color:#000000;"&gt;private delegate void GeneralDelegate(object state);&lt;br /&gt;private GeneralDelegate invoker;&lt;br /&gt;private void MethodRunningInMainThread(object state)&lt;br /&gt;{&lt;br /&gt;   treeView1.Nodes.Add(new TreeNode((string)state));&lt;br /&gt;}&lt;br /&gt;private void EventHandlerRunningInOtherThread(object sender, System.EventArgs e)&lt;br /&gt;{&lt;br /&gt;   invoker = new GeneralDelegate(MethodRunningInMainThread);&lt;br /&gt;   treeView1.Invoke(invoker, new object[]{"Some Text"});&lt;br /&gt;}&lt;br /&gt;&lt;/spam&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="color:#000000;"&gt;Instead of using the TreeView control in the &lt;strong&gt;EventHandlerRunningInOtherThread&lt;/strong&gt; method, it creates a delegate (that could be created in the class contructor once) and calls the TreeView.Invoke method in order to marshal the values requiered ("Some Text") in the &lt;strong&gt;MethodRunningInMainThread&lt;/strong&gt; method by the TreeView control.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-108078679563692343?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108078679563692343'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108078679563692343'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/03/action-being-performed-on-this-control.html' title='The action being performed on this control is being called from the wrong thread.'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-108078063160225320</id><published>2004-03-31T18:38:00.000-06:00</published><updated>2004-08-11T10:07:03.126-05:00</updated><title type='text'>Enabling XP Visual Style in Windows Forms</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;Back to the .NET Framework version 1.0, the requirement to enable Windows XP Visual Style in a Windows Forms Application was a little dull, adding a xml manifest file into the application resources (opening the assembly from VS.NET). Now in .NET Framework version 1.1, you just need to call the static method &lt;strong&gt;Application.EnableVisualStyles&lt;/strong&gt; just before calling the method Application.Run in your Main method:&lt;/spam&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:arial;"&gt;static void Main()&lt;br /&gt;{&lt;br /&gt;  Application.EnableVisualStyles();&lt;br /&gt;  Application.Run(new frmMainForm());&lt;br /&gt;}&lt;/span&gt;&lt;/spam&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;You also need to change the standard flat sytle of all the Button controls in your form to FlatSytle.System:&lt;br /&gt;&lt;em&gt;btnSubmit.FlatStyle = System.Windows.Forms.FlatStyle.System;&lt;/em&gt;&lt;br /&gt;&lt;br /&gt;Of course, you need any Windows XP version or above.&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-108078063160225320?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108078063160225320'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108078063160225320'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/03/enabling-xp-visual-style-in-windows.html' title='Enabling XP Visual Style in Windows Forms'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-108067136703332105</id><published>2004-03-30T12:19:00.001-06:00</published><updated>2004-08-11T10:30:11.570-05:00</updated><title type='text'>Marshal.SizeOf returns 1 for System.Char</title><content type='html'>&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;As you might know, in .NET all strings are Unicode, because of that, the size of a System.Char type must be 2 bytes. When you try to get the size in bytes of a System.Char using the Marshal.SizeOf method, you will find that it return 1 instead of 2.&lt;br /&gt;This is not a bug, Marshal.SizeOf was made for unmanaged types, not for managed types as System.Char. Marshal.SizeOf gets the size of the System.Char type declared in the mscorlib assembly, if you check its declaration using the "Lutz Roeder's .NET Reflector" you will notice that the System.Char type has the following modifiers:&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Modifiers: Serializable BeforeFieldInit &lt;/em&gt;&lt;strong&gt;Ansi&lt;/strong&gt;&lt;em&gt; Auto &lt;/em&gt;&lt;br /&gt;&lt;br /&gt;By this reason, the size of any Ansi character &lt;strong&gt;is 1&lt;/strong&gt;. In order to get size of a character, you should use the value of &lt;strong&gt;Marshal.SystemDefaultCharSize&lt;/strong&gt;.&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-108067136703332105?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108067136703332105'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108067136703332105'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/03/marshalsizeof-returns-1-for-systemchar.html' title='Marshal.SizeOf returns 1 for System.Char'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-108067013355860924</id><published>2004-03-30T11:48:00.005-06:00</published><updated>2004-08-11T10:38:40.720-05:00</updated><title type='text'>How to recursively search a subdirectory or a file in a given directory</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;The question is a little tricky, because we immediately think about write a recursive method, but there are some issues that you might want to considerate; Think about the &lt;strong&gt;Call Stack&lt;/strong&gt;, what if the directory is too deep, enough to run into a &lt;strong&gt;StackOverflowException&lt;/strong&gt; exception, or simple as you are paying a performance penalty.&lt;br /&gt;&lt;br /&gt;Instead, try to use a Stack to remember the directories to look up:&lt;/spam&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:arial;"&gt;&lt;span style="font-size:78%;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:arial;"&gt;&lt;span style="font-size:78%;"&gt;sealed class App {&lt;br /&gt;  static void Main() {&lt;br /&gt;    StringCollection results = FindDirectory("c:\\Development", "compressionsink.dll", "*");&lt;br /&gt;    foreach(string value in results)&lt;br /&gt;      Console.WriteLine(value);&lt;br /&gt;  }&lt;br /&gt;  public StringCollection FindDirectory(string root, string value, string searchPattern)&lt;br /&gt;  {&lt;br /&gt;    StringCollection results = new StringCollection();&lt;br /&gt;    Stack stack = new Stack();&lt;br /&gt;    stack.Push(root);&lt;br /&gt;    while(stack.Count &gt; 0) {&lt;br /&gt;      string currentDir = stack.Pop().ToString();&lt;br /&gt;      if(0 == string.Compare(Path.GetFileName(currentDir), value, true, CultureInfo.InvariantCulture)) {&lt;br /&gt;        results.Add(currentDir);&lt;br /&gt;      }&lt;br /&gt;      foreach(string path in Directory.GetFiles(currentDir, searchPattern)) {&lt;br /&gt;        string ok = Path.GetFileName(path);&lt;br /&gt;        if(0 == string.Compare(ok, value, true, CultureInfo.InvariantCulture)) {&lt;br /&gt;          results.Add(path);&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;     foreach(string dir in Directory.GetDirectories(currentDir)) {&lt;br /&gt;       stack.Push(dir);&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;    return results;&lt;br /&gt;  }&lt;br /&gt;}&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-108067013355860924?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108067013355860924'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108067013355860924'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/03/how-to-recursively-search-subdirectory.html' title='How to recursively search a subdirectory or a file in a given directory'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-108066866804350595</id><published>2004-03-30T11:38:00.000-06:00</published><updated>2004-08-11T10:40:47.600-05:00</updated><title type='text'>Check if a local account is enabled or disabled in .NET</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:arial;color:#000000;"&gt;DirectoryEntry de = new DirectoryEntry("WinNT://&lt;machinename&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:arial;color:#000000;"&gt;/&lt;localaccount&gt;");&lt;br /&gt;int value = Convert.ToInt32(de.Properties["UserFlags"][0]);&lt;br /&gt;if((value &amp; 0x002) == 0)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="color:#000000;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:arial;"&gt;  Console.WriteLine("This account is enabled.");&lt;/span&gt;&lt;/spam&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="color:#000000;"&gt;If is an AD Account, check the property &lt;strong&gt;userAccountControl&lt;/strong&gt; instead.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-108066866804350595?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108066866804350595'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108066866804350595'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/03/check-if-local-account-is-enabled-or.html' title='Check if a local account is enabled or disabled in .NET'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-6698224.post-108066759499520135</id><published>2004-03-30T11:24:00.001-06:00</published><updated>2004-08-11T10:43:00.426-05:00</updated><title type='text'>Win32Exception class forgotten</title><content type='html'>&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;It’s very common to see people (and books) declaring the FormatMessage Win32 API function in .NET in order to get the message of the Last Win32 Error Code.&lt;br /&gt;In .NET, if you want to get the message error of the error code returned by the last unmanaged function called using Platform Invoke, use:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-size:78%;"&gt;String message = (new System.ComponentModel.Win32Exception()).Message;&lt;/span&gt;&lt;/em&gt;&lt;br /&gt;&lt;/spam&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;Instead of P/Invoke the FormatMessage Win32 API function, like:&lt;br /&gt;&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-size:78%;"&gt;[DllImport("kernel32.dll", CharSet=CharSet.Auto)]&lt;br /&gt;static extern int FormatMessage(int dwFlags,ref IntPtr lpSource,&lt;br /&gt;int dwMessageId,int dwLanguageId,ref String lpBuffer,int nSize,&lt;br /&gt;IntPtr Arguments)&lt;/span&gt;&lt;br /&gt;&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-size:85%;"&gt;Even the Microsoft Configuration Management Application Block for .NET (http://msdn.microsoft.com/library/en-us/dnbda/html/cmab.asp) declares FormatMessage in its DataProtection class and uses the following code instead of writing a simple line (new Win32Exception()).Message :&lt;br /&gt;&lt;/spam&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;span style="font-family:arial;"&gt;&lt;span style="font-size:78%;"&gt;private static String GetErrorMessage(int errorCode)&lt;br /&gt;{&lt;br /&gt;  int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;&lt;br /&gt;  int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;&lt;br /&gt;  int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;&lt;br /&gt;  int messageSize = 255;&lt;br /&gt;  String lpMsgBuf = "";&lt;br /&gt;  int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER  FORMAT_MESSAGE_FROM_SYSTEM  FORMAT_MESSAGE_IGNORE_INSERTS;&lt;br /&gt;  IntPtr ptrlpSource = new IntPtr();&lt;br /&gt;  IntPtr prtArguments = new IntPtr();&lt;br /&gt;  int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref lpMsgBuf, messageSize, IntPtr.Zero );&lt;br /&gt;  if(0 == retVal)&lt;br /&gt;  {&lt;br /&gt;    throw new Exception( Resource.ResourceManager[ "Res_ExceptionFormattingMessage", errorCode ] );&lt;br /&gt;  }&lt;br /&gt;  return lpMsgBuf;&lt;br /&gt;}&lt;/spam&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:trebuchet ms;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6698224-108066759499520135?l=danielvl.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108066759499520135'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6698224/posts/default/108066759499520135'/><link rel='alternate' type='text/html' href='http://danielvl.blogspot.com/2004/03/win32exception-class-forgotten.html' title='Win32Exception class forgotten'/><author><name>Daniel</name><uri>http://www.blogger.com/profile/11219787932132490222</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author></entry></feed>
