Archive for the ‘Programming’ Category

Debugging “Just My Code”

Saturday, December 5th, 2009

Within EVEMon we have started making heavy use of LINQBridge which uses Visual Studio 2008’s Multi-Targeting capabilities to allow a .NET 2.0 applications to use the compiler functionality of C# 3.0. This reduces our need to push EVEMon towards .NET 3.5, and simplifies our dependency stack for the end user (.NET 2.0 is pre-installed on Vista and above, .NET 3.5 is pre-installed in Windows 7 and above).

One of the annoyances I have run into is every time there is a problem with a LINQ statement the debugger will stop in the LINQBridge project rather than EVEMon’s code; this usually tells you nothing useful forcing you to dig into the exception to find the stack trace to find out which line caused the exception.

I found a natty attribute in DebuggerNonUserCode that allows you to tell the debugger to treat a class as Non-User Code:

[System.Diagnostics.DebuggerNonUserCode]

So far, I have not found a disadvantage in doing this. I am being conservative with my use in case I find some glaring problem, however LINQBridge has proven a stable project, and quite frankly I would much rather be looking at my own broken code when something goes wrong, rather than LINQBridges working code.

Controling Code Outlining with the Keyboard

Tuesday, November 3rd, 2009

Code outlining is a feature of supported by Visual Studio and many other editors, MSDN has some good documentation for VS2005, VS2008 and VS2010. If I were asked to explain this as briefly as possible, I would probably say:

Code Outlining is the logical partitioning of code in such a way that the user interface, or editor, is able to selectively hide the body of the content (such as a class, struct, enum or method) whilst leaving the signature or some identifying comment visible.

You can see this in action in Visual Studio 2008 with the following Screenshot:

CodeOutliningVS2008

I accidentally turned off Code Outlining today by hitting some keyboard shortcut that I didn’t know how to reverse, this lead me to discover several useful keyboard shortcuts for managing the display of your code from the keyboard.

As it turns out I managed to hit Ctrl-M followed by Ctrl-P (or just P in fact) which maps to Edit.StopOutlining, by default it seems that the Visual C# 2005 mapping scheme doesn’t provide a shortcut to enable Automatic Outlining so instead you can access the command through Edit Menu -> Outlining -> Start Automatic Outlining.

Enabled again, I get to play with code outlining from the keyboard:

  • To toggle (collapse an expanded block or expand a collapsed block) the closest outlined element use Ctrl-M followed by Ctrl-M.
  • To toggle everything use Ctrl-M followed by Ctrl-L (I find little use for this)
  • To collapse to definitions use Ctrl-M followed by Ctrl-O

The last one is the most useful when used in conjunction with Regions as after colapsing to definitions you will get something similar to this:

ColapseToDefinitionsVS2008

You might have noticed in the first screenshot that CodeRush Xpress adds a coloured line between the beginning and end of blocks of code, this is a nice feature if you have long blocks of code, which of course you shouldn’t have.

CodeRushXpressBlockLines

There we go, an errant key stroke can lead to learning and blogging, who would have thought it?

Adjusting Selections in CodeRush Xpress

Friday, October 16th, 2009

I found this function totally accidentally when I knocked my mouse into a key when something interesting popped up on Twitter. I happened to have a variable selected like so:

Variable Selected in Visual Studio

The key that I knocked was the Number Pad “+” key, and it expanded the selection like this:

VariableSelectedPlus1

As any self respecting Systems Administrator would do I wondered if doing exactly the same thing repeatedly would have equal or compound effects.

VariableSelectedAgainAgain

Strangely enough it worked, the selection will continue to expand selecting increasingly larger sections of code. The reverse works as well if you press the Number Pad “-” key the selection will be reduced.

WhatHappened

As I was looking for documentation on this function; I found that you could use the CamelCase select function to start selections off using Ctrl+Shift+Left (or Ctrl+Shift+Right) this will take your selection from the current cursor position to the next upper case character to the left (or right). This can be coupled with the Number Pad “+” to expand your selection from a variable to an expression, to a line then a block. Neat!

Each of these discoveries was announced with a handy little popup in the bottom right hand corner of the viewport telling me what happened, and if relevant what function was suppressed.

I am going to make a concerted effort to spend my lunch break watching some more videos on DevExpress’ training site. I got 10 minutes today and watched what appeared to be footage from a launch event or a conference which was interesting but didn’t really teach me anything specifically.

Find Files in CodeRush Xpress

Monday, October 12th, 2009

As I mentioned in my previous entry I have started using DevExpress’s CodeRush Xpress. It is a free cut down version of CodeRush that I heard about in an episode of .NET Rocks. I have wanted add something to my Visual Studio development experience and I can’t justify the cost of either Resharper or CodeRush at the moment. There has been quite a bit of discussion about Resharper vs. CodeRush and in my experience most people I have spoken to love one and hate the other.

I hope to be able to write a series of posts about some of the features found in CodeRush Xpress which I hope will clarify their purpose and use in my mind and maybe help someone find the function they are looking for.

QuickFileNavigationAbouFirst off “Quick File Navigation”, this is a search function for locating a file by file name, I am finding it increasingly useful when looking through patches as it allows me to very quickly jump to a file name. Particularly as I am trying to move my projects to a one class per file so if I can remember the class name I can find the code file very quickly.

The “Quick File Navigation” dialog is accessible through the keyboard short cut Ctrl+Alt+F. Typing will filter the list box by the name of the file matching anywhere in the filename including the extension.

An additional feature for those who like me use CamelCase in their file names; if you enter your filter terms in capital letters it will search for capitalized words, in order within file names. Thus entering “AW” into the search box will also bring up the AboutWindow.cs in the above solution.

Combining the above with the Ctrl-G keyboard shortcut in Visual Studio 2008 to go to a specific line we can do the following to go to line 162 in ShipLoadoutSelectWindow.cs:

Ctrl+Alt+F  →  S,L,S,W  →  Ctrl+G →  1,6,2

DevExpress have a great training video on their tv.devexpress.com sub site.

Ctrl+Alt+F → S,L,S,W → Ctrl+G → 1,6,2

Long time no blog…

Saturday, October 10th, 2009

I had been intending to post up all sorts of bits of news about life, driving, programming and cats but it didn’t happen. I have been busy which is good, however it doesn’t leave very much room for blogging. This post is going to be one really quick catch up entry to try and get me back into the swing of it.

I finally passed my driving test at the beginning of the summer, it was my fourth try and I passed with three minors which was a great feeling… since then I have driven once, as part of PassPlus, which didn’t really feel vastly different, although it was more enjoyable knowing that I didn’t have a test coming up. Catherine and I have decided to wait for a bit before we get a car so we can assess what Catherine will need in terms of model and adaptations in a car to allow her to get the most out of it.

Zoe and KerrySadly Kerry, Zoe’s sister died a few months after Zoe, as a family we weren’t ready to lose her, although it wouldn’t have been fair to let her suffer an operation that might not help anyway, so my parents decided it was best to have her put down. My brother and I got a chance to say good bye to her, she wasn’t her normal self and was in a lot of pain. We are all very sad.

In programming land I spent last weekend learning SQL Server Integration Services and this weekend I plan to get into Analysis Services. Have been doing some utility programming at work to automate some of the file server operations we do, I am trying to automate as much as possible using something similar to duck tape programming to free me up to do more programming to create a more unified and friendly framework for managing the school network. Started using CodeRush Xpress both at home and at work.

In sys. admin world I have been continuing to use and learn Windows 7, I now have RTM installed at work still waiting for RTM to arrive at home on or around the 22nd. Been reading and listening to various resources surrounding Windows 2008 R2 particularly some excellent podcasts on RunAs radio.

Fixing EVEMon’s Crashy C++ DLLs

Saturday, April 18th, 2009

I have been working on EVEMon for about two months now, taking on the responsibility of committing changes to the trunk, fixing bugs and adding new features. As a project I have been involved for several years submitting bug fixes and little features, it was down to my experiences with EVEMon that I decided to implement Subversion and Trac at work.

Unfortunately the first time it came to me to be responsible for a release, it seemed to go terribly wrong. The updated installer worked fine, and it seemed initially there were no problems with the updated code base. However BattleClinic shortly went a little mad with bug reports similar to this one:

EVEMon Version: 1.2.7.1283
.NET Runtime Version: 2.0.50727.1434
Operating System: Microsoft Windows NT 6.0.6001 Service Pack 1
Executable Path: “C:\Program Files\EVEMon\EVEMon.exe”

System.IO.FileLoadException: Could not load file or assembly ‘lgLCDNETWrapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log for more detail. (Exception from HRESULT: 0x800736B1)
File name: ‘lgLCDNETWrapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null’ —> System.Runtime.InteropServices.COMException (0x800736B1): The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log for more detail. (Exception from HRESULT: 0x800736B1)
at EVEMon.LogitechG15.Lcdisplay.Dispose(Boolean bDisposing)
at EVEMon.LogitechG15.Lcdisplay.Finalize() in D:\EVEMon\EveMon.LogitechG15\Lcdisplay.cs:line 70

For those who are not familiar the above is the crash report generated by EVEMon when a .NET exception is unhandled. lgLCDNETWrapper is the C++ library EVEMon uses to communicate with the Logitech G15. A similar error was generated for EVEMons other C++ component the window relocator.

Tonto Auri quickly spotted that the problem was something to do with the re-compiled C++ DLLs and switching the new DLLs with those from an older package resolved the problems. Whilst this was a fix, I wasn’t willing to just revert the changes and give up on the changes we had made to the G15 library, additionally I was concerned that we had not made changes to the window relocation code and that was causing at least one person a problem.

Quite a bit of searching about with Google and Stack Overflow resulting in finding these two little gems of information:

These two posts basically gave me the tools and the knowledge to fix EVEMon’s problems.

First I made the sudden (and “facepalm”) realization that EVEMon deployed the Visual Studio 2005 Redistributable; we had moved to Visual Studio 2008 SP1 about a month before the release of 1.2.7. I confirmed this with the Dependency Walker utility, by walking lgLCDNETWrapper.dll I was able to confirm that version 9.0.21022.8 of the Microsoft CRT Library (Microsoft.VC90.CRT) would be required.

The next trick was figuring out which re-distributables to deploy with EVEMon to ensure the end user would have all the dependencies required to use all of the features; frustratingly it seems that Microsoft only bundles the latest version of the re-distributables with Visual Studio 2008 and that was version 9.0.30729.1 which was not going to cut it.

Enter Nuonsofts excellent article providing a step by step guide on adding the required compiler flags to ensure the latest version of the CRT was bound. I did find a nice GUI resource editor that was capable of getting the manifests out in XN Resource Editor which made the process a little faster.

A little bit of hacking later the updated Visual Studio 2008 SP1 CRT DLLs and manifest file were deployed to the Microsoft.VC90.CRT folder within the EVEMon program files directory as the VC80 CRT libraries were in the past and the two C++ projects were setup to bind to the latest version with the _BIND_TO_CURRENT_CRT_VERSION=1 preprocessor definition.

A bit more poking arround with XN Resource Editor and Dependency Walker showed we were now in a far better position to have a working copy of EVEMon in the hands of our end users.

Strange SPQuery Behaviour

Friday, February 13th, 2009

I have been working on a very simple web part for a long time, the actual code for the web part takes less than an hour to write and merely displays a colour coded letter based on a query from a SharePoint list taking the date as a parameter.

It has taken a long time because of some strange behaviour with the SPQuery object, initially there was a problem with every row in the table being returned irrespective of the query, the odd thing was that if you ran the query in U2U CAML Query Builder it worked and the SPListItemCollection.count method would return 1 suggesting the query was working.

After adding a copious number of debugging command, I built up a pattern of what was happening and why. However no matter what I changed it either threw an exception as expected or returned all of the rows. I started commenting out lines of code one at a time trying to find the culprit, eventually moving on to commenting out the Debug.WriteLine statements, until I hit this line:

86
Debug.WriteLine(fetchCurrentWeek.ViewXml);

After commenting out this line the whole thing worked perfectly, I can put the date forward on the server and it will update I can change the source table and it reflects the changes after the cache has been cleared. I have looked over the Microsoft Documentation on SPQuery.ViewXml and can’t find any reference to it actually affecting the functionality of the object.

I have included my code at the end of this post for anyone to see, feel free to experiment with it. If you figure it out please do let me know.

using (SPWeb site = SPControl.GetContextSite(Context).OpenWeb())
{
 SPList weekList = site.GetList(this.SourceList);
 // sharepoint requires that we use a ISO8601 DateTime string,
 // generate it and insert it into the query
 string mondayOfCurrentWeek = SPUtility.CreateISO8601DateTimeFromSystemDateTime(GetMondayOfCurrentWeek().Date);
 StringBuilder camlBuilder = new StringBuilder();
 camlBuilder.AppendLine  ("<Where>");
 camlBuilder.AppendLine  ("  <Eq>");
 camlBuilder.AppendLine  ("    <FieldRef Name='WeekCommencing'/>");
 camlBuilder.AppendFormat("    <Value Type='DateTime'>{0}</Value>\r\n", mondayOfCurrentWeek);
 camlBuilder.AppendLine  ("  </Eq>");
 camlBuilder.AppendLine  ("</Where>");
 
 string getWeekCAML = camlBuilder.ToString();
 SPQuery fetchCurrentWeek = new SPQuery(weekList.DefaultView);
 // fetch all of the matching weeks from the table
 fetchCurrentWeek.Query = getWeekCAML;
 SPListItemCollection matchingWeeks = weekList.GetItems(fetchCurrentWeek); // the results of the query should only ever equal 1 if it dosn't something is wrong
 if (matchingWeeks.Count == 1)
 {
  SPListItem week = (SPListItem)matchingWeeks[0];
  WeekType = week["Week Type"].ToString();
  WeekType = SPEncode.HtmlEncode(WeekType); // HtmlEncode the result to make sure it dosn't contain any nastys
  String WeekNumber = week["Week Number"].ToString();
  WeekNumber = SPEncode.HtmlEncode(WeekNumber);
  Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "[WeekWidget] Found record for {0} as Week {1} - Number {2}", mondayOfCurrentWeek, WeekType, WeekNumber));
  // cache the WeekType for 7 days from midnight on Monday as this is when the changeover between weeks happenswou
  Context.Cache.Insert(CACHE_WEEKTYPE, WeekType, null, GetMondayOfCurrentWeek().AddDays(7), TimeSpan.Zero);
 } // end if matchingWeeks count equal to 1
 else
 {
  // more than one week matched, we can not determine the output correctly
  ErrorText.Text = "More than one week matched in the table, please check the table.";
  LogEvent("More than one week (" + matchingWeeks.Count.ToString() + ") matched in the table, please check the table.");
 } // end else

Happy Hunting

Adding VSeWSS 1.3 Solutions to Source Control

Thursday, February 5th, 2009

Having done a little experimenting with Visual Studio Extensions for WSS (VSeWSS), I wanted to start actually developing features for our intranet site. I try and add everything that is even slightly important into source control (Subversion). VSeWSS creates normal looking solutions, however when you deploy your project to a SharePoint site it created an additional directory alongside “bin” and “obj” called “pkg”.

This “pkg” folder contains the manifest.xml, soloution.xml and feature.xml files that are used to create the feature to be deployed into SharePoint. Initially I was including this folder in my commits however, I noticed that any tweaks made to feature.xml were overwritten when you deployed the package again. After some searching around I came across an article that suggests deleting the “pkg” folder under certain circumstances. From this I assume that the contents of the “pkg” folder is generated each and every time you package and deploy your solution (or indeed project), thus it does not need to be added to source control.

While writing this post I did come across another blog that suggests adding the “pkg” folder to source control. There does appear to be little advice out there regarding VSeWSS and Source Control. I would be interested to hear others experiences on the subject.

Learn a Little More About Richard Slater

Saturday, December 13th, 2008

Graeme Arthur tagged me, normally I hate these things but this is kind of a reverse chain letter… sort of… maybe if i re-write the rules slightly.

Rules:

  • Link to the original tagger(s), and list these rules on my blog.
  • Share 7 facts about myself in the post – some random, some weird.
  • Tag 7 people at the end of your post by leaving their names and the links to their blogs.
  • Let them know they’ve been tagged by leaving a comment on their blogs and/or Twitter.

7 Facts About Me:

  1. At the time of writing I have been alive for approximately 871 million seconds.
  2. I first started programming (in Spectrum BASIC) at about the age of 8 since then I have coded projects in Visual Basic, Pascal, C/C++, Java, Ada, PHP and C#.
  3. I have played EVE Online for almost five years now, have been playing less often of late however I still get to enjoy the time I do spend in EVE, although it is getting increasingly more and more lonely in space.
  4. I have been learning about Sharepoint over the past few weeks, I have already completed most of the Microsoft e-Learning on the topic and read several blogs, I recently signed up to the Sharepoint User Group UK.
  5. I wrote my dissertation on the future of MMOGs, I disagree with a good proportion of what I wrote, however it was one of the most enjoyable parts of my Degree.
  6. I type at between 40 and 60 words per minute, it has taken me 5 minutes to get to 300 words in this blog post, some of them however were copied and pasted from Graeme’s post.
  7. Like Graeme I also play bass (albeit I play badly) and dabble in live PA, although I don’t really enjoy the later any more.

7 people to tag:

  1. Catherine over at I Married You
  2. Haly over at Halycopter
  3. Tim over at Thoughts of a musing pedant
  4. CrazyKinux over at CrazyKinux’s Musing
  5. Winterblink over at winterblink.com
  6. James Glass over at Outthere
  7. Peter Arthur over at Robotii

I know the last two were on Graeme’s list, however I honestly can’t think of anyone who would appreciate it most of the blogs I read are a strictly one way thing. I was tempted to link JoCo, Felicia Day and Mark Russinovich – however I really don’t think they would appreciate it.

Credit where credit is due Mythic!

Tuesday, August 26th, 2008

Apparently this was released sometime last week however I had missed it completely, Mythic and its Electronic Arts overlords have decided that they will not be crediting former staff members in Warhammer Online. I really don’t understand it, Ryan Shwayder put it across better than I can.

Everyone who contributes to a project puts something in, even if their product is not explicitly included in the final complete product they are part of the process. Not including someone’s name in the credits of anything be it a game, a film or a TV show is a giant slap in the face. As a programmer I would be gutted to think that I had been excluded from the credits having put years of work into and then moved on to better things a few months before launch.

Open source manages it just fine, huge sweeping lists of credits for relativly minor utilities. Why can a game? Its not like anyone is forced to watch it or read it. Put it as a file in the install directory, or a page on a website.