The Developer Stash

Arbitrary Contemplations
posts - 20, comments - 29, trackbacks - 0

My Links

News




Locations of visitors to this page

 





Elroy D'silva's Blog

Twitter












Archives

Post Categories

Blogs I read

Saturday, November 14, 2009

Write to a console window from a Windows application

Ever wondered how you could write out to a separate console window from your Windows application rather than writing to a log file? You might need to do that for a number of reasons – mostly related to debugging. Here’s how you do it:

Predefined APIs are available in kernel32.dll that will allow you to write to a console.

AllocConsole

FreeConsole

AttachConsole

AllocConsole() attaches your process to a console window where the process’ standard output will be redirected. A process can be associated with only one console, so the AllocConsole() function fails if the calling process is already associated with a console. FreeConsole() is used to detach the process from the current console and AttachConsole() can be used to attach associate a console with the current process.

The following shows a sample usage of the feature.

Capture

 You’ll need to import the AllocConsole and FreeConsole API from kernel32.dll.

Capture1

  And you can always make it Conditional if you want to use it only while debugging.

Capture2

posted @ Saturday, November 14, 2009 3:46 AM | Feedback (1) |

Saturday, August 29, 2009

VSX Add-in Development Adventures: Window.Object returns NULL

I’m in the middle of developing an add-in for Visual Studio 2008, a simple one that would help our in-house code review process. If you’ve ever written a VS add-in, you’d know the basic routine – Visual Studio creates the basic class Connect derived from IDTExtensibility2, then you put in your code in the methods OnConnection, OnDisconnection, OnAddInsUpdate, OnStartUpComplete and OnBeginShutdown.

I developed a few user controls (UserControl objects) for showing some UI as tool windows. One of them was AddCommentView which is just a dumb UserControl that displays some textboxes and buttons and also takes in some input. I deployed this control as a separate DLL and linked it to the add-in. Then, as usual, the following code should do the job of creating the tool window.

AddCommentView

And like always it worked just fine till I tried to test it and that’s when I realized something wasn’t quite right.

The CreateToolWindow2 method has two outputs – the tool window (which is returned by the method) and the hosted user control (the ref parameter ‘objTemp’ in this case). The hosted user control can also be retrieved through the Window.Object property of the returned tool window. The problem was – no matter what I tried the Window.Object property always returned null and so did ‘objTemp’.

RetrieveValues

The solution – I was acting dumb, very dumb. Visual Studio requires all hosted controls to be visible to COM. After adding the attribute to the entire assembly using [assembly: ComVisible(true)] , everything started working like a charm (you can apply it even to the individual control if you don’t want to make the entire assembly COMVisible).

posted @ Saturday, August 29, 2009 1:01 PM | Feedback (0) |

Saturday, April 18, 2009

TaskList in Visual Studio - Making it better

It was great to come across a new feature in Visual Studio - The TaskList. Well, yes it isn't new but, it certainly was new to me. I had never really come across this wonderful feature until last week when secretGeek blogged that code would suck less if the compiler could raise an error when a // TODO token was detected. A comment from Goran indicated that it was completely possible with the current versions of Visual Studio.

A //TODO: token.

Are shown in VS if you click TaskList. Couldn’t live without this one :)

Yeah, sure. Can’t live without it. Although the compiler doesn’t throw you an error, it indicates all parts of code where such tokens appear and hence can be taken care of easily.

 

How To Use

You can use the TaskList comments for:

  • Features to be added
  • Problems to be corrected
  • Classes to implement
  • Place-markers for error handling code
  • Reminders to check in the file

Add your comment in the code preceded by the token.        

tasklist_1

Once done, open up the TaskList from the View menu.

tasklist_2

Once the TaskList is up you will see something like this.

tasklist_3

The TaskList also allows custom tokens to be included. Go to Tools –> Options –> Environment –> TaskList.

 tasklist_4

User Tasks

You can add your own tasks in the TaskList by selecting the ‘User Tasks’ selection from the dropdown list on the TaskList toolbox. These tasks aren’t associated with code, but will allow users to add their own high level tasks.

 

Extending the TaskList

I would like to see the following additional options in the TaskList:

  • Task Alarms – which would associate an alarm with a high priority comment to help just in case you forget to take care.
  • User Tasks to be more specific (by having a file anchor like ‘Comments’) to allow user tasks to be associated with a particular line in code.

And no, I’m not waiting on Microsoft to extend it in some other version of Visual Studio. I’m starting right away to write a package that would do this for me, hopefully.

 

More Things to Remember

With Visual Basic projects, the Task List displays all of the comments in the project. With Visual C# and Visual J# projects, the Task List displays only the comments that are found in the files currently opened for edit. With Visual C++ projects, the Task List displays only the comments that are found in the file currently active in the editor.

posted @ Saturday, April 18, 2009 4:59 AM | Feedback (5) |

Monday, January 05, 2009

C# 3.0 for Beginners - Extension Methods - Part 2

Custom Extension Methods

C# 3.0 enables you with the ability to extend the functionality of existing types. This means that you can write your own extension methods that would give the programmer the feeling that those are just methods provided by the existing type.

For example, System.Int32 doesn't provide you an IsEven() method which would tell you if the integer is holding an even value. It would obviously be great if you could write a method which does this, just in case you use this functionality heavily in your code. You would then write a method like this

public bool IsEven(Int32 i)
{
 if(i%2 == 0)
 {
     return true;
 }
 else
 {
     return false;
 }
}

Well, that's perfectly fine. But, I wish we could make it more easier. And I bet we can for sure, that's what extension methods are here for. No, we cannot go and implement the IsEven() method for the Int32 type, but C# 3.0 allows you to extend type functionality in a different way.

Check out the following code. I'm extending the Int32 type to expose the Int32.IsEven() extension method.

public static class MyIntegerExtensionMethod
{
 public static bool IsEven(this Int32 i)
 {
     if (i % 2 == 0)
     {
         return true;
     }
     else
     {
         return false;
     }
 }
}

public class Program
{
 static void Main(string[] args)
 {
     Int32 n = 9;
     Console.WriteLine(n.IsEven());
 }
}

Let's look at the most important part, the method signature.

public static bool IsEven(this Int32 i)

A template for the above would be

<access_modifier> static <return_type> Method_Name(this <extended_type> <instance_of_type>, <args_list>)

While <access_modifer> and <return_type> are intuitive, special attention is needed at the parameters of the method. The first parameter resembles the type which is to be extended, preceded with the 'this' keyword. The arguments that follow are the actual arguments/parameters to the method.

Another thing to remember is that extension methods should be defined as static members in a static class. The only difference between a normal method and an extension method is that the first parameter of an extension is always prefixed with the 'this' keyword. The rest are normal parameters. Also, when using the extension method you should remember to keep the extension method within the scope of its usage or you could even include it using the 'using' directive.


Usage Restrictions

  • Extension methods can access only the public members of the type being extended.
  • If you define an extension method whose signature matches with the method already existing in the extended type, priority is given to the existing method and the new extension method is ignored.


Have fun with this great feature.

posted @ Monday, January 05, 2009 11:32 PM | Feedback (0) |

Saturday, January 03, 2009

Why I will never have a girlfriend - My take

My take on Tristan Miller's paper titled Why I will never have a girlfriend.

You can find it here

What are your views on this?

posted @ Saturday, January 03, 2009 1:23 PM | Feedback (0) |

Powered by: