tag:blogger.com,1999:blog-49277794613666196872017-07-04T13:36:24.589+03:00std::string strMyBlog = "Chris Taylor";//TODO: Ramblings of a software development enthusiastChris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.comBlogger29125tag:blogger.com,1999:blog-4927779461366619687.post-57750607827234038572016-11-06T01:27:00.000+02:002016-11-28T07:16:09.218+02:00Cross Compile for Raspberry PI on Windows 10Cross Compiling for Raspberry PI or other ARM based devices capable of running Linux, on Windows 10 using the "Window subsystem for Linux" (aka Bash on Windows) is now as simple as cross compiling on native Linux.<br /><br />To learn more about the Windows subsystem for Linux and how to activate it you can follow the instructions <a href="http://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/">here</a>.<br /><br />Once you have completed that you can follow these steps to install the toolchain required to cross compile for the Raspberry PI.<br /><b><br /></b><b>Step 1</b> - Make sure your environment is up to date<br /><blockquote class="tr_bq">$ sudo apt-get update<br />$ sudo apt-get upgrade&nbsp;</blockquote><b>Step 2</b> - Install the development tools<br /><blockquote class="tr_bq">$ sudo apt-get install -y build-essential gcc-arm-linux-gnueabi g++-arm-linux-gnueabi&nbsp;</blockquote>For the very basics, that is it...<br /><br />Let's try build some code and get it to run on the Raspberry PI<br /><br /><b>Step 3</b> - Create a source directory<br /><br />In the bash shell you can create a "source" folder in the user directory and switch to the directory by issuing the following commands<br /><blockquote class="tr_bq">$ mkdir ~/source<br />$ cd ~/source</blockquote>The first command will create a "source" directory in the current users home directory, the second will change the current directory to the newly created "source" directory.<br /><br /><b>Step 4</b> - Write some code<br /><br />First we will launch the nano text editor, in the Windows bash shell, by issuing the following<br /><blockquote class="tr_bq">$ nano test.cpp&nbsp;</blockquote>This command will open the nano editor and set the file name to "test.cpp". Enter the following code in the editor window<br /><blockquote class="tr_bq">#include &lt;iostream&gt;<br/>int main()&nbsp;</iostream><iostream><br />{<br />&nbsp;</iostream><iostream>&nbsp; &nbsp;std::cout &lt;&lt; "Hello from your Raspberry PI!" &lt;&lt; std::endl;<br />&nbsp;</iostream><iostream>&nbsp; &nbsp;return 0;&nbsp;</iostream><iostream><br />}&nbsp;</iostream></blockquote><iostream><br /></iostream><iostream>Press "Ctrl-X" followed by "y" followed by "enter".&nbsp;</iostream><br /><iostream><br /></iostream><iostream>Now you should be back at the command prompt in the source directory we created earlier. Issuing the "ls" command should show that we have a new file called "test.cpp".&nbsp;</iostream><br /><iostream><b><br /></b></iostream><iostream><b>Step 5</b> - Compiling using the cross compiler</iostream><br /><iostream><br /></iostream><iostream>Next, we can compile the code for the Raspberry PI using the following command &nbsp;</iostream><br /><blockquote class="tr_bq"><iostream>$ arm-linux-gnueabi-g++ test.cpp -o test&nbsp;</iostream></blockquote><iostream>This will start the compiler, which will compile the test.cpp file and if you have entered the code correctly, create an executable binary file called "test". If you have any errors you can go back to step 4 above and use the same commands to edit the file and fix any errors you might have encountered.&nbsp; </iostream><br /><iostream><br /></iostream><iostream>Assuming you had no errors, you should now have a binary file on your machine called "test". This is the executable that is ready to run on an ARM based device running Linux, like your Raspberry PI. We can confirm that it is indeed not compatible with the x64 architecture of your Windows machine by trying to execute the code as follow.&nbsp;</iostream><br /><blockquote class="tr_bq"><iostream>$ ./test&nbsp;</iostream></blockquote><iostream>The application should fail to execute and give a messages along the lines of&nbsp;</iostream><br /><iostream><i><br /></i></iostream><br /><blockquote class="tr_bq"><iostream><i>bash: ./test: cannot execute binary file: Exec format error.</i>&nbsp;</iostream></blockquote><iostream><br /></iostream><iostream>This is because the binary is not in the correct format for the platform you are trying to execute it on. We need to copy the binary over to your Raspberry PI and try execute the binary there</iostream>.<br /><b><br /></b><b>Step 6</b> - Copy the binary to the Raspberry PI<br /><br />I am going to assume that you already have your Raspberry PI on the network and have identified the IP address, if not you can use Google or Bing to go get to that point.<br /><br />In my case, my Raspberry PI is on the network and has been assigned an IP address of 192.168.0.50.<br /><b><br /></b><b>&nbsp; Note:</b>You need to replace the IP addresses below with the IP address of &nbsp;<b>your </b>Raspberry PI.<br /><br />To copy the binary from your windows machine to the Raspberry PI we can use secure copy (scp), this is a Unix/Linux command to securely copy files between two machines using a secure shell. The Windows subsystem for Linux makes these commands available to you "natively" from within the bash shell.<br /><br />Enter the command below to copy the file from the Windows machine to your Raspberry PI (remember to change the IP address to the IP of your Raspberry PI)<br /><blockquote class="tr_bq">$ scp test pi@192.168.0.50:/home/pi&nbsp;</blockquote>If this is the first time you are setting up a secure connection to the Raspberry PI from bash, either using scp or ssh you will be prompted to confirm that you trust the target machine, you can enter "yes" to have the host signature entered into your local known hosts file.<br /><br /><b>Note:</b> If you took too long to confirm the host, the underlying connection might have been broken, in that case you can just reissue the scp command above.<br /><br />Next you will need to enter the password, the default password for the "pi" user is "raspberry" which you can enter in response to the password prompt. When the command completes without errors, the binary should be copied over to your Raspberry. Let's go check...<br /><br /><b>Step 7</b> - Executing the binary on the Raspberry PI<br /><br />From within the bash shell on your Windows machine, you can now ssh to to the Raspberry PI. If you prefer you could use Putty, but since we have the capability to use the Windows bash shell to do this I am going to stick with that. The next command will open a ssh session to your Raspberry PI, again, remember to use the IP address for your Raspberry and not the IP address in the example (unless of course your IP matches the on in the example :) )<br /><blockquote class="tr_bq">$ ssh pi@192.168.0.50</blockquote>Next, you will be prompted for the password for the user "pi", enter "raspberry" and you should now have an interactive secure session with your Raspberry PI. You can identify that you are interacting with the Raspberry and not the local shell by looking at the prompt, it should be a bright green prompt with the following text.<br /><blockquote class="tr_bq">pi@raspberrypi:~ $&nbsp;</blockquote>If you see the above, you know you are now remotely interacting with the Raspberry. Entering "ls" command should list the "test" file, also in bright green, that you copied from the Windows machine.<br /><br />Lets try executing the binary and see if we have more luck than we did when executing it earlier on the Windows machine.<br /><br />At the prompt enter "./test", you should then see the following<br /><br /><blockquote class="tr_bq">pi@raspberrypi:~ $ ./test<br />Hello from your Raspberry PI!&nbsp;</blockquote><br />If you see that then it worked! You compiled a source file on your Windows machine, using the ARM tools to target an ARM based Linux distribution, copied the file to the target device and executed it. How awesome is that???<br /><br />There is still much more to this, but this should get you started cross compiling on Windows for your Raspberry PI. Best of all, this will also work for other ARM based devices capable of running Linux, like the BeagleBone Black for example.<br /><br />For a more complete experience, take a look at:<br /><br /><a href="http://visualgdb.com/">VisualGDB </a>- Use Visual Studio to target a range of embedded devices and Linux devices<br /><a href="https://visualstudiogallery.msdn.microsoft.com/725025cf-7067-45c2-8d01-1e0fd359ae6e">Visual C++ for Linux development</a> - Use Visual Studio to compile and deploy to ARM based Linux devices<img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/OVZLlfI1ZV0" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2016/11/cross-compile-for-raspberry-pi-on.htmltag:blogger.com,1999:blog-4927779461366619687.post-2770855751009611352016-02-09T05:41:00.001+02:002016-02-09T05:54:10.010+02:00Learning a little TypeScript - Implementing a generic Queue collectionNot being much of a JavaScript guy, other than dabbling when I needed a little client side script, I thought I should probably join the rest of the world and move away from the comfort of C/C++ and C#. Taking baby steps I&nbsp;tried my&nbsp;hand at TypeScript first. I thought others might find my first attempt useful.<br /><br />Wanting to try out classes and generics, I decided to implement a toy collection class. The snippet&nbsp; is loosely based on the .NET generic Queue<t> collection interface and implements a similar collection in TypeScript.</t><br /><br />Without further ado, here is my very first TypeScript snippet.<br /><br /><script src="https://gist.github.com/anonymous/8f73e96a7e9682a9d4e4.js"></script><br /><br />And here is a quick and dirty sample showing how the queue could be used<br /><br /><script src="https://gist.github.com/anonymous/fa01f36de3ce992343b4.js"></script><br /><img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/4zC2-Mi26Ac" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2016/02/learning-little-typescript-implementing.htmltag:blogger.com,1999:blog-4927779461366619687.post-75018625048903274932014-07-09T02:42:00.003+03:002014-07-09T02:42:37.513+03:00Taking my hobby playing with embedded devices to the next levelI have been playing around with all sorts of embedded micro-controllers, ranging from the Atmel AVRs on the Arduino boards, to the powerful ARM Cortex processors running .NETMF and plenty of native code in-between. I recently decided I wanted to try my hand at FPGA, it looked totally foreign and it is not like anything else I had ever done.<br /><br />All I can say is I am hooked, having the power to construct a processor or logic array to your liking just feels cool. I started off just over a week ago and I took it slow, implementing half-adders then full-adders until I eventually build a full ALU. My goal is to build a custom processor designed to run the Pascal PCode generated by the original UCSD-Pascal compilers.<br /><br />So far I have not had much to show for it, everything toggles a few LEDs drives a few 7-Segment displays etc. Today I thought I would start playing with the clock management features, the thing with the clocks is without an oscilloscope you cannot really see if things are happening the way you expect. So I thought I would kill two birds with one stone. I will eventually need to generate video output and I need a custom clock to generate the right frequency to get the monitor to display the video data at the target resolution.<br /><br />The Paplio board that I am using has a Xilinx Spartan 6 FPGA which is being clocked at 32MHz, I was able to synthesize a frequency of 64MHz, close enough to the 65MHz required to support VGA 1024x768 signal. This is not the limit, I am sure it can still go higher, I just have not tried it yet.<br /><br />Here is a quick video of the system in action <br /><div class="separator" style="clear: both; text-align: center;"><iframe allowFullScreen='true' webkitallowfullscreen='true' mozallowfullscreen='true' width='320' height='266' src='https://www.youtube.com/embed/Maf5a1D23ds?feature=player_embedded' FRAMEBORDER='0' /></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: left;">Here is a link to the hardware that I am using</div><div class="separator" style="clear: both; text-align: left;">http://papilio.cc/index.php?n=Papilio.Hardware</div><br /><img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/MRbvzVW7ecs" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2014/07/taking-my-hobby-playing-with-embedded.htmltag:blogger.com,1999:blog-4927779461366619687.post-8658063329101386372012-09-08T11:42:00.000+03:002012-09-08T11:42:20.579+03:00New Gadgeteer Hardware and More...Christmas came early... I ordered a bunch of kit that arrived yesterday, just in time for the weekend. <a href="http://3.bp.blogspot.com/-HmWqFOhqtyI/UEr9rS7dq0I/AAAAAAAAAzQ/pcXcMkFfDrs/s1600/EarlyChristmas.JPG" imageanchor="1"><img border="0" height="240" src="http://3.bp.blogspot.com/-HmWqFOhqtyI/UEr9rS7dq0I/AAAAAAAAAzQ/pcXcMkFfDrs/s320/EarlyChristmas.JPG" width="320" /></a> <br /><strong></strong><br /><strong>Development Boards</strong><br /><a href="http://www.ghielectronics.com/catalog/product/332" target="_blank">FEZ Hydra Basic Kit</a><br /><a href="http://www.ghielectronics.com/catalog/product/349" target="_blank">FEZ Cerberus Mainboard</a><br /><a href="http://www.st.com/internet/evalboard/product/252419.jsp" target="_blank">ST Development Board (STM32F4Discovery)</a><br /><br /><strong>Modules</strong><br /><a href="http://www.newhavendisplay.com/nhd0420d3znswbbwv3-p-5745.html" target="_blank">Newhaven - LCD Character Display</a><br /><a href="http://www.ghielectronics.com/catalog/product/330" target="_blank">USB Client SP Module</a><br /><a href="http://www.ghielectronics.com/catalog/product/355" target="_blank">RS232 Module</a><br /><a href="http://www.ghielectronics.com/catalog/product/343" target="_blank">OLED Display Module (128x128)</a><br /><a href="http://www.ghielectronics.com/catalog/product/333" target="_blank">Ethernet ENC28 Module</a><br /><a href="http://www.ghielectronics.com/catalog/product/273" target="_blank">Extender Module</a><br /><br />So far&nbsp;I have tested the OLED, it is much smaller than I expected but it is really a nice little screen. I can see it being very useful for low volume data that needs to be displayed.<br /><br />The real fun so far was the Newhaven LCD Character display. I am a software guy, so having to solder does not make me feel comfortable. Don't get me wrong, soldering a few header pins on to a board is no issue.<br /><br />The data sheet for the display refers to jumpers being set to select the communication mode for the display. but when I opened the device there were no jumpers to be found. A quick Bing search and it turns out that what they call jumper are really two pads on the board that need to be shorted. And those pads sit right next to the processor on the display controller. <br /><br />When I finally decided to bite the bullet and drop a little solder between the pads, my 11 year old daughter came to check-up on what I was doing. She saw the tiny area I was tackling with what seems like a huge soldering iron and promptly offered to help, she suggested I put that the board under a magnifying glass, which I did and the end result I finally wrote a 'Hello World' .NETMF application.<br /><br /><div class="separator" style="clear: both; text-align: center;"><a href="http://1.bp.blogspot.com/-VLnnUKDIsJI/UEsEvfw2HpI/AAAAAAAAAzo/Zf1wALfH0bk/s1600/Display-HelloWorld.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="166" src="http://1.bp.blogspot.com/-VLnnUKDIsJI/UEsEvfw2HpI/AAAAAAAAAzo/Zf1wALfH0bk/s320/Display-HelloWorld.png" width="320" /></a></div><div class="separator" style="clear: both; text-align: center;">&nbsp;</div><br />Let me tell you I was quite relieved when it all worked. <br /><br />Now to start playing with the STM32F4 Discovery board... <br /><img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/kKge_uhCPco" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2012/09/new-gadgeteer-hardware-and-more.htmltag:blogger.com,1999:blog-4927779461366619687.post-64312675955024066802012-05-26T12:32:00.001+03:002012-05-26T12:32:44.910+03:00Mini-Pacman on Miniature Arcade Console<p>I recently <a href="http://taylorza.blogspot.com/2012/05/playing-with-net-micro-framework-and.html">blogged</a> about some of the stuff I am playing with on the .NET Micro Framework Gadgeteer platform. Kenny Spade from the <a href="http://www.tinyclr.com">TinyCLR</a> community forums has kindly built a miniature arcade and put up a video of Mini-Pacman running on the arcade.</p> <iframe height="315" src="http://www.youtube.com/embed/3OmvOt-V0Yg" frameborder="0" width="560" allowfullscreen="allowfullscreen"></iframe> <p>It is so cool to see your work move from a bunch of loose pieces lying around on a desk to something that looks so polished. Thanks Kenny!</p> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/wd5RzPn3yWg" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com2http://taylorza.blogspot.com/2012/05/mini-pacman-on-miniature-arcade-console.htmltag:blogger.com,1999:blog-4927779461366619687.post-79672118566836851042012-05-20T15:45:00.001+03:002012-05-20T16:03:48.610+03:00Playing with .NET Micro Framework and GadgeteerI am sure most people have heard about the .NET Micro Framework and more recently Gadgeteer. While I have had a keen interest for sometime, I never got around to purchasing any of the hardware until about 3 weeks ago. <br /><br />Here are a few things I have done in my endeavor to learn more about the framework and the embedded devices. <br /><br />The first application I developed was a little Pac man clone, I made the source for this available on codeplex just follow this link <a href="http://chrismcstuff.codeplex.com/" title="http://chrismcstuff.codeplex.com/">http://chrismcstuff.codeplex.com/</a><br /><iframe width="420" height="315" src="http://www.youtube.com/embed/lrjgm6SnuLM" frameborder="0" allowfullscreen></iframe><br />More recently, I have been writing a ray-casting engine in the style of Wolfenstein 3D. Eventually I hope to release this as well, with a simple interface that others can use to develop games of this style.<br /><br />Here is the latest video of the ray-casting engine.<br /><iframe allowfullscreen="allowfullscreen" frameborder="0" height="315" src="http://www.youtube.com/embed/8_8V_ulJcTc" width="420"></iframe> <br />I will post updates as I make more progress.<br /><br />I am doing this on the <a href="http://www.ghielectronics.com/catalog/product/297">FEZ Spider</a> from <a href="http://www.ghielectronics.com/">GHI Electronics</a><img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/SXirVJVw9d4" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2012/05/playing-with-net-micro-framework-and.htmltag:blogger.com,1999:blog-4927779461366619687.post-74264336530055418802011-10-02T19:04:00.001+03:002011-10-02T19:37:23.600+03:00My first attempt at using the HTML 5 Canvas<p>Today I found myself wondering what kind of performance I can get with Javascript and the new HTML 5 Canvas. Looking for something fun to implement quickly I dug up some code for a demo I did of the Silverlight WriteableBitmap and started porting that to Javascript.</p> <p>Now I am not much of a Javascript guy, in fact I can safely say that this is probably the most Javascript I have written, probably more than all other previous attempts at playing with Javascript put together. </p> <p>So why am I writing about this if I lack so much experience, well to be honest, it is because I was actually quite surprised by the results and I thought it would be worth sharing. </p> <p>I did the initial development using IE 9, and I was impressed by the performance, I hit the test page using Firefox 7, Google Chrome 14 and Safari 5.1. By far Firefox was the slowest, IE 9, Chrome and Safari all performed really well on my Window 7 box. So if you are viewing this in Firefox, try it with IE 9 and see if your experience is similar.</p> <iframe width="360" height="280" src="http://dotnetwarrior.com/demos/metaballs/metaballs.htm" frameborder="0"> </iframe> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/DKUbxAfCrX4" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2011/10/my-first-attempt-at-using-html-5-canvas.htmltag:blogger.com,1999:blog-4927779461366619687.post-20295911819251747812011-09-06T18:39:00.001+03:002011-09-07T10:50:10.783+03:00Practical Algorithms and Data Structures : Arrays–Part 1<p>To understand arrays, you first need to understand some basic concepts of how computer memory is managed. This is going to be a very high-level explanation with many over simplifications, basically I hope to share just enough to ensure that you don’t get lost when we get into the details of arrays. </p> <p>You can think of computer memory as a collection of blocks which are placed sequentially one after the other in a row. Each block is uniquely identified by a number, this number in known as a memory address. For example, the first block would be at memory address 0 the next at memory address 1 and so on until you reach the last block.</p> <p>Each of of these blocks or memory locations can store a single piece of data, this piece of data is known as a byte. A byte is made-up of 8 bits where each bit can have a binary state of either ‘1’ or ‘0’, 256 unique combinations of 1s and 0s can be formed for a byte. The interpretation or meaning associated to each of these patterns depends on what part of the system is looking at the data and how it has chosen to interpret the data. For example if your application is interpreting the data in the memory area as letters of the alphabet you might choose to interpret the bit pattern 01000001 as the upper case letter ‘A’. If on the other hand your application is treating the data as a numerical quantity representing someone's age for example, that same bit pattern would be interpreted as the numeric value 65. (See note 1 below)</p> <p>More complex data can be represented by combining the patterns of multiple memory locations and interpreting those patterns appropriately. Let me give you an example.</p> <p>Suppose you wanted to store a string of characters in memory, you could choose to store that string with a length prefix which indicates how many characters are in the string, and the subsequent memory locations would contain the bytes that represent each character of the string. </p> <p><a href="http://lh5.ggpht.com/-S9JIRpI-6jo/TmY-sYYlRkI/AAAAAAAAAyA/1d_xVv6Lg_k/s1600-h/String-in-memory5.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="String in memory" border="0" alt="String in memory" src="http://lh3.ggpht.com/-gm_dRZLjT08/TmY-tOnkNKI/AAAAAAAAAyE/YWRFac4P_3w/String-in-memory_thumb1.png?imgmax=800" width="205" height="70" /></a></p> <p>The above image represents a string stored in memory with a length prefix of 5. Memory location 0 contains the bit pattern for the number 5 indicating that the next 5 memory locations contain the character data of the string. Memory location 1 contains the bit pattern 01100011 which has the numeric value of 99, but because we know this is a character of a string we will map this to the character ‘c’ (We are using an ASCII mapping here, see note 2 below), the ‘r at memory address 3 is encoded as 01110010 which represents the numeric value 114. This mapping is done for the 5 memory locations after the length prefix. </p> <p>Similarly we need encoding mechanisms to store large numeric integer values, real numbers which can represent fractions of a whole number etc. As you might have already realized, the data stored in a single memory location can be quite limiting having only 256 possible values. That is not much to work with, it might be fine for storing your age, but what about the population of a country? As with the example above where we used multiple memory locations to store a string, a similar solution can be found for storing larger numbers that will require multiple memory locations which when interpreted as a whole represent a larger numeric value. For example, using 4 memory locations we have a total of 32 bits of data which gives you 4,294,967,295 unique bit patterns which covers us for the population for any country in the world, we need to go even bigger if we want to store the entire population of the world however. </p> <p>The important thing here is that you know up front how to interpret the various pieces of information stored in the memory and how many bytes make up a single unit of information.</p> <p>As you can imagine, these encodings get quite complex, fortunately you rarely need to deal with these low level details, for the most part the details are nicely taken care of for us by the higher level tools that we use to write our software. </p> <p>The important take away from this post, is that more complex data representations can require multiple memory locations to store a single instance of data. </p> <hr /> <p><a href="http://taylorza.blogspot.com/2011/07/practical-algorithms-and-data.html">Prev – Introduction</a></p> <hr /> <p>* Notes: </p> <ol> <li>The bit patterns are not random, they follow a numbering scheme known as the binary system. Read more about this <a href="http://en.wikipedia.org/wiki/Binary_numeral_system">here</a>. </li> <li>Interpreting the bit patterns as characters is done using a look-up table, in this case I have used the ASCII table which you can read more about <a href="http://en.wikipedia.org/wiki/Ascii">here</a>. </li> </ol> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/a-zrhJBFGeQ" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2011/09/practical-algorithms-and-data.htmltag:blogger.com,1999:blog-4927779461366619687.post-81566691107210729842011-07-09T21:28:00.000+03:002011-09-07T10:52:51.564+03:00Practical Algorithms and Data Structures : Introduction<p>Welcome to the first of hopefully many posts on Algorithms and Data Structures.</p> <p>One of the areas of computer science that I have always felt drawn to is the study of algorithms. That is not to say that I have developed any kind of specialized expertise in the field, but I do find it tremendously interesting and continuously strive to expand on my understanding and ability to apply algorithms effectively.</p> <p>For me at least, the best way to evolve my knowledge of a topic is to attempt to explain some aspect of the particular topic to someone else. On countless occasions I have found that by answering questions and having my explanations challenged, has enabled me to reach new levels of that “Aha” moment when suddenly a topic that I thought I understood just became that much clearer. </p> <p>Now you might be wondering why I would even bother writing something like this, especially given all the material out there covering this specific area. Well other than my selfish motivation to learn more in the process, I have also realized that so many developers today lack the basics in terms of even the simplest algorithms. They might know the terminology and sometimes not even that, but as soon as you start probing on the specifics of the algorithms, how to decide which algorithm to use under which circumstance etc. things start to go sideways.</p> <p>Why is it the case, that so many practicing developers today find themselves lacking in this area? Well I guess, the truth is that with all the excellent tooling and libraries accompanying&#160; most languages and development platforms today, very few people need to actually delve into the depths of the algorithms and data structures they use. Everything is right there, got a list and want to find something in it, just call Find, Search, IndexOf or whatever function is documented to find an item in the list and be done with it. There is definitely nothing wrong with this, these libraries are professionally developed, robust and already used by thousands of developers so they are well QA’d. There is however tremendous value in having a good understanding of the algorithms and data structures you use. </p> <p>If you understand your data, and you know what it is you need to do with that data, the next step is selecting the most appropriate data structure to store your data. Algorithms and data structures are tightly coupled together, often the data structures you use to store your data will determine which algorithms can be used efficiently on the data. While most data structures can be searched, what will vary is how efficiently that search can be performed, depending on the underlying data structure or even the ordering of the data in the structure. Having an understanding of the various data structures and the corresponding algorithms can help you choose the most efficient way to work with your data and ensure your software does not buckle under the pressure of huge data volumes just because you selected the wrong data structure and/or algorithm to manipulate and manage your data. Selecting the right algorithm for the job often it the key to a successful outcome, but to do that you need to understand the pros and cons of what is available to you.</p> <p>With this series of blog posts I hope to share some of my learning's and at the same time gain a deeper level of understanding as we explore the algorithms and data structures together. I invite you to participate in this series, if you know a better way to implement something or have a better approach to explaining a specific algorithm, please share with us and help enrich our journey.</p> <p><strong><font size="4">What are Data Structures?</font></strong></p> <p>Data structures are the containers that you use to store and manage the data for your application.</p> <p>As we design and develop software, one of the decisions we need to make is what data types to use to store the application specific data. If we where developing a simple contact management system, in which we can enter contact information and later retrieve that information, we would need to define how the contacts would be represented internally within the system i.e. the data elements that represent a contact, such as first name, last name, address, email address, mobile number, date of birth etc. as well as the data types used to store each of these elements, how multiple contacts will be maintained and managed within the system, all of which help define the data structures that will be required to build a functional system. </p> <p>However simply looking at the data requirements is not always enough, we also need to look at the algorithms we intend to apply to these data structures, how will we manipulate the data, perform searches, sort the data etc.. As we will discover through this series, the intended algorithms will have a bearing on the data structures we might select to represent the data in the system.</p> <p><strong><font size="4">What is an Algorithm?</font></strong></p> <p>An algorithm is a recipe or set of instructions that can be followed to solve a specific type of problem. </p> <p>Assume we have chosen to store our contacts from earlier in a list in which we can access each contact by walking through the list item by item, like paging through a book. We now need to find an algorithm we can use to search through the list to locate a contact by last name. How would you go about that? Given what we know about the data structure, the obvious solution would be to iterate through the list of contacts comparing the last name element to the search key. If you find a match you can stop the iteration and return the instance of the contact that was found, otherwise if you reach the end of the list and no match was found you return some indication that the contact does not exist. These steps describe what is known as a Sequential Search.</p> <p>There are situations where using the simple Sequential Search algorithm might not be the best option and could severely hurt the performance of your system. For example, if the list in question contained a significant number of items and you need to perform frequent searches to determine the existence of an item in the list, this could quickly become a bottleneck. Every time we search for an item that does not exist, we will be iterating through the entire list just to determine that the item does not exist, in the best case the item we are searching for is found quickly within the first few items of the list, while on other occasions the item might only be found towards the end of the list. We will look at this in a little more depth in the section on Big-O notation.</p> <p>Let’s look at one possible alternative, we could use a Binary Search. This algorithm can be significantly more efficient than a Sequential Search, especially in the worst case scenarios where the item being searched is either not in the list or it exists far from the beginning of the list. However, to be able to use the Binary Search, the collection of items will need to conform to the basic requirements of the Binary Search.</p> <ol> <li>The list of items must be sorted </li> <li>The list structure must support what is often called random access. i.e. we should be able to access item 83 in a list of 100 items without needing to iterate over the first 82 items. </li> </ol> <p>Given the above constraints are met, for the moment we will ignore the cost of ensuring the data is sorted, while not insignificant, for the purposes of the discussion we will choose to ignore it for now, we can use the Binary Search algorithm to introduce some significant optimization. </p> <p>Here is a quick introductory example of the basics of the Binary Search algorithm.</p> <p>First you select the item in the middle of the list, in a list of 100 items that will be item 50. Now compare item 50 (the midpoint item) to the search key, if it is a match we can terminate the search and return item, if the search key is greater than the midpoint item then we know that, <strong>if the item exists</strong>, it must be in the second half of the list. We know this because the list is sorted, therefore if the the search key is greater than the midpoint item, if must be greater than all the items preceding the midpoint item. And visa versa, if the search key is less than the midpoint item, then a potentially matching item would be in the first half of the list. Can you see how with a single comparison we have eliminated half of the items to be searched?</p> <p>Having determined which half of the list the item might be in, you can repeat the same logic on that subset of the data. Having narrowed the list of items down to a subset of 50, you can again select the midpoint item and compare it to the search key, which will either be a match or indicate that the search key potentially exists in the top or bottom half of the subset. After 2 comparisons we have eliminated roughly 75% of the items to be searched. You can continue until you either find the item or run out of items to search which would indicate that the item does not exist in the list.</p> <p>We will cover both the Sequential and Binary Search in more detail later, for now I just want to use this to demonstrate how selecting the right algorithm for the job can make a difference and how the nature of the data might influence the algorithms you can use. And it also leads us into the next topic and that is Big-O notation.</p> <p><strong><font size="4">Big-O notation</font></strong></p> <p>I am sure you have at some point seen or read about Big-O notation even if you did not know what it meant. If you spent anytime reading about algorithms you might have seen something like the following <strong>O(n)</strong>, <strong>O(log n)</strong> or <strong>O(n<sup>3</sup>)</strong>. And if you wondered what it all means, I will try to give a very brief non-mathematical description of how you can make some basic sense of this notation.</p> <p>When selecting an algorithm there are a number of factors that you might have to take into consideration. For desktops or server based applications your primary criteria might be performance, where you want to select the algorithm that is going to give you the best performance regardless of the amount of memory the algorithm requires to be executed. On the other hand, on mobile devices you might be more concerned about the memory requirements of a particular algorithm. In either case, we need some way to represent this these characteristics of an algorithm, this representation needs to be simple enough that just by looking at it I can tell if one algorithm will perform better than another or if it will be more memory efficient without needing to read or understand the complex mathematical analysis of each algorithm. For our purposes we will focus on the performance aspect and discuss the memory aspect later when we work with actual algorithms.</p> <p>Big-O notation gives us a concise notation that captures the performance characteristics of an algorithm over a collection of items. Basically we can see at a glace if the algorithm performance will degrade rapidly, linearly or gradually as the number of items in the collection increases. Of course as we saw earlier algorithms have best case scenarios as well as worst case scenarios, Big-O notation represents the average case, so looking at the Big-O for a Binary Search I can say that on average the Binary Search will out perform a Sequential Search. That does not mean it will always out perform the Sequential Search, remember, if the matching item is first in the list the Sequential Search will find it immediately, while the Binary Search will need to perform a few iterations before finding that the first item is the matching item, but on average we would expect that the Binary Search will perform better for real world searches. </p> <p>Using Big-O notation, the Sequential Search would be described as an <strong>O(n)</strong> algorithm, where <strong><em>n</em></strong> represents the number of items the algorithm will be working with. If we searched a list of 10 items then <strong><em>n=10</em></strong> and if we searched a list of 1000 items then <strong><em>n=1000.</em></strong> From this we can conclude that on average the algorithm performs linearly, if we double the number of items the average search time will double, so the relationship between the number of items and the execution time is linear.</p> <p>How does that compare to our Binary Search algorithm, well without getting into the details now, I will tell you that a Binary Search is an<strong> O(log n)</strong> algorithm. This means that as the number of items increase the execution time increases logarithmically. Mathematically <strong>log(n) </strong>&lt; <strong>n</strong> were <strong>n </strong>is a positive integer<strong> </strong>(see the table below), therefore we can say that Binary Search is faster than a Sequential search. </p> <p>Lets look a quick analysis, if the item that I am searching for is the first item in a list of 1,000,000 items then the Sequential Search will clearly out perform the Binary Search which is going to jump to the middle of the list, see that the item is in the first half of the list and half that portion and so on for a total of 20 comparisons before locating the target item at the beginning of the list. However if the item being searched was the last item in the list then the Sequential Search would require 1,000,000 comparisons while the Binary Search worst case would not be more than 20 comparisons. So the worst case of the Binary Search of a collection of sorted items is 20 comparisons while the sequential search will exceed this worst case for when searching of any of the 999 980 that are after the first 20 items in the list.</p> <p>What we have seen here is that the Sequential Search has a best case execution of <strong>O(1)</strong>, that is constant time regardless of the number of items in the list, of course this is the absolute best case when you are lucky enough to have the item you are looking for be the first item in the list. While the worst case if the item is the last item or the item does not exist at all will be O(n) which is also the average case. </p> <p>The Binary Search also has a best case scenario of O(1), that is when the item you are searching for happens to be the item in the middle of the list, in which case the item would be found on the first comparison, but the average case is O(log n).</p> <table border="1" cellspacing="0" cellpadding="2" width="503"><tbody> <tr> <td valign="top" width="80"><strong>n</strong></td> <td valign="top" width="80"><strong>O(1)</strong></td> <td valign="top" width="80"><strong>O(n)</strong></td> <td valign="top" width="76"><strong>O(log n)</strong></td> <td valign="top" width="80"><strong>O(n log n)</strong></td> <td valign="top" width="105"><strong>O(n<sup>2</sup>)</strong></td> </tr> <tr> <td>1</td> <td>1</td> <td>1</td> <td>0</td> <td>0</td> <td>1</td> </tr> <tr> <td>10</td> <td>1</td> <td>10</td> <td>3.32</td> <td>33.22</td> <td>100</td> </tr> <tr> <td>100</td> <td>1</td> <td>100</td> <td>6.64</td> <td>664.39</td> <td>10000</td> </tr> <tr> <td>1000</td> <td>1</td> <td>1000</td> <td>9.97</td> <td>9965.78</td> <td>1000000</td> </tr> <tr> <td>10000</td> <td>1</td> <td>10000</td> <td>13.29</td> <td>132877.12</td> <td>100000000</td> </tr> <tr> <td>100000</td> <td>1</td> <td>100000</td> <td>16.61</td> <td>1660964.05</td> <td>10000000000</td> </tr> <tr> <td>1000000</td> <td>1</td> <td>1000000</td> <td>19.93</td> <td>19931568.57</td> <td>1000000000000</td> </tr> </tbody></table> <p>Looking at the above table you see a comparison of some Big-O representations for various values of <strong>n</strong>. This should give you a feel for how one algorithm would perform relative to another based on the Big-O of the algorithm. The best general purpose sorting algorithms today are <strong>O(n log n)</strong> algorithms.</p> <p>Given the speed of computers today, even the worst performing algorithms will appear to perform efficiently for small values of n. That is why it is very important to understand the volume of data that your system might need to work with and make sure you test with volumes that are representative of what you expect to see in the production environment.</p> <p>When selecting your algorithms, make sure that you fully grasp the context in which the algorithm will be used and how that scope might change overtime as your system hopefully becomes more and more popular.</p> <p>If you would like to see a visual representation of the table above take a look at my <a href="http://taylorza.blogspot.com/2010/12/visualizing-big-o-complexity-functions.html">Big-O Visualizer</a>. Note this application requires Silverlight 4. </p> <hr /> <p><a href="http://taylorza.blogspot.com/2011/09/practical-algorithms-and-data.html">Next: Arrays – Part 1</a></p> <hr /> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/rQSivf0qy1k" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2011/07/practical-algorithms-and-data.htmltag:blogger.com,1999:blog-4927779461366619687.post-88924479853467930412010-12-23T14:12:00.002+02:002011-08-23T20:40:07.285+03:00Visualizing Big-O complexity functionsI quickly threw this little Silverlight application together to help visualize some of the common Big-O complexity functions often quoted in any discusion on Data Structures and Algorithms.<br /><br /><div id="silverlightControlHost" style="height:400px"><object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> <param name="source" value="http://dotnetwarrior.com/Silverlight/BigOVisualizer/BigOVisualizer.xap"/><param name="onError" value="onSilverlightError" /><param name="background" value="white" /><param name="minRuntimeVersion" value="4.0.50826.0" /><param name="maxFrameRate" value="10" /><param name="autoUpgrade" value="true" /><a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50826.0" style="text-decoration:none"> <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/> </a> </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe><br /></div><i><small>This works on Linux Firefox with <a href="http://www.go-mono.com/moonlight/prerelease.aspx">Moonlight 3 Preview Release</a> it is a little slow but it works.</small></i><br /><br />The following is a brief summary of the functions demonstrated in the application. You can get more detail <a href="http://en.wikipedia.org/wiki/Big_O_notation">here</a>.<br /><table border="1"><tr> <th>Function</th><th>Description</th><th>Example</th> </tr>
<tr> <td>O(1)</td> <td>Constant complexity regardless of the domain size.</td> <td><a href="http://en.wikipedia.org/wiki/Array_data_structure">Array direct indexing</a>, <a href="http://en.wikipedia.org/wiki/Hash_table">Hash table</a></td> </tr>
<tr> <td>O(n)</td> <td>Linear complexity. As the domain size increases, the time/complexity increases linearly. If you double the items the complexity will double.</td> <td><a href="http://en.wikipedia.org/wiki/Sequential_search">Sequential search</a></td> </tr>
<tr> <td>O(log n)</td> <td>Logarithmic complexity. As the domain size increases, the time/complexity increases logorithmically</td> <td><a href="http://en.wikipedia.org/wiki/Binary_search_algorithm">Binary Search</a> on sorted data, Lookup in <a href="http://en.wikipedia.org/wiki/Red-black_tree">balanced binary tree</a></td> </tr>
<tr> <td>O(n log n)</td> <td>Log Linear complexity. As the domain size increases, the time/complexity increases at a log linear or geometric rate.</td> <td><a href="http://en.wikipedia.org/wiki/Heapsort">Heap Sort</a>, <a href="http://en.wikipedia.org/wiki/Merge_sort">Merge Sort</a>, <a href="http://en.wikipedia.org/wiki/Quicksort">Quick Sort</a><sup>1</sup></td> </tr>
<tr> <td>O(n&#178;)</td> <td>Quadratic complexity. As the domain size increases, the time/complexity increases quadratically.</td> <td><a href="http://en.wikipedia.org/wiki/Bubble_sort">Bubble Sort</a>, <a href="http://en.wikipedia.org/wiki/Insertion_sort">Insertion sort</a></td> </tr>
<tr> <td>O(n&#179;)</td> <td>Cubic complexity. As the domain size increases, the time/complexity increases at a cubic rate.</td> <td>Naive multiplication of two nxn matrices.</td> </tr>
<tr> <td>O(2&#8319;)</td> <td>Exponential complexity. As the domain size increases, the time/complexity increases exponentialy.</td> <td>Some graph algoritms like finding the exact solution to the <a href="http://en.wikipedia.org/wiki/Traveling_salesman_problem">traveling salesman problem</a>.</td> </tr>
</table><i><sup>1</sup>Quick sort has best and average case of O(n log n) but worst case of O(n&#178;)</i><img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/6MItpFyrU0s" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2010/12/visualizing-big-o-complexity-functions.htmltag:blogger.com,1999:blog-4927779461366619687.post-4561608391852158312010-09-25T01:08:00.001+03:002010-09-25T01:12:15.174+03:00Code Share: Finding controls by control type.<p>On stackoverflow.com the question was asked, how to <a href="http://stackoverflow.com/questions/3788399/find-contentplaceholders-in-master-page/3788493#3788493">‘Find ContentPlaceHolders in Master Page’</a>.&#160; </p> <p>In this case the OP wanted to work with a Master Page other than the one that was already The first part of the problem was getting the Master Page loaded in memory so that the Control tree could be interrogated. </p> <p>Fortunately loading the Master Page is quite simple, you can use <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.templatecontrol.aspx">LoadControl</a> to load the Master Page just like you would load any other user control.</p> <p>For example in the Page_Load handler you could use something like the following to load the Master Page.</p> <div id="codeSnippetWrapper"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">var site1Master = LoadControl(<span style="color: #006080">&quot;Site1.Master&quot;</span>);</pre></div><br /><p>The next part, finding all the controls of a specific type, requires a simple recursive routine to search the control tree for all the controls of the type that you are interested in. Here is a simple implementation of just such a routine.</p><br /><div id="codeSnippetWrapper"><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> WebHelper<br />{<br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> IList&lt;T&gt; FindControlsByType&lt;T&gt;(Control root) <br /> <span style="color: #0000ff">where</span> T : Control<br /> {<br /> <span style="color: #0000ff">if</span> (root == <span style="color: #0000ff">null</span>) <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ArgumentNullException(<span style="color: #006080">&quot;root&quot;</span>);<br /><br /> List&lt;T&gt; controls = <span style="color: #0000ff">new</span> List&lt;T&gt;();<br /> FindControlsByType&lt;T&gt;(root, controls);<br /> <span style="color: #0000ff">return</span> controls;<br /> }<br /><br /> <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> FindControlsByType&lt;T&gt;(Control root, IList&lt;T&gt; controls)<br /> <span style="color: #0000ff">where</span> T : Control<br /> {<br /> <span style="color: #0000ff">foreach</span> (Control control <span style="color: #0000ff">in</span> root.Controls)<br /> {<br /> <span style="color: #0000ff">if</span> (control <span style="color: #0000ff">is</span> T)<br /> {<br /> controls.Add(control <span style="color: #0000ff">as</span> T);<br /> }<br /> <span style="color: #0000ff">if</span> (control.Controls.Count &gt; 0)<br /> {<br /> FindControlsByType&lt;T&gt;(control, controls);<br /> }<br /> }<br /> }<br />}</pre></div><br /><p>Using the tow pieces of code above, finding all the ContentPlaceHolders on the Master Page can be done like this.</p><br /><div id="codeSnippetWrapper"><br /> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">// Load the Master Page</span><br />var site1Master = LoadControl(<span style="color: #006080">&quot;Site1.Master&quot;</span>);<br /><br /><span style="color: #008000">// Find the list of ContentPlaceHolder controls</span><br />var controls = WebHelper.FindControlsByType&lt;ContentPlaceHolder&gt;(site1Master);<br /><br /><span style="color: #008000">// Do something with each control that was found</span><br /><span style="color: #0000ff">foreach</span> (var control <span style="color: #0000ff">in</span> controls)<br />{<br /> Response.Write(control.ClientID);<br /> Response.Write(<span style="color: #006080">&quot;&lt;br /&gt;&quot;</span>);<br />}</pre><br /></div><br /><p>Hope someone finds this useful. I thought I would share it since I took the few moments to write it and did not want it to go to waste.</p> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/XUYbOTNRYbU" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2010/09/code-share-finding-controls-by-control.htmltag:blogger.com,1999:blog-4927779461366619687.post-69022203275168197812010-06-27T18:25:00.001+03:002010-06-27T18:25:42.842+03:00Crossing the process boundary with .NET<h4>&#160;</h4> <p>Every so often my post <a>Hacking my way across the process boundary</a> gets some attention. Mostly in the form of requests for a .NET version of this technique. Now out of laziness more than anything else I have not actually taken the time or effort to do the conversion until now. So for those that need to access ListView or TreeView data from another process here is a simple example of one possible way to do it. Since I used C# for the example, I could very well have used unsafe code blocks to do some of the work, however I decided to avoid this making this example applicable to VB.NET developers as well. If you would like to see a version using unsafe code blocks, drop me a note and I will get round to it. To keep the sample short I have removed anything but the most rudimentary error checking. For an explanation of this code, please refer to the original post sighted above.</p> <p>&#160;</p> <div id="codeSnippetWrapper"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">using</span> System;<br /><span style="color: #0000ff">using</span> System.Runtime.InteropServices;<br /><span style="color: #0000ff">using</span> System.Text;<br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> CrossProcessMemory<br />{<br /> <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> LVM_GETITEM = 0x1005;<br /> <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> LVM_SETITEM = 0x1006;<br /> <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> LVIF_TEXT = 0x0001;<br /> <span style="color: #0000ff">const</span> <span style="color: #0000ff">uint</span> PROCESS_ALL_ACCESS = (<span style="color: #0000ff">uint</span>)(0x000F0000L | 0x00100000L | 0xFFF);<br /> <span style="color: #0000ff">const</span> <span style="color: #0000ff">uint</span> MEM_COMMIT = 0x1000;<br /> <span style="color: #0000ff">const</span> <span style="color: #0000ff">uint</span> MEM_RELEASE = 0x8000;<br /> <span style="color: #0000ff">const</span> <span style="color: #0000ff">uint</span> PAGE_READWRITE = 0x04;<br /><br /> [DllImport(<span style="color: #006080">&quot;user32.dll&quot;</span>)]<br /> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> <span style="color: #0000ff">bool</span> SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam, IntPtr lParam);<br /> <br /> [DllImport(<span style="color: #006080">&quot;user32&quot;</span>)]<br /> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> IntPtr GetWindowThreadProcessId( IntPtr hWnd, <span style="color: #0000ff">out</span> <span style="color: #0000ff">int</span> lpwdProcessID ); <br /> <br /> [DllImport(<span style="color: #006080">&quot;kernel32&quot;</span>)]<br /> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> IntPtr OpenProcess(<span style="color: #0000ff">uint</span> dwDesiredAccess, <span style="color: #0000ff">bool</span> bInheritHandle, <br /> <span style="color: #0000ff">int</span> dwProcessId);<br /><br /> [DllImport(<span style="color: #006080">&quot;kernel32&quot;</span>)]<br /> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> IntPtr VirtualAllocEx( IntPtr hProcess, IntPtr lpAddress, <br /> <span style="color: #0000ff">int</span> dwSize, <span style="color: #0000ff">uint</span> flAllocationType, <span style="color: #0000ff">uint</span> flProtect);<br /><br /> [DllImport(<span style="color: #006080">&quot;kernel32&quot;</span>)]<br /> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> <span style="color: #0000ff">bool</span> VirtualFreeEx( IntPtr hProcess, IntPtr lpAddress, <span style="color: #0000ff">int</span> dwSize, <br /> <span style="color: #0000ff">uint</span> dwFreeType );<br /><br /> [DllImport(<span style="color: #006080">&quot;kernel32&quot;</span>)]<br /> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> <span style="color: #0000ff">bool</span> WriteProcessMemory( IntPtr hProcess, IntPtr lpBaseAddress, <br /> <span style="color: #0000ff">ref</span> LV_ITEM buffer, <span style="color: #0000ff">int</span> dwSize, IntPtr lpNumberOfBytesWritten );<br /><br /> [DllImport(<span style="color: #006080">&quot;kernel32&quot;</span>)]<br /> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> <span style="color: #0000ff">bool</span> ReadProcessMemory( IntPtr hProcess, IntPtr lpBaseAddress, <br /> IntPtr lpBuffer, <span style="color: #0000ff">int</span> dwSize, IntPtr lpNumberOfBytesRead );<br /><br /> [DllImport(<span style="color: #006080">&quot;kernel32&quot;</span>)]<br /> <span style="color: #0000ff">static</span> <span style="color: #0000ff">extern</span> <span style="color: #0000ff">bool</span> CloseHandle( IntPtr hObject );<br /><br /> [StructLayout(LayoutKind.Sequential)]<br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">struct</span> LV_ITEM<br /> {<br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">uint</span> mask; <br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> iItem; <br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> iSubItem; <br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">uint</span> state; <br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">uint</span> stateMask; <br /> <span style="color: #0000ff">public</span> IntPtr pszText; <br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> cchTextMax; <br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> iImage;<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> ReadListViewItem( IntPtr hWnd, <span style="color: #0000ff">int</span> item )<br /> {<br /> <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> dwBufferSize = 1024;<br /> <br /> <span style="color: #0000ff">int</span> dwProcessID;<br /> LV_ITEM lvItem; <br /> <span style="color: #0000ff">string</span> retval;<br /> <span style="color: #0000ff">bool</span> bSuccess;<br /> IntPtr hProcess = IntPtr.Zero;<br /> IntPtr lpRemoteBuffer = IntPtr.Zero;<br /> IntPtr lpLocalBuffer = IntPtr.Zero;<br /> IntPtr threadId = IntPtr.Zero;<br /> <br /> <span style="color: #0000ff">try</span><br /> {<br /> lvItem = <span style="color: #0000ff">new</span> LV_ITEM();<br /> lpLocalBuffer = Marshal.AllocHGlobal(dwBufferSize);<br /> <span style="color: #008000">// Get the process id owning the window</span><br /> threadId = GetWindowThreadProcessId( hWnd, <span style="color: #0000ff">out</span> dwProcessID );<br /> <span style="color: #0000ff">if</span> ( (threadId == IntPtr.Zero) || (dwProcessID == 0) )<br /> <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ArgumentException( <span style="color: #006080">&quot;hWnd&quot;</span> );<br /><br /> <span style="color: #008000">// Open the process with all access</span><br /> hProcess = OpenProcess( PROCESS_ALL_ACCESS, <span style="color: #0000ff">false</span>, dwProcessID );<br /> <span style="color: #0000ff">if</span> ( hProcess == IntPtr.Zero )<br /> <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException( <span style="color: #006080">&quot;Failed to access process&quot;</span> );<br /><br /> <span style="color: #008000">// Allocate a buffer in the remote process</span><br /> lpRemoteBuffer = VirtualAllocEx( hProcess, IntPtr.Zero, dwBufferSize, MEM_COMMIT, <br /> PAGE_READWRITE );<br /> <span style="color: #0000ff">if</span> ( lpRemoteBuffer == IntPtr.Zero )<br /> <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> SystemException( <span style="color: #006080">&quot;Failed to allocate memory in remote process&quot;</span> );<br /> <br /> <span style="color: #008000">// Fill in the LVITEM struct, this is in your own process</span><br /> <span style="color: #008000">// Set the pszText member to somewhere in the remote buffer,</span><br /> <span style="color: #008000">// For the example I used the address imediately following the LVITEM stuct</span><br /> lvItem.mask = LVIF_TEXT;<br /> lvItem.iItem = item;<br /> lvItem.pszText = (IntPtr)(lpRemoteBuffer.ToInt32() + Marshal.SizeOf(<span style="color: #0000ff">typeof</span>(LV_ITEM)));<br /> lvItem.cchTextMax = 50;<br /><br /> <span style="color: #008000">// Copy the local LVITEM to the remote buffer</span><br /> bSuccess = WriteProcessMemory( hProcess, lpRemoteBuffer, <span style="color: #0000ff">ref</span> lvItem, <br /> Marshal.SizeOf(<span style="color: #0000ff">typeof</span>(LV_ITEM)), IntPtr.Zero );<br /> <span style="color: #0000ff">if</span> ( !bSuccess )<br /> <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> SystemException( <span style="color: #006080">&quot;Failed to write to process memory&quot;</span> );<br /><br /> <span style="color: #008000">// Send the message to the remote window with the address of the remote buffer</span><br /> SendMessage( hWnd, LVM_GETITEM, 0, lpRemoteBuffer);<br /> <br /> <span style="color: #008000">// Read the struct back from the remote process into local buffer</span><br /> bSuccess = ReadProcessMemory( hProcess, lpRemoteBuffer, lpLocalBuffer, dwBufferSize, <br /> IntPtr.Zero );<br /> <span style="color: #0000ff">if</span> ( !bSuccess )<br /> <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> SystemException( <span style="color: #006080">&quot;Failed to read from process memory&quot;</span> );<br /> <br /> <span style="color: #008000">// At this point the lpLocalBuffer contains the returned LV_ITEM structure</span><br /> <span style="color: #008000">// the next line extracts the text from the buffer into a managed string</span><br /> retval = Marshal.PtrToStringAnsi((IntPtr)(lpLocalBuffer.ToInt32() + <br /> Marshal.SizeOf(<span style="color: #0000ff">typeof</span>(LV_ITEM))));<br /> }<br /> <span style="color: #0000ff">finally</span><br /> { <br /> <span style="color: #0000ff">if</span> ( lpLocalBuffer != IntPtr.Zero )<br /> Marshal.FreeHGlobal( lpLocalBuffer );<br /> <span style="color: #0000ff">if</span> ( lpRemoteBuffer != IntPtr.Zero )<br /> VirtualFreeEx( hProcess, lpRemoteBuffer, 0, MEM_RELEASE ); <br /> <span style="color: #0000ff">if</span> ( hProcess != IntPtr.Zero )<br /> CloseHandle( hProcess );<br /> }<br /> <span style="color: #0000ff">return</span> retval;<br /> }<br />}<br /></pre><br /><br /> <br /></div> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/eyWwL-cXd3U" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com15http://taylorza.blogspot.com/2010/06/crossing-process-boundary-with-net.htmltag:blogger.com,1999:blog-4927779461366619687.post-70828944424112062702010-06-06T10:57:00.001+03:002010-06-06T11:08:46.589+03:00Visual Studio 2010 – Box Selection<p>This has been blogged about and promoted everywhere and it is true, it is a super cool feature that even the pre-.NET IDE supported, but VS2010 breaths new life into the feature with some great enhancements. For a very cool intro to the functionality take a look <a href="http://www.youtube.com/watch?v=qp2WuJ2rEE4">here</a> where a member of the IDE team demonstrates the enhancements.</p> <p>So, why on earth would I be blogging about “old” news. Well one thing that I found “missing” with this feature until now was the ability to use the keyboard to perform a block selection. Now with VS 2010 you can use <strong>Shift</strong>+<strong>Alt</strong>+[<strong>Up</strong>, <strong>Down</strong>, <strong>Left</strong>, <strong>Right</strong>] to perform a block selection. This is a great enhancement, and worth knowing about if you are a keyboard junkie.</p> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/FNo7FZweVas" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2010/06/visual-studio-2010-box-selection.htmltag:blogger.com,1999:blog-4927779461366619687.post-9093886961175245412010-04-17T14:37:00.002+03:002010-04-17T14:48:38.527+03:00Archive: Binary data from a Structure<p>For this post, I will be using the binary structure used by the trusty old DBF file structure.</p><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode">[StructLayout(LayoutKind.Sequential, Pack = 1)]<br /><span class="kwrd">struct</span> DBFHeader<br />{<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> Tag;<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> Year;<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> Month;<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> Day; <br /> <span class="kwrd">public</span> <span class="kwrd">int</span> RecCount;<br /> <span class="kwrd">public</span> <span class="kwrd">short</span> HeaderSize;<br /> <span class="kwrd">public</span> <span class="kwrd">short</span> RecSize;<br /> <span class="kwrd">public</span> <span class="kwrd">short</span> Reserved1;<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> Trans;<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> Encrypt;<br /> <span class="kwrd">public</span> <span class="kwrd">long</span> Reserved2_1;<br /> <span class="kwrd">public</span> <span class="kwrd">short</span> Reserved2_2;<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> MDX;<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> LangId;<br /> <span class="kwrd">public</span> <span class="kwrd">short</span> Reserved3;<br />}</pre></div><p>Given the above structure, our challenge now is to transfer it from its in memory binary format to a stream. The stream may be a physical file, memory stream even a network stream. The most important aspect of all the techniques that I will investigate here is that the resulting binary data format is consistent with the format expected by the reader.</p><p>After looking at the interface provided by the Stream class, from which all streams inherit, I came to the conclusion that I would have to convert the structure to a byte array (<b>byte[]</b>). </p><p>Again our first stop is the interop services provided by the .NET framework. The following piece of code will convert a structure to a <b>byte[]</b> that can then be written to a stream.</p><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode">DBFHeader hdr = <span class="kwrd">new</span> DBFHeader(); <br /><span class="kwrd">byte</span>[] data = <span class="kwrd">new</span> <span class="kwrd">byte</span>[Marshal.SizeOf(hdr)];<br /><span class="kwrd">unsafe</span><br />{<br /> DBFHeader *p = &amp;hdr;<br /> Marshal.Copy( (IntPtr)p, data, 0, data.Length ); <br />} </pre></div><p>This code requires that you compile with unsafe code allowed. This can be done either by supplying the <b>/unsafe</b> switch to the command line compiler or in the Visual Studio .NET project properties under Configuration Properties&gt;Build, you can set the <b>Allow Unsafe Code Blocks</b> to True.</p><p>After creating a instance of the structure <b>DBFHeader</b> a <b>byte[]</b> is created large enough to hold the contents of the structure. At that point we need to transfer the contents of the <b>DBFHeader</b> instance to the <b>byte[]</b>. To accomplish this, I declared an unsafe block and assigned the address of the structure, being a <b>ValueType </b>the structure is allocated on the local stack and is therefore implicitly <b>pinned</b>. Then the <b>Marshal.Copy </b>method is used to copy the data from an address in memory to the <b>byte[]</b>. Now we are free to write the byte array to a stream using the <b>Write</b> method provided by the stream. </p><p>There are a number of things that count against this technique. Using unsafe code blocks means that the assembly can not be verified and the caller would require the <b>SkipVerification</b> permission. The use of the <b>Marshal </b>methods requires that the immediate caller has <b>SecurityPermissionAttribute.UnmanagedCode</b>. This technique also requires that the data be moved in two stages, from the structure to the <b>byte[]</b> an then from there to the <b>stream</b>, this could be costly for large structures.</p><p>The first problem of the code being non-verifiable can be overcome by using a technique very similar to the above technique, without the requirement for unsafe code blocks. The following code demonstrates this technique. </p><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode">DBFHeader hdr = <span class="kwrd">new</span> DBFHeader(); <br /><span class="kwrd">byte</span>[] data = <span class="kwrd">new</span> <span class="kwrd">byte</span>[Marshal.SizeOf(hdr)];<br />IntPtr p = Marshal.AllocHGlobal( Marshal.SizeOf(hdr) );<br />Marshal.StructureToPtr( hdr, p, <span class="kwrd">false</span> );<br />Marshal.Copy( (IntPtr)p, data, 0, data.Length ); <br />Marshal.FreeHGlobal( p );</pre></div><p>Notice that it still requires that the data be moved in two stages and the use of the Marshal class carries the same security requirements as earlier. The only benefit is that the resulting assembly remains verifiable, not requiring the <b>/unsafe </b>option. However, allocating memory from the unmanaged heap carries with it additional performance overheads.</p><p>And finally the pure managed code solution that while not as easy as the prior techniques, it does benefit from the fact that the code is easily ported to other .NET languages and does not have the same security restrictions as the other two techniques. The solution is to use the <b>BinaryWriter </b>class to write each bit of information from the structure to the stream. </p><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode"><span class="kwrd">using</span> (BinaryWriter wr = <span class="kwrd">new</span> BinaryWriter( stm ))<br />{<br /> wr.Write( hdr.Tag );<br /> wr.Write( hdr.Year );<br /> wr.Write( hdr.Month );<br /> wr.Write( hdr.Day );<br /> wr.Write( hdr.RecCount );<br /> wr.Write( hdr.HeaderSize );<br /> wr.Write( hdr.RecSize );<br /> wr.Write( hdr.Reserved1 );<br /> wr.Write( hdr.Trans );<br /> wr.Write( hdr.Encrypt );<br /> wr.Write( hdr.Reserved2_1 );<br /> wr.Write( hdr.Reserved2_2 );<br /> wr.Write( hdr.MDX );<br /> wr.Write( hdr.LangId );<br /> wr.Write( hdr.Reserved3 ); <br />}</pre></div><p>This code first creates a <b>BinaryWriter</b> that provides binary methods to access the underlying stream. In this case the underlying stream is designated by the <b>stm </b>argument passed to the <b>BinaryWriter</b> constructor. Then using the binary writer, each member of the structure is written in order to the stream through the <b>BinaryWriter </b>instance. I have already mentioned the clear advantages of this technique, but it does carry with it a share of disadvantages. The first and most obvious is that it requires you to write out each member individually, and with larger structures, this can be a tedious and error prone task. This brings me to the second disadvantage, if the data is not written out in the exact order expected by the reader, the reader will either fail to read the structure or even worse, corrupt data.</p><p>Just as a closing note, the final technique could also be used to get a <b>byte[]</b> by writing to a <b>MemoryStream</b> and then calling the <b>GetBuffer</b> method provided by <b>MemoryStream</b> to access the underlying <b>byte[].</b> </p><p></p><b></b><br /><p>Conclusion</p><p>As with every design, it is a series of compromises that determines the ultimate chooses we make. The above techniques provide a number of alternative ranging from simplicity of code to flexibility of implementation. I believe that the final option is most likely the more purist and from a reusability standpoint the more effective solution. </p><img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/U64mO6yVfD8" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2010/04/archive-binary-data-from-structure.htmltag:blogger.com,1999:blog-4927779461366619687.post-29293606077435420592010-04-17T14:06:00.005+03:002010-04-17T14:45:48.434+03:00Archive: Structure from Binary Data<p>Original Post Date: 8 Jan 2004</p><p>The question of moving stream data into a structure seems to be addressed every so often on the newsgroups. While I was building a front-end for a small packet sniffing application I was faced with these same questions. While I was aware of the standard approaches I decided to investigate what the alternative options where. This article is a summary of the primary ways I looked at of mapping binary data to a structure.</p><h3>Defining the structure</h3><p>The first thing that I did was to define the structure, for this example I am going to use the IP header. Deciding on the correct definition of the structure is dependent on the solution or approach taken to solve this problem.</p><p>Given that I have spent a considerable amount of time familiarizing myself with the intricacies of the P/Invoke or interoperability services, my first solution was to use <b>System.Runtime.InteropServices</b> to marshal the data into a structure. I promptly defined the following structure to handle the marshaled data.</p><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode">[StructLayout(LayoutKind.Sequential, Pack = 1)]<br /><span class="kwrd">struct</span> IpHeader<br />{<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> VerLen;<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> TOS;<br /> <span class="kwrd">public</span> <span class="kwrd">short</span> TotalLength; <br /> <span class="kwrd">public</span> <span class="kwrd">short</span> ID;<br /> <span class="kwrd">public</span> <span class="kwrd">short</span> Offset;<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> TTL;<br /> <span class="kwrd">public</span> <span class="kwrd">byte</span> Protocol;<br /> <span class="kwrd">public</span> <span class="kwrd">short</span> Checksum;<br /> <span class="kwrd">public</span> <span class="kwrd">int</span> SrcAddr;<br /> <span class="kwrd">public</span> <span class="kwrd">int</span> DestAddr;<br />}<br /></pre></div><p>The two things to note about the definition of the structure is that it is defined as a sequential structure which ensures that the order of the members and relative location of these members are not altered by the CLR. </p><p>Now that the structure is defined the next step is to populate the structure with data. In this case the data came from a call to the <b>Receive</b> method of the <b>Socket</b> class. To read IP header the socket was created using <b>SocketType.Raw</b>. The <b>Receive</b> method fills a byte array with the data and it is the first 20 bytes of this data that makes up the IP header. Because the structure that I have defined, and the structure of the data in these 20 bytes are a one to one match, using a language like C++ reading this data would be a relatively simple task requiring only a cast.</p><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode">IpHeader *pHeader = (IpHeader*)packet;</pre></div><p>With the structure of the <b>IpHeader</b> overlayed on the memory stream it is a matter of accessing the members of the structure. Unfortunately our structure is stored in managed memory and using code like the above in C# would require entering an unsafe code block. The following piece of code demonstrates how this could be achieved in C#.</p><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode">IpHeader iphdr;<br /><span class="kwrd">unsafe</span><br />{<br /> <span class="kwrd">fixed</span> ( <span class="kwrd">byte</span> *pData = packet)<br /> {<br /> iphdr = *(IpHeader*)pData;<br /> }<br />}<br /><span class="rem">//Use iphdr...</span><br /></pre>For the above code to compile successfully you will have to flip the switch to allow unsafe code to be compiled. This can be achieved using either the <b>/unsafe</b> switch for the command line compiler or in the Visual Studio .NET project properties under <b>Configuration Properties&gt;Build</b> you can set the <b>Allow Unsafe Code Blocks</b> to <b>true</b>.</div><p>The code in the unsafe block uses the <b>fixed</b> keyword to pin the packet structure in memory ensuring that the garbage collector does not shift the memory around under our feet. Because the memory is fixed it can now be used much like a standard C/C++ pointer allowing us to cast the block of memory to an <b>IpHeader*</b> and assign the pointer to <b>iphdr</b> structure. </p><p>Though this is a relatively simple piece of code there are a few points to be noted about this solution. First whenever we allow unsafe code blocks in a C# application we inhibit the CLR’s ability to verify the code. In this case, by fixing the data in memory and accessing it like a pointer the CLR can no longer check the array bounds and our code is open to a buffer overrun. The second point of concern with the fixed memory is that the garbage collector cannot shift this memory impacting the garbage collection process and the performance of the garbage collector if a collection is required. And finally this solution, as far as I know, can not be done in VB.NET, so it would have to be written in C# and the assembly referenced from VB.NET.</p><p>That brings me to the solution of using the marshaling provided by interop services. The clear advantage of this solution is that it can be applied in both C# and VB.NET and it does not require the use of unsafe code blocks therefore the code remains verifiable. Before I discuss this solution any further let me present a code snippet that demonstrates the solution. </p><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode">IntPtr pIP = Marshal.AllocHGlobal( len );<br />Marshal.Copy( packet, 0, pIP, len );<br />iphdr = (IpHeader)Marshal.PtrToStructure( pIP, <span class="kwrd">typeof</span>(IpHeader) );<br />Marshal.FreeHGlobal( pIP );<br /></pre>Now the moment I wrote this piece of code it felt like a knife stabbing into my back. The first thing that happens is that a block of memory is allocated on the unmanaged heap. Since the memory is allocated from the unmanaged heap it does not impact the garbage collector in anyway. The problem is that the packet is stored in the managed heap so we need to first copy the data from the buffer in the managed heap to the buffer in the unmanaged heap using <b>Marshal.Copy</b>. Once the data resides in the unmanaged heap we can use the <b>Marshal.PtrToStructure</b> to marshal the data back to the managed heap in the form of our <b>IpHeader</b> structure. And finally we must free the unmanaged memory that we allocated.</div><p>This really gave me a sour taste in my mouth. Every packet that I picked up needs to be shifted from the managed heap to the unmanaged heap and then be marshaled back to managed heap. Are there words to describe this? Clearly I was not satisfied with this solution.</p><p>The options investigated so far have required somehow side stepping the way the CLR normally would go about its business. Introducing risks such as buffer overruns or even memory leaks if we fail to free the unmanaged memory we have allocated.</p><p>So is there a completely managed solution to the problem? Well yes there is and at first this might seem like it is really the long way around in comparison to the options we have explored so far. However after I present the code I will demonstrate why in this particular case it was actually less code than the previous solutions. This solution makes use of the <b>BinaryReader</b> to read the data directly into the structures members.</p><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode">System.IO.MemoryStream stm = <span class="kwrd">new</span> System.IO.MemoryStream( packet, 0, len );<br />System.IO.BinaryReader rdr = <span class="kwrd">new</span> System.IO.BinaryReader( stm );<br />iphdr.VerLen = rdr.ReadByte();<br />iphdr.TOS = rdr.ReadByte();<br />iphdr.TotalLength = rdr.ReadInt16();<br />iphdr.ID = rdr.ReadInt16();<br />iphdr.Offset = rdr.ReadInt16();<br />iphdr.TTL = rdr.ReadByte();<br />iphdr.Protocol = rdr.ReadByte();<br />iphdr.Checksum = rdr.ReadInt16();<br />iphdr.SrcAddr = rdr.ReadInt32();<br />iphdr.DestAddr = rdr.ReadInt32();<br /></pre></div><p>This solution has it’s own set of advantages and disadvantages. Firstly the structure definition can loose the StructLayoutAttribute since the physical layout of the structure is no longer important. The primary advantage in my opinion of this code is that we are working with code that is 100% managed and there are no cute tricks that could end up tripping us up, although there are other aspects to this solution that could. Another advantage is particular to the case of reading network data, which I will address shortly.</p><p>The code creates a <b>MemoryStream</b> on the existing packet buffer and a <b>BinaryReader</b> is created to read the data from the <b>MemoryStream</b>. Then byte for byte (and now and then a <b>int</b> or <b>short</b>) the data is read into an instance of the structure. </p><p>As I said earlier, the next advantage is specific to the requirements of this particular case. As you know when reading data from a socket the data is in network byte order or big-endian form. This byte ordering is incompatible with Intel processors, which expect multi-byte numeric data types to be represented in little-endian form. Since the packet’s data is in big-endian we are required to convert each <b>Int16</b> and <b>Int32</b> (<b>short</b> and <b>int</b>) to little-endian form. I use <b>IPAddress.NetworkToHostOrder</b> to do the byte swapping for me, since this is required regardless of which of the above solutions where chosen, this would mean calling the function on each non-byte member of the struct. Using the last solution we investigated this could be accomplished in one step while filling the structure.</p><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode">iphdr.VerLen = rdr.ReadByte();<br />iphdr.TOS = rdr.ReadByte();<br />iphdr.TotalLength = IPAddress.NetworkToHostOrder(rdr.ReadInt16());<br />iphdr.ID = IPAddress.NetworkToHostOrder(rdr.ReadInt16());<br />iphdr.Offset = IPAddress.NetworkToHostOrder(rdr.ReadInt16());<br />iphdr.TTL = rdr.ReadByte();<br />iphdr.Protocol = rdr.ReadByte();<br />iphdr.Checksum = IPAddress.NetworkToHostOrder(rdr.ReadInt16());<br />iphdr.SrcAddr = IPAddress.NetworkToHostOrder(rdr.ReadInt32());<br />iphdr.DestAddr = IPAddress.NetworkToHostOrder(rdr.ReadInt32());<br /></pre></div><p>The most obvious concern using this solution is the possibility of misaligned reads, e.g. reading a <b>Byte</b> where you should have read an <b>Int</b>. One mistake of this kind and all the data after that point are misaligned and make no sense. This solution also requires that you maintain both the structure and the code that populates the structure, if the structure changes in someway the code to read the structure needs to be updated. And even with medium sized structures you end up doing a lot of byte counting and checking that every read is performed in the correct order, which is very error prone.</p><p>I have found that including a constructor and/or a static method provides an elegant means of hiding the actual method used to perform the de-serialization. The following pieces of code demonstrate the two possible ways of creating a de-serialized packet using either a static method or constructor.</p><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode">IpHeader iphdr = IpHeader.FromPacket( packet, len );</pre>Or</div><div id="codeSnippetWrapper"><pre id="codeSnippet" class="csharpcode">IpHeader iphdr = <span class="kwrd">new</span> IpHeader( packet, len );</pre>Typically I implement both, and the static method just creates an instance of the structure using the appropriate constructor.</div><h3>Conclusion</h3><p>Though the above solutions are not the only solutions, most other solutions are some form of one of these. Alternatively you could use managed C++ and get the best of both worlds and the expense of verifiable code. The presented solutions are applicable whenever you want to read binary data into a structured form; regardless of the source of the data, be it a network byte stream, or data from a disk file. </p><p>Selecting an appropriate solution is dependent on your requirements and constraints; personally I tend towards using unsafe code blocks especially if the structures are large (call me lazy) or the final solution of reading the data byte for byte. I just can’t feel good about moving perfectly good memory all over the show.</p><img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/-WGFYc5IrlQ" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2010/04/archive-structure-from-binary-data.htmltag:blogger.com,1999:blog-4927779461366619687.post-35403214629583064242009-11-07T17:32:00.001+02:002009-11-07T17:50:49.612+02:00Archive – Debugging WindowsIdentity and IsInRole<p>This is one of those invaluable little utility functions, you never need it until you face a problem trying to determine why IsInRole is returning and unexpected value when you run your application in a non development production environment. Using this function you can quickly determine the list of roles or groups that IsInRole is matching against.</p> <strong>Framework 2.0 and Later</strong> <div id="codeSnippetWrapper" class="csharpcode-wrapper"> <pre id="codeSnippet" class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">string</span>[] GetWindowsIdentityRoles(WindowsIdentity identity)<br />{<br /> <span class="kwrd">if</span> (identity == <span class="kwrd">null</span>) <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentNullException(<span class="str">&quot;identity&quot;</span>);<br /><br /> IdentityReferenceCollection groups = identity.Groups.Translate(<span class="kwrd">typeof</span>(NTAccount));<br /> <span class="kwrd">string</span>[] roles = <span class="kwrd">new</span> <span class="kwrd">string</span>[groups.Count]; <br /> <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i &lt; groups.Count; ++i)<br /> {<br /> roles[i] = groups[i].Value; <br /> }<br /><br /> <span class="kwrd">return</span> roles;<br />}<br /></pre><br /></div><br /><strong>Framework 1.0/1.1 (For that legacy code)</strong> <br /><br /><div id="codeSnippetWrapper" class="csharpcode-wrapper"><br /> <pre id="codeSnippet" class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span>[] GetWindowsIdentityRoles( WindowsIdentity identity )<br />{<br /> <span class="kwrd">object</span> result = <span class="kwrd">typeof</span>(WindowsIdentity).InvokeMember( <span class="str">&quot;_GetRoles&quot;</span>,<br /> BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.NonPublic,<br /> <span class="kwrd">null</span>, identity, <span class="kwrd">new</span> <span class="kwrd">object</span>[]{identity.Token}, <span class="kwrd">null</span> );<br /><br /> <span class="kwrd">return</span> (<span class="kwrd">string</span>[])result; <br />}</pre><br /><br /> <br /></div> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/eNgGJeLDq6c" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2009/11/archive-debugging-windowsidentity-and.htmltag:blogger.com,1999:blog-4927779461366619687.post-29979764914251375982009-11-01T15:21:00.002+02:002009-11-15T21:12:57.658+02:00Archive - Thread Local Storage in .NET<span style="color: blue;">Another repost from my original blog.</span><br />Those of you familiar with the intricacies of Win32 multi-threaded programming will be intimately familiar with thread local storage (TLS). And you might have wondered how .NET exposes this often useful construct.<br />Before we begin I would like to offer a brief description of what TLS is, for those of you that might not have had the fortune of learning Win32 multi-threaded programming. TLS is a means of storing data on a per-thread basis. Any methods accessing the data will access the data associated with the thread in which the method is running. And this is achieved without the method having any special knowledge of the thread in which it is executing. <br />Now the real question, how can we achieve the same results in the .NET managed environment? Well depending on your situation there are a number of options available. I will describe only 3 options here. First I will describe the more commonly known CallContext and then what in my experience is less widely known Thread Static variables and finally there is the thread data slots.<br /><b></b><br />The CallContext <br />Most of you are probably familiar with the CallContext class and how it relates to .NET remoting. A CallContext is used to flow data across multiple calls with out having to pass the data along as some part of the argument list of each called method. And if the stored data implements the ILogicalThreadAffinative interface the data will flow across machine boundaries.<br />A call context is maintained by the .NET framework for each logical thread of execution. By storing data in the call context you are essentially storing data specific to the thread currently executing the method, and any method called in that thread of execution has access to the threads call context. As a simple example, if you wanted to maintain a count of the number of times a function was called per thread you could do some thing like the following.<br /><div class="csharpcode-wrapper"><pre class="csharpcode" id="codeSnippet"><span class="kwrd">class</span> Class1<br />{<br /><span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)<br />{<br />System.Threading.Thread t1 = <span class="kwrd">new</span> System.Threading.Thread(<span class="kwrd">new</span> System.Threading.ThreadStart(ThreadFunction));<br />System.Threading.Thread t2 = <span class="kwrd">new</span> System.Threading.Thread(<span class="kwrd">new</span> System.Threading.ThreadStart(ThreadFunction));<br /><br /><span class="rem">// Start the threads</span><br />t1.Start();<br />t2.Start();<br /><br /><span class="rem">// Wait for the threads to complete</span><br />t1.Join();<br />t2.Join();<br /><br />System.Console.Read();<br />} <br /><br /><span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">string</span> PerThreadCallCounterKey = <span class="str">"CallCounter"</span>;<br /><span class="kwrd">private</span> <span class="kwrd">static</span> Random Rnd = <span class="kwrd">new</span> Random();<br /><br /><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> ThreadFunction()<br />{<br /><span class="rem">// Initialize the per-thread CallCounter</span><br />System.Runtime.Remoting.Messaging.CallContext.SetData(PerThreadCallCounterKey, 0);<br /><span class="rem">// Pick a randon number of times to call the target method</span><br /><span class="kwrd">int</span> iterations = Rnd.Next(255); <br /><span class="rem">// Call the method iteration number of times</span><br /><span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i &lt; iterations; ++i)<br />{<br />DoTheWork();<br />}<br />DisplayCounterValue();<br />}<br /><br /><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> DoTheWork()<br />{<br /><span class="rem">// Get the current threads CallCounter from the CallContext</span><br /><span class="kwrd">int</span> counter = (<span class="kwrd">int</span>)System.Runtime.Remoting.Messaging.CallContext.GetData(PerThreadCallCounterKey);<br /><br /><span class="rem">// Increment the counter and store the new value in the CallContext</span><br /><span class="rem">// Notice the pre-increment (++counter).</span><br />System.Runtime.Remoting.Messaging.CallContext.SetData(PerThreadCallCounterKey, ++counter);<br /><br /><span class="rem">//OK, so we updated the counter now do the real work of this method</span><br /><span class="rem">//...</span><br />}<br /><br /><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> DisplayCounterValue()<br />{<br /><span class="rem">// Get the current threads CallCounter from the CallContext</span><br /><span class="kwrd">int</span> counter = (<span class="kwrd">int</span>)System.Runtime.Remoting.Messaging.CallContext.GetData(PerThreadCallCounterKey);<br />System.Console.WriteLine(<span class="str">"The method was called {0} times from this thread"</span>, counter);<br />}<br />}<br /></pre><br /></div><br />Notice how neither the DoTheWork or DisplayCounterValue methods have any knowledge of the calling thread. They are only aware of context information which is thread specific.<br /><br /><b>Thread Static Members</b><br />As an alternative to using the CallContext class, .NET provides support for what I call Thread Static Members. Thread static members for the most part act like normal static members except that they have per-thread storage rather than per-AppDomain. Declaring a thread static member is very similar to declaring a normal static member, except that the ThreadStaticAttribute attribute is applied to the declaration as follows.<br /><br /><br /><div class="csharpcode-wrapper" id="codeSnippetWrapper"><br /><pre class="csharpcode" id="codeSnippet">[ThreadStatic]<br /><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">int</span> PerThreadCallCounter;</pre></div><br />From this point on any thing that is done to the PerThreadCallCounter member affects the instance of this member associate with the current thread of execution. The following achieves the same results as the earlier example, only this time rather than using the CallContext class a thread static member is used.<br /><br /><div class="csharpcode-wrapper" id="codeSnippetWrapper"><pre class="csharpcode" id="codeSnippet"><span class="kwrd">class</span> Class1<br />{<br /><span class="kwrd">static</span> <span class="kwrd">void</span> Main(<span class="kwrd">string</span>[] args)<br />{<br />System.Threading.Thread t1 = <span class="kwrd">new</span> System.Threading.Thread(<span class="kwrd">new</span> System.Threading.ThreadStart(ThreadFunction));<br />System.Threading.Thread t2 = <span class="kwrd">new</span> System.Threading.Thread(<span class="kwrd">new</span> System.Threading.ThreadStart(ThreadFunction));<br /><br /><span class="rem">// Start the threads</span><br />t1.Start();<br />t2.Start();<br /><br /><span class="rem">// Wait for the threads to complete</span><br />t1.Join();<br />t2.Join();<br />System.Console.Read();<br />} <br /><br /><br />[ThreadStatic]<br /><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">int</span> PerThreadCallCounter;<br /><br /><span class="kwrd">private</span> <span class="kwrd">static</span> Random Rnd = <span class="kwrd">new</span> Random();<br /><br /><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> ThreadFunction()<br />{ <br /><span class="rem">// Pick a randon number of times to call the target method </span><br /><span class="kwrd">int</span> iterations = Rnd.Next(255); <br /><br /><span class="rem">// Call the method iteration number of times</span><br /><span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i &lt; iterations; ++i)<br />{<br />DoTheWork();<br />}<br />DisplayCounterValue();<br />}<br /><br /><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> DoTheWork()<br />{<br /><span class="rem">// Increment the static member associated with the current thread</span><br />PerThreadCallCounter++;<br /><br /><span class="rem">//OK, so we updated the counter now do the real work of this method</span><br /><span class="rem">//...</span><br />}<br /><br /><span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> DisplayCounterValue()<br />{<br /><span class="rem">// Display the value of the thread static member to the user</span><br />System.Console.WriteLine(<span class="str">"The method was called {0} times from this thread"</span>, PerThreadCallCounter);<br />}<br />}<br /></pre></div><br /><br />As you can see and I am sure you will agree this is a much simpler solution. However a word of caution, with this example the naming of the instance becomes critical, sooner or later you will need to come back to a piece of code and if you loose site of the fact that a particular static member is bound to a thread rather than the entire AppDomain you could be in for some interesting debugging in the early hours of the morning.<br /><br /><br />While thread static members appear to work just like there normal static member counter parts, there are a few caveats. Firstly you might be tempted to initialize your thread static members in a static constructor. Unfortunately the results might surprise you. At first inspection it might appear that your initialization was ignored, however this is not the case, it is just that the static constructor is not run per-thread, but per-AppDomain. That is it is only executed once which means that the Thread Static member is only initialized for one thread, the and the rest are left untouched. And no, you can not apply the ThreadStaticAttribute to the constructor, this attribute can only be applied to member variables (fields).<br /><br /><br />Of course as apposed to the CallContext solution the ThreadStatic member is limited to the AppDomain boundary, which for most requirements is sufficient.<br /><br /><br /><strong>Thread Data Slots<br /><br /></strong>Thread data slots are allocated once for every thread by using the Thread.AllocateDataSlot(). Once a data slot has been allocated the slot can be used from any thread to access the data stored in the data slot for the executing thread. This most closely matches the workings of the TLS APIs in the Win32 <u>API</u>. Rather than bore you with another rendition of the demonstration application I will leave it to the reader to investigate this solution which is adequately documented in the MSDN documentation.<br /><br /><br /><b></b><br />See Also:<br /><br />Along similar lines as the ThreadStaticAttribute, you might also want to take a look at the ContextStaticAttribute. Especially for those of you interested in the potential of AOP offered by context bound objects.<img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/5brbh3wYaTw" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2009/11/archive-thread-local-storage-in-net.htmltag:blogger.com,1999:blog-4927779461366619687.post-46637785362254047202009-11-01T15:12:00.001+02:002009-11-01T15:12:06.652+02:00Color Gradient<p>Here is a quick routine I put together which is used to calculate discrete transitions from a start color to an end color in 255 steps. You could call this function in a loop to generate all the color transitions from the start color to the end color.</p> <div id="codeSnippetWrapper" class="csharpcode-wrapper"> <pre id="codeSnippet" class="csharpcode"><span class="kwrd">public</span> Color Gradient(Color from, Color to, <span class="kwrd">byte</span> step)<br />{<br /> <span class="kwrd">double</span> stepFactor = (<span class="kwrd">double</span>)step / 255;<br /> <span class="kwrd">return</span> Color.FromArgb(<br /> (<span class="kwrd">int</span>)(from.R + ((to.R - from.R) * stepFactor)),<br /> (<span class="kwrd">int</span>)(from.G + ((to.G - from.G) * stepFactor)),<br /> (<span class="kwrd">int</span>)(from.B + ((to.B - from.B) * stepFactor)));<br />}</pre><br /><br /> <br /></div> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/csfmv52Ru98" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2009/11/color-gradient.htmltag:blogger.com,1999:blog-4927779461366619687.post-70132649918978070292009-09-26T12:01:00.001+03:002009-09-26T12:01:32.257+03:00Re-throw InnerException and preserve the original StackTrace<h4>Repost from my original blog at the dotnetjunkies (03 March 2004)</h4> <p>One of the problems when throwing a <strong>new</strong> exception from with-in a catch handler is that the stack trace information captured for the initial exception is lost and a new stack trace is built from the point of the throw. Of course when throwing the new exception all that is required to circumvent this problem is to set the inner exception of the new exception to the initial exception that was caught by the catch handler.</p> <p>However, on occasion, you want to throw the InnerException it self. Recently on the SADeveloper forum exactly this scenario arose. A poster on the forum was using reflection to invoke a method. The poster realized that exceptions thrown by methods invoked through reflection were wrapped in the InnerException of a TargetInvocationException. Now what the poster wanted to do was to catch the TargetInvocationException and then re-throw the InnerException. I assume the reason for this was that the caller of the method, which performed the reflection invoke, was not concerned with the current implementation detail and was only required to deal with the original exceptions. The only problem with this solution is that once the InnerException is rethrown, the stack trace no longer contained the original stack trace information. </p> <p>Naturally curiosity got the better of me and I set out to see if I could find a solution. After all, this is what I enjoy most about my free time (between 2-3 in the morning), I get to do things I enjoy and that is learn how the internals work. On this occasion, I got lucky and the first idea I had worked. I new that the remoting infrastructure in fact did something very similar. When an exception is throw from the remoting server, the server side stack trace is preserved on the client side. This is done by setting the private member _remoteStackTraceString to the server side stack trace. Then when the StackTrace property of the exception is called, the _remoteStackTraceString is concatenated with the _stackTraceString and returned to the caller. So I figured that all that was required was the set the _remoteStackTraceString of the InnerException to the current StackTrace of the InnerException. Then when the InnerException is re thrown, the stack trace is replaced with the new stack trace, but the _remoteStackTraceString is preserved. And the end result is a complete stack trace as if the original exception being throw never even saw a TargetInvocationException. So the question is how do we go about manipulating the private _remoteStackTraceString, well you guessed it, reflection. </p> <p>As usual, before I present the code for this I have to say, just because something works does not mean it is the right thing to do. But is was Fun... And now I know a little more about how the remoting mechanism and exceptions fit together.</p> <pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">void</span> test1()<br />{<br /> <span class="rem">// Throw an exception for testing purposes</span><br /> <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentException( <span class="str">&quot;test1&quot;</span> ); <br />}<br /><br /><span class="kwrd">void</span> test2()<br />{<br /> <span class="kwrd">try</span><br /> { <br /> MethodInfo mi = <span class="kwrd">typeof</span>(Form1).GetMethod( <span class="str">&quot;test1&quot;</span> );<br /> mi.Invoke( <span class="kwrd">this</span>, <span class="kwrd">null</span> );<br /> }<br /> <span class="kwrd">catch</span>( TargetInvocationException tiex )<br /> { <br /> <span class="rem">// NB: Error checking etc. excluded</span><br /> <span class="rem">// Get the _remoteStackTraceString of the Exception class</span><br /> FieldInfo remoteStackTraceString = <span class="kwrd">typeof</span>(Exception).GetField(<span class="str">&quot;_remoteStackTraceString&quot;</span>,<br /> BindingFlags.Instance | BindingFlags.NonPublic );<br /><br /> <span class="rem">// Set the InnerException._remoteStackTraceString to the current InnerException.StackTrace</span><br /> remoteStackTraceString.SetValue( tiex.InnerException, <br /> tiex.InnerException.StackTrace + Environment.NewLine );<br /><br /> <span class="rem">// Throw the new exception</span><br /> <span class="kwrd">throw</span> tiex.InnerException;<br /> }<br />}<br /><br />...<br /><br /><span class="rem">// Test the effect of the above code by calling test2() and handling </span><br /><span class="rem">// the exception that is thrown</span><br /><span class="kwrd">try</span><br />{<br /> test2(); <span class="rem">// Call the method that uses reflection to call another method</span><br />}<br /><span class="kwrd">catch</span> ( Exception ex )<br />{<br /> Console.WriteLine( ex.ToString() );<br />}</pre> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/KEUqJi3hBJI" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2009/09/re-throw-innerexception-and-preserve.htmltag:blogger.com,1999:blog-4927779461366619687.post-59422548293260900062009-09-24T22:38:00.001+03:002009-09-24T22:38:25.013+03:00Silverlight – Ray casting engine<p>I have been writing a little ray casting engine for Silverlight, this is a topic that I was very interested in some 12 or so years ago and the last time I did anything like this was all in C++. So I whipped out those old notes and references as started out writing the basic engine. So far it can handle multi-height textured walls, textured floors and ceilings, background textures, very basic lighting and in game objects that can be interacted with. </p> <p>While testing the engine I found that my test cases resembled a pac man type game in a “3d” world so I thought what the heck and added some basic scoring, lives and a compass. The results can be seen <a href="http://www.silverarcade.com/games/taylorza/labyrinth" target="_blank">here</a></p> <p><a href="http://www.silverarcade.com/games/taylorza/labyrinth"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ScreenshotMashoo" border="0" alt="ScreenshotMashoo" src="http://lh6.ggpht.com/_sofFKEz4lW8/SrvKr_mnv6I/AAAAAAAAAus/nK-fvm-0UA8/ScreenshotMashoo_thumb%5B7%5D.png?imgmax=800" width="244" height="186" /></a></p> <p>Make no mistake, this is no master piece it is mostly some test code that I was using to test the engine. There are still some minor rendering artefacts that can sometimes be seen in the distance between the walls and the textured floors. The engine still suffers from minor cumulative rounding errors that degrades the quality of the generated scene, but all in good time.</p> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/CaQoxsimtj4" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2009/09/silverlight-ray-casting-engine.htmltag:blogger.com,1999:blog-4927779461366619687.post-74676835802538905702009-08-22T15:41:00.003+03:002011-07-06T06:02:51.821+03:00Silverlight – SpriteSheet management with WriteableBitmapAs part of my Silverlight education process I have been writing some games. First a few basic things like <a href="http://www.silverarcade.com/games/taylorza/sildoku" target="_blank">Sildoku</a> and <a href="http://www.silverarcade.com/games/taylorza/peg-solitaire" target="_blank">Peg Solitaire</a>. Now I am advancing to something a bit more challenging, I am writting a small 2d racing game. For this I started with some of the fun parts, writing a Sprite manager, while this is still in the early stages I thought I would share one of my basic classes the SpriteSheet class.<br />For those of you who do not know what a sprite sheet is, it is an set of images placed in a grid layout and composed into one larger image. Often sequential images in the grid will represent frames in an animation of some action such as walking etc. Take a look at this <a href="http://en.wikipedia.org/wiki/Sprite_sheet" target="_blank">Wikipedia</a> link.<br />My sprite manager at this stage supports both static and animated sprites, using one or more sprite sheets as a source of the images. Here is the code for the SpriteSheet class.<br /><pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> SpriteSheet<br />{<br /><span class="kwrd"> private</span> BitmapSource _spriteSheetSource;<br /><span class="kwrd"> private</span> WriteableBitmap _spriteSheetBitmap;<br /><span class="kwrd"> private</span> <span class="kwrd">int</span> _sheetWidth;<br /><span class="kwrd"> private</span> <span class="kwrd">int</span> _sheetHeight; <br /><br /><span class="kwrd"> public</span> SpriteSheet(BitmapSource spriteSheetSource)<br /> {<br /><span class="kwrd"> if</span> (spriteSheetSource == <span class="kwrd">null</span>) <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentNullException(<span class="str">"spriteSheetSource"</span>);<br /><br /> _spriteSheetSource = spriteSheetSource;<br /> _spriteSheetBitmap = <span class="kwrd">new</span> WriteableBitmap(_spriteSheetSource);<br /> _sheetWidth = _spriteSheetBitmap.PixelWidth;<br /> _sheetHeight = _spriteSheetBitmap.PixelHeight;<br /> }<br /><br /><span class="kwrd"> public</span> WriteableBitmap GetBitmap(<span class="kwrd">int</span> x, <span class="kwrd">int</span> y, <span class="kwrd">int</span> width, <span class="kwrd">int</span> height)<br /> {<br /> WriteableBitmap destination = <span class="kwrd">new</span> WriteableBitmap(width, height); <br /> GetBitmap(destination, x, y, width, height);<br /><span class="kwrd"> return</span> destination;<br /> }<br /><br /><span class="kwrd"> public</span> <span class="kwrd">void</span> GetBitmap(WriteableBitmap targetBitmap, <span class="kwrd">int</span> x, <span class="kwrd">int</span> y, <span class="kwrd">int</span> width, <span class="kwrd">int</span> height)<br /> {<br /><span class="rem"> // Validate incomming data</span><br /><span class="kwrd"> if</span> (targetBitmap == <span class="kwrd">null</span>) <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentNullException(<span class="str">"targetBitmap"</span>);<br /><span class="kwrd"> if</span> (x &lt; 0 || x &gt;= _sheetWidth) <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentOutOfRangeException(<span class="str">"x"</span>);<br /><span class="kwrd"> if</span> (y &lt; 0 || y &gt;= _sheetHeight) <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentOutOfRangeException(<span class="str">"y"</span>);<br /><span class="kwrd"> if</span> (width &lt; 0 || (x + width &gt; _sheetWidth)) <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentOutOfRangeException(<span class="str">"width"</span>);<br /><span class="kwrd"> if</span> (height &lt; 0 || (y + height &gt; _sheetHeight)) <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentOutOfRangeException(<span class="str">"height"</span>);<br /><br /><span class="rem"> // Get pixel buffers for the sprite sheet and the target bitmap</span><br /><span class="kwrd"> int</span>[] sourcePixels = _spriteSheetBitmap.Pixels;<br /><span class="kwrd"> int</span>[] targetPixels = targetBitmap.Pixels;<br /><br /> <span class="rem">// Calculate starting offsets into the pixel buffers </span><br /><span class="kwrd"> int</span> sourceOffset = x + (y * _sheetWidth); <br /><span class="kwrd"> int</span> targetOffset = 0;<br /><br /> <span class="rem">// Note that the offsets and widths are multiplied by 4, </span><span class="rem">this is because Buffer.BlockCopy requires</span><br /><span class="rem"> // byte offsets into the buffers and our buffers are integer buffers. To optimize this I have </span><br /><span class="rem"> // premultiplied to values so that the multiplication is removed from the loop</span><br /><span class="kwrd"> int</span> sourceByteOffset = sourceOffset &lt;&lt; 2;<br /><span class="kwrd"> int</span> sheetByteWidth = _sheetWidth &lt;&lt; 2;<br /><span class="kwrd"> int</span> targetByteWidth = width &lt;&lt; 2;<br /><span class="kwrd"> for</span> (<span class="kwrd">int</span> row = 0; row &lt; height; ++row)<br /> {<br /> Buffer.BlockCopy(sourcePixels, sourceByteOffset, targetPixels, targetOffset, targetByteWidth);<br /> sourceByteOffset += sheetByteWidth;<br /> targetOffset += targetByteWidth;<br /> } <br /> } <br /><br /><span class="kwrd"> public</span> <span class="kwrd">int</span> Width<br /> {<br /> get { <span class="kwrd">return</span> _sheetWidth; }<br /> }<br /><br /><span class="kwrd"> public</span> <span class="kwrd">int</span> Height<br /> {<br /> get { <span class="kwrd">return</span> _sheetHeight; }<br /> }<br />}</pre>The interesting function in this class is the <strong>GetBitmap(WriteableBitmap targetBitmap, int x, int y, int width, int height)</strong> overload. This function is the work horse of the class. It is responsible for transferring a region of the source image starting at x, y to the new WriteableBitmap. The <strong>GetBitmap(int x, int y, int width, int height) </strong>overload calls this function with a pre-allocated WriteableBitmap of the correct dimensions. <br />Using the class is quite simple. Assume you have a couple of standard Image controls declared on your page, something like the following.<br /><br /><pre class="csharpcode">&lt;UserControl x:Class=<span class="str">"Vulcan.SL.TestApplication.MainPage"</span><br />xmlns=<span class="str">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span> <br />xmlns:x=<span class="str">"http://schemas.microsoft.com/winfx/2006/xaml"</span><br />xmlns:d=<span class="str">"http://schemas.microsoft.com/expression/blend/2008"</span> xmlns:mc=<span class="str">"http://schemas.openxmlformats.org/markup-compatibility/2006"</span><br />mc:Ignorable=<span class="str">"d"</span> d:DesignWidth=<span class="str">"640"</span> d:DesignHeight=<span class="str">"480"</span>&gt;<br /> &lt;Canvas x:Name=<span class="str">"LayoutRoot"</span>&gt; <br /> &lt;Image x:Name=<span class="str">"mySprite1"</span> Width=<span class="str">"100"</span> Height=<span class="str">"100"</span> Canvas.Left=<span class="str">"0"</span>/&gt;<br /> &lt;Image x:Name=<span class="str">"mySprite2"</span> Width=<span class="str">"100"</span> Height=<span class="str">"100"</span> Canvas.Left=<span class="str">"110"</span>/&gt;<br /> &lt;/Canvas&gt;<br />&lt;/UserControl&gt;</pre>You will notice that I have not set the Source of the images, we will do this in code using the GetBitmap function of the SpriteSheet class.<br />Next in the code behind file we need to load the image that and create an instance of the SpriteSheet class passing the image. The trick here is that you should only create the SpriteSheet instance once the image has been fully downloaded. Here is one way of doing this.<br /><pre class="csharpcode"><span class="kwrd">public</span> MainPage()<br />{<br /> InitializeComponent();<br /><br /> BitmapImage spriteSheetBitmap = <span class="kwrd">new</span> BitmapImage(<span class="kwrd">new</span> Uri(<span class="str">"/Images/WindmillLeft.png"</span>, UriKind.Relative)); <br /> spriteSheetBitmap.CreateOptions = BitmapCreateOptions.None;<br /> spriteSheetBitmap.ImageOpened += <span class="kwrd">new</span> EventHandler&lt;RoutedEventArgs&gt;(image_ImageOpened); <br />}<br /><br /><span class="kwrd">void</span> image_ImageOpened(<span class="kwrd">object</span> sender, RoutedEventArgs e)<br />{<br /> BitmapImage spriteSheetBitmap = sender <span class="kwrd">as</span> BitmapImage;<br /><br /> SpriteSheet spriteSheet = <span class="kwrd">new</span> SpriteSheet(spriteSheetBitmap);<br /><br /><span class="rem"> // Set the source of the mySprite1 to an image extracted from the SpriteSheet</span><br /> mySprite1.Source = spriteSheet.GetBitmap(0, 0, 224, 240);<br /><br /><span class="rem"> // Set the source of the mySprite2 to an image extracted from the SpriteSheet</span><br /> mySprite2.Source = spriteSheet.GetBitmap(2240, 0, 224, 240);<br />}</pre>Here I create a BitmapImage class and set the CreateOptions to BitmapCreateOptions.None. This ensures that the image is created without delay, however the creation still takes place asynchronously so we need attach a handler to the ImageOpened event which indicates that the image has been downloaded and decoded, indicating that we can now use the image. <br />In the ImageOpened event handler we can construct our SpriteSheet and used the GetBitmap function to extract the regions of interest and assign them to the relevant Image instances. I have purposely not used instance members in these examples so that each piece of code is self contained, and of course I have forgone error checks etc. for the sake of clarity.<br />Note that the SpriteSheet does not perform any kind of caching of the regions that are extracted, this means that if you request the same region multiple times with the GetBitmap function you will receive new instances of WriteableBitmaps for each call. In my case I take care of the caching at a higher level. I have a SpriteFrameSet class which is a cache of the frames used for an animation sequence, internally this class uses the SpriteSheet to build an array of images ie. frames, and from that point on the array is referenced to retrieve each frame from the frame set.<br />As my code for the Sprite and AnimatedSprite classes matures I will hopefully find some time to release that on this blog.<img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/c0eodYGn_7s" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2009/08/silverlight-spritesheet-management-with.htmltag:blogger.com,1999:blog-4927779461366619687.post-24123940628586818862009-08-19T02:33:00.001+03:002009-08-19T02:33:56.822+03:00Silverlight – My first Silverlight games published<p>To day is somewhat of a milestone for me, after more than 15 years in the industry I have never released anything I have written into the wild. More than that, I do very little UI work, focusing mainly on backend server components. </p> <p>So what is all my excitement about, well I have written 2 small Silverlight games and submitted them to <a href="http://www.silverarcade.com" target="_blank">Silver Arcade</a>. Neither game is really ground breaking, they have served as an avenue gaining some basic experience with Silverlight. Each game was written in less than 24 hours so there are no fancy bells and whistles, heck they do not even have sound (Mostly because I do not even have speakers attached to my computer, no I do not play games :))</p> <p>The first game is called Sildoku, yes you guessed it, it is a Sudoku clone. It has 4 levels of difficulty and each game is randomly generated on the fly. Try it <a href="http://www.silverarcade.com/games/taylorza/sildoku" target="_blank">here</a></p> <p><a href="http://lh6.ggpht.com/_sofFKEz4lW8/Sos6UmI8fDI/AAAAAAAAAuc/pQb57m3R2Wc/s1600-h/ScreenshotSmall%5B2%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ScreenshotSmall" border="0" alt="ScreenshotSmall" src="http://lh5.ggpht.com/_sofFKEz4lW8/Sos6WluXcUI/AAAAAAAAAug/W_Wd0CZly04/ScreenshotSmall_thumb.png?imgmax=800" width="164" height="164" /></a>&#160; <br /></p> <p>The second game is Peg Solitaire. Which you can try <a href="http://www.silverarcade.com/games/taylorza/peg-solitaire" target="_blank">here</a></p> <p><a href="http://lh3.ggpht.com/_sofFKEz4lW8/Sos6XlR1EuI/AAAAAAAAAuk/oFzRvVzoWCI/s1600-h/Promotional%5B2%5D.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Promotional" border="0" alt="Promotional" src="http://lh4.ggpht.com/_sofFKEz4lW8/Sos6Y5tZOTI/AAAAAAAAAuo/06y-Eeme-_U/Promotional_thumb.jpg?imgmax=800" width="162" height="162" /></a></p> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/qrXhdj1AClk" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2009/08/silverlight-my-first-silverlight-games.htmltag:blogger.com,1999:blog-4927779461366619687.post-19157561869548858432009-08-18T13:00:00.002+03:002009-09-27T15:27:18.692+03:00Archive – Hacking my way across the process boundary<p><font color="#0000ff">This is another one of the posts that I am migrating from my previous blog. The post is based on an answer I provided on a news group post. A subsequent post provides a similar solution for .NET. This post was put up in early 2004.</font></p> <p>About 2 years ago a C++/MFC question appeared on the news groups asking how to send windows messages to a ListView control in another process and receive the data returned in the LVITEM structure. At the time I came up with what I thought to be an imaginative solution and it worked for the op. Recently this question reappeared but this time for the .NET environment, a quick Google search and I pointed the OP to the original post, leaving the conversion to .NET as an exercise for the reader. Since the routine did the job again and I remember having some fun playing around with the routine so I thought I would revive it here. Now I just need to get around to converting it to .NET :) </p> <p>In short you allocate a block of memory in the target process using VirtualAllocEx, this block must be big enough to hold both the LVITEM structure and any string data you expect back. You also declare an instance of the structure in your local process and setup all the necessary fields; the only field that is configured differently is the pszText member, which is defined as pointing to the block of memory in the target process offset by the sizeof the LVITEM structure. Then the local structure is moved to the target process using WriteProcessMemory. Once all the structures are in place and in the correct process all that remains is to send the message using SendMessage, and passing the address of the structure in the remote process as the lParam. On successful return of the SendMessage the remote process should contain the requested data and you can copy the data back to the local process using ReadProcessMemory. Once again the pszText pointer has to be fixed up in the local process to reference the local buffer offset again by the sizeof LVITEM. </p> <p>The following C++ code provides an example of getting a item from a ListView hosted in another process.</p> <pre class="csharpcode"><span class="kwrd">const</span> DWORD dwBufSize = 1024;<br /><br />DWORD dwProcessID;<br />DWORD dwResult;<br />HANDLE hProcess;<br /><br />BYTE *lpRemoteBuffer;<br /><br />LVITEM lvItem = {0};<br /><br />BYTE lpLocalBuffer[dwBufSize] = {0};<br /><br /><span class="rem">// Get the process id owning the window</span><br />::GetWindowThreadProcessId( hwndListView, &amp;dwProcessID );<br /><br /><span class="rem">// Open the process wih all access (You may not have the rights to do this)</span><br />hProcess = ::OpenProcess( PROCESS_ALL_ACCESS, FALSE, dwProcessID );<br /><br /><span class="rem">// Allocate a buffer in the remote process</span><br />lpRemoteBuffer = (BYTE*)::VirtualAllocEx( hProcess, NULL, dwBufSize,<br />MEM_COMMIT, PAGE_READWRITE );<br /><br /><span class="rem">// Fill in the LVITEM struct, this is in your own process</span><br /><span class="rem">// Set the pszText member to somewhere in the remote buffer,</span><br /><span class="rem">// For the example I used the address imediately following the LVITEM stuct</span><br />lvItem.mask = LVIF_TEXT;<br />lvItem.iItem = 0;<br />lvItem.cchTextMax = 50;<br /><br /><span class="rem">// Point to after LVITEM in the remote buffer</span><br />lvItem.pszText = (LPTSTR)(lpRemoteBuffer + <span class="kwrd">sizeof</span>( LVITEM )); <br /><br /><span class="rem">// Copy the local LVITEM to the remote buffer</span><br />::WriteProcessMemory( hProcess, (LPVOID)lpRemoteBuffer, <br /> &amp;lvItem, <span class="kwrd">sizeof</span>(LVITEM), NULL );<br /><br /><span class="rem">// Send the message</span><br />::SendMessage( hwndListView, LVM_GETITEM, 0, (LPARAM)lpRemoteBuffer);<br /><br /><span class="rem">// Read the struct back from the remote process into local buffer</span><br />::ReadProcessMemory( hProcess, (LPVOID)lpRemoteBuffer, lpLocalBuffer,<br /> dwBufSize, NULL );<br /><br /><span class="rem">//Fix pszText to point to same offset in local buffer</span><br />lvItem.pszText = (LPTSTR)(lpLocalBuffer + <span class="kwrd">sizeof</span>( LVITEM ));<br /><br /><span class="rem">// Clean-up</span><br />::VirtualFreeEx( hProcess, (LPVOID)lpRemoteBuffer, 0, MEM_RELEASE ); <br />::CloseHandle( hProcess );</pre><img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/0PTZ7-hMjKw" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com5http://taylorza.blogspot.com/2009/08/archive-hacking-my-way-across-process.htmltag:blogger.com,1999:blog-4927779461366619687.post-49785058000188684922009-08-15T16:36:00.001+03:002009-08-15T16:36:45.254+03:00Silverlight – WriteableBitmap SetPixel extension method<p>I am in the process of writing two interesting controls (At least interesting to me). For one of the controls I needed a way to directly set pixels on the WriteableBitmap with full support for alpha blending, the result is the following extension method.</p> <pre class="csharpcode"><span class="kwrd">using</span> System;<br /><span class="kwrd">using</span> System.Windows.Media;<br /><span class="kwrd">using</span> System.Windows.Media.Imaging;<br /><br /><span class="kwrd">namespace</span> ExtensionMethods<br />{<br /> <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> WriteableBitmapExtensions<br /> {<br /> <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> SetPixel(<span class="kwrd">this</span> WriteableBitmap wb, <span class="kwrd">int</span> x, <span class="kwrd">int</span> y, Color color)<br /> {<br /> <span class="rem">// Validate that the x,y coordinates are within the bounds of the bitmap</span><br /> <span class="kwrd">if</span> (x &lt; 0 || x &gt;= wb.PixelWidth || y &lt; 0 || y &gt;= wb.PixelHeight) <span class="kwrd">return</span>;<br /> <br /> <span class="kwrd">int</span> offset = (y * wb.PixelWidth) + x; <br /> <span class="kwrd">int</span> pixel;<br /> <span class="kwrd">int</span>[] buffer = wb.Pixels;<br /><br /> <span class="kwrd">if</span> (color.A == 255)<br /> { <br /> <span class="rem">// Since no alpha blending is required we can directly use the incomming color</span><br /> <span class="rem">// compose the integer that needs to be written to the pixel buffer.</span><br /> pixel = (color.A &lt;&lt; 24) | (color.R &lt;&lt; 16) | (color.G &lt;&lt; 8) | color.B;<br /> }<br /> <span class="kwrd">else</span><br /> {<br /> <span class="rem">// Get the current pixle in the pixel buffer that we need to blend with</span><br /> pixel = buffer[offset];<br /> <br /> <span class="rem">// calculate the alpha channel ratios used for the blend of </span><br /> <span class="rem">// the source and destination pixels</span><br /> <span class="kwrd">double</span> sourceAlpha = color.A / 255.0;<br /> <span class="kwrd">double</span> inverseSourceAlpha = 1 - sourceAlpha;<br /><br /> <span class="rem">// Extract the color components of the current pixel in the buffer</span><br /> <span class="kwrd">byte</span> destA = (<span class="kwrd">byte</span>)(pixel &gt;&gt; 24);<br /> <span class="kwrd">byte</span> destR = (<span class="kwrd">byte</span>)(pixel &gt;&gt; 16);<br /> <span class="kwrd">byte</span> destG = (<span class="kwrd">byte</span>)(pixel &gt;&gt; 8);<br /> <span class="kwrd">byte</span> destB = (<span class="kwrd">byte</span>)pixel;<br /> <br /> <span class="rem">// Calculate the color components of the new pixel. </span><br /> <span class="rem">// This is the blend of the destination pixel and the new source pixel</span><br /> <span class="kwrd">byte</span> pixelA = (<span class="kwrd">byte</span>)((color.A * sourceAlpha) + (inverseSourceAlpha * destA));<br /> <span class="kwrd">byte</span> pixelR = (<span class="kwrd">byte</span>)((color.R * sourceAlpha) + (inverseSourceAlpha * destR));<br /> <span class="kwrd">byte</span> pixelG = (<span class="kwrd">byte</span>)((color.G * sourceAlpha) + (inverseSourceAlpha * destG));<br /> <span class="kwrd">byte</span> pixelB = (<span class="kwrd">byte</span>)((color.B * sourceAlpha) + (inverseSourceAlpha * destB));<br /><br /> <span class="rem">// Reconstitute the color components into an int to be written to the pixel buffer</span><br /> pixel = (pixelA &lt;&lt; 24) | (pixelR &lt;&lt; 16) | (pixelG &lt;&lt; 8) | pixelB;<br /> }<br /><br /> <span class="rem">// Write new pixel to the pixel buffer</span><br /> buffer[offset] = pixel;<br /> }<br /> }<br />}</pre><br /><br /><p>If you are interested, the 2 controls I have written are</p><br /><br /><ol><br /> <li><strong>TextureSurface</strong> – A control which supports direct manipulation of the pixel buffer. The convenience is that there is no need for the developer to manage the WriteableBitmap and the Image control and there are precooked functions for setting pixels with alpha channel support etc.</li><br /><br /> <li><strong>ImageEx</strong> – This is an enhanced Image control which supports rendering only a portion of the source image, the purpose is to provide a means of rendering regions from a source image to be used like a sprite sheet.</li><br /></ol><br /><br /><p>Both controls are quite functional, but being quite new to Silverlight and not having done any WPF I am a little concerned that I have not made the most efficient use of the Control life cycle and feel like I have hacked a few things to work. Anyone out there willing to take a look at the code and make a few recommendations? </p> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/1ET-7dNXe_E" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com2http://taylorza.blogspot.com/2009/08/silverlight-writeablebitmap-setpixel.htmltag:blogger.com,1999:blog-4927779461366619687.post-47953044017920789892009-08-14T12:26:00.001+03:002009-08-14T12:31:08.915+03:00Silverlight – Set focus to the silverlight control<p>When loading a web page which hosts a Silverlight application, I noticed that before interacting with the Silverlight application the user was required to first click on the application so that it got input focus. It was immediately obvious that I would need to use a small piece of java script to set the focus to the control. This is how I got it to work.</p> <p>1. Give the &lt;object&gt; element in the web page an ID <br />2. Pass the ‘onLoad’ parameter on the Silverlight application assigning a JavaScript function that will focus to application in the browser.</p> <pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">object</span> <strong><em><span class="attr">id</span><span class="kwrd">=&quot;silverlightControl&quot;</span></em></strong> <span class="attr">data</span><span class="kwrd">=&quot;data:application/x-silverlight-2,&quot;</span> <span class="attr">type</span><span class="kwrd">=&quot;application/x-silverlight-2&quot;</span> <span class="attr">width</span><span class="kwrd">=&quot;100%&quot;</span> <span class="attr">height</span><span class="kwrd">=&quot;100%&quot;</span><span class="kwrd">&gt;</span><br /> <span class="kwrd">&lt;</span><span class="html">param</span> <span class="attr">name</span><span class="kwrd">=&quot;source&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;ClientBin/DragSnapDemo.xap&quot;</span><span class="kwrd">/&gt;</span><br /> <span class="kwrd">&lt;</span><span class="html">param</span> <span class="attr">name</span><span class="kwrd">=&quot;onError&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;onSilverlightError&quot;</span> <span class="kwrd">/&gt;</span><br /> <strong><em><span class="kwrd">&lt;</span><span class="html">param</span> <span class="attr">name</span><span class="kwrd">=&quot;onLoad&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;silverlightControlHost_Load&quot;</span> <span class="kwrd">/&gt;</span></em></strong><br /> <span class="kwrd">&lt;</span><span class="html">param</span> <span class="attr">name</span><span class="kwrd">=&quot;background&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;white&quot;</span> <span class="kwrd">/&gt;</span><br /> <span class="kwrd">&lt;</span><span class="html">param</span> <span class="attr">name</span><span class="kwrd">=&quot;minRuntimeVersion&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;3.0.40624.0&quot;</span> <span class="kwrd">/&gt;</span><br /> <span class="kwrd">&lt;</span><span class="html">param</span> <span class="attr">name</span><span class="kwrd">=&quot;autoUpgrade&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;true&quot;</span> <span class="kwrd">/&gt;</span><br /> <span class="kwrd">&lt;</span><span class="html">a</span> <span class="attr">href</span><span class="kwrd">=&quot;http://go.microsoft.com/fwlink/?LinkID=149156&amp;v=3.0.40624.0&quot;</span> <span class="attr">style</span><span class="kwrd">=&quot;text-decoration:none&quot;</span><span class="kwrd">&gt;</span><br /> <span class="kwrd">&lt;</span><span class="html">img</span> <span class="attr">src</span><span class="kwrd">=&quot;http://go.microsoft.com/fwlink/?LinkId=108181&quot;</span> <span class="attr">alt</span><span class="kwrd">=&quot;Get Microsoft Silverlight&quot;</span> <span class="attr">style</span><span class="kwrd">=&quot;border-style:none&quot;</span><span class="kwrd">/&gt;</span><br /> <span class="kwrd">&lt;/</span><span class="html">a</span><span class="kwrd">&gt;</span><br /><span class="kwrd">&lt;/</span><span class="html">object</span><span class="kwrd">&gt;</span></pre><br /><br /><p>3. Finally write the JavaScript function that is called when the Silverlight application has be loaded</p><br /><br /><pre class="csharpcode"><span class="kwrd">function</span> silverlightControlHost_Load(sender, args) <br />{<br /> <span class="kwrd">var</span> control = document.getElementById(<span class="str">&quot;silverlightControl&quot;</span>);<br /> control.focus();<br />}</pre><br /><br /><p>The trick here is that the JavaScript function is only called once the Silverlight application has been fully downloaded and loaded into the browser. To achieve this I used the onLoad event of the application.</p> <img src="http://feeds.feedburner.com/~r/taylorza_blogspot/~4/bVmg3tPZfr4" height="1" width="1" alt=""/>Chris Taylorhttp://www.blogger.com/profile/16376474093485672479noreply@blogger.com0http://taylorza.blogspot.com/2009/08/silverlight-set-focus-to-silverlight.html