Expression evaluation

(+6 rating, 6 votes)

May 27th, 2010

Let’s start with a simple quiz: 7/2 = … . Of course is 3.5 but is this also true for code?

If you somehow use a non-fractional data type for storing the result, you will always get the result 3. And that should not surprise you.

int result = 7/2; //expression is 3

However, if you choose to use a fractional data type, things will change …

double result = 7/2;

… or not. The value stored in the variable result is still 3 (actually 3.0 or something really close to 3.0 – since floating point data types store the approximation of a number).

Why is this happening?
Read the rest of this post »

New clothes

(0 rating, 8 votes)

May 17th, 2010

As you may have noticed, the blog got new clothes (a new theme).

This new theme is completely written by myself (yeah, I wrote PHP :-) ) but I used some other sites and themes as sources of inspiration. First of all, I tried to create a theme that will present just the actions that are available in and related to the current context. For example: you will not see the recent archive on the About page. Similarly, you will not see my link recommendations or details about me on the main page; for such details access the About page.

Secondly, the sidebar is fully dynamic and, usually, changes as you navigate through the site. It will display information related to the current (context) page. For the moment, some sidebars are quite empty since I have some more widgets to develop.

Voting was moved at the end of the post and is visible only when you see the full post. In my opinion, there is no reason to vote a post until you went through it. Also, the starts for voting were replaced by a binary choice (Like/Dislike).

Read the rest of this post »

Fancy windows previewer

(+7 rating, 7 votes)

May 5th, 2010

Tutorial description
Objective

Create a fancy-looking application that displays the preview of the open applications.

Covered topics
  • Enumerating windows and getting various information about them
  • Creating the Aero glass effect
  • Using the DWM windows preview feature
Requirements
  • Windows Vista/7 with the Aero theme active
  • Visual Studio 2010 (or 2008 but requires some changes in code which are not covered by this tutorial)
Target audienceIntermediate users
DownloadDownload IconTaskSwitcher (13.07 KB)

The basic idea behind this application is the following: upon start, we create snapshot (a list) of all the available windows and we use it to decide what previews to display. Once the list is created, a preview is drawn for each item. There is a drawback for this approach: if new windows are created or some existing are closed the interface will not display them (actually for closed windows will replace the preview with an icon). The advantage: is a simple implementation.

There are three parts for this project.

  1. Enumerating only the open applications’ windows
  2. Make a glass window
  3. Generate a live preview for each window

Enumerating windows

Enumerating all windows can be done with the EnumWindows function from user32.dll which can be easily imported in C#.

[DllImport("user32.dll")]
private static extern int EnumWindows(EnumWindowsCallbackDelegate callback, int lParam = 0);
private delegate bool EnumWindowsCallbackDelegate(IntPtr hWnd, int lParam);

However, the result of a pure enumeration will return hundreds of windows. The most powerful filtration is to keep just the windows that are visible. Again, a function from user32.dll called IsWindowVisible is used to check whether a hWnd belongs to a visible window. Probably, after this step you will have just 30-40 windows left in the list.

[DllImport("user32.dll")]
private static extern bool IsWindowVisible(IntPtr hWnd);

The next step is to decide which is the most meaningful representative window from each cluster of windows related by ownership. The Old New Thing blog presents an algorithm for this problem. The logic behind this algorithm is: “For each visible window, walk up its owner chain until you find the root owner. Then walk back down the visible last active popup chain until you find a visible window. If you’re back to where you’re started, then put the window in the Alt+Tab list.” A few Dll imports and the translation of the pseudocode to C# gives the following code.

Read the rest of this post »

Tutorials questionnaire results

(0 rating, 0 votes)

May 3rd, 2010

After having the results from the tutorials questionnaire, I concluded the following:

  • The opinions around focused vs. complete tutorials are split almost even (56% vs. 44%). However, someone uggested that he prefers focused tutorials (as text) and a download link with the full project. In my opinion this idea is great.
  • Most people prefer tutorials of a difficulty level above or equal to their proficiency level. However, there were a few anomalies  in the results: some that ranked themselves as ‘Advanced’ or ‘Intermediate’ prefer beginner tutorials. This might have two explanations: they prefer beginner tutorials for the other programming fields or they just overestimated themselves.
  • The result for the tutorial format is conclusive: almost everyone wants text + images. Just a few prefer video tutorials.

So, the final result is: intermediate or advanced tutorials, presenting just the essentials parts as text but allowing the download of full source.

Spam, UPS and coincidences

(0 rating, 0 votes)

Mar 17th, 2010

Yesterday I registered on UPS’ website to send a package. Today I received an e-mail called “UPS Delivery Problem NR.1644339?” from an ups.com e-mail address (!) saying that the delivery failed because of inexact address. I was supposed to print the attached form and go to their office to pick my package.

There are a lot of reasons why the e-mail is spam:

  • The e-mail got into the junk folder
  • I was unable to download the attachment because the webmail found a virus in it (good job Hotmail!)
  • The e-mail was saying something about my delivery from January
  • UPS don’t ask you to go their office to pick the package
  • The message was sent to “Victor Harris”

The coincidence is that today the package should have arrived so, initially, I thought is a valid message. There might be a security breach in UPS’ system that allows others to track packages. I never got such an e-mail before registering and sending.

What is even more interesting is that I registered with one e-mail address and I got the spam on another one. Coincidence or consequence?

My Interview with Microsoft

(+2 rating, 2 votes)

Mar 13th, 2010

This post is for those who want to apply or have already applied (but not finished the interview) for a Microsoft Job. The recruitment process is quite similar for everyone and consists of a few steps.

  1. Application
  2. E-Mail Interview
  3. Phone Interview
  4. On Site Interview

I will tell you my story and how I went through the four phases.

1. Application

My blog’s title (Ex Nihilo Nihil Fit) means “Nothing Comes Out of Nothing”. You can’t get a job at Microsoft by not doing anything – this is true for anything else. The first step you need to complete is the application process.

For this, many options are available. You can…

  • … apply online on Microsoft’s Careers website as I did
  • … send your CV to different e-mail addresses (there are some dedicated e-mails for different positions)
  • … apply through some 3rd party organization (job shop, campus recruitment, job agency, etc)

On MS Careers you just have to post your CV and choose the job you want. That’s all! No recommendation letter, no cover letter, no nothing. Of course, not every CV passes the selection process. Here are some tips for improving your resume (worked for me):

  1. Don’t write it just before applying! Write a draft version, wait a few days and then review it. This way you will find a lot of mistakes and stupid things you wrote initially. If you review it immediately after writing, your mind will not be criticism oriented and will just ignore mistakes. Repeat the write-wait-review process as many times as necessary, until you find that the review revealed no mistakes.
  2. After you did the final review and the CV is bullet-proof, ask others to review it. They will definitely find inconsistencies and mistakes and this will make you feel stupid. This is good because will open your eyes will make you go into an ‘I want to improve’ mode. You’ll try to correct everything. After you come up with a modified version go again through steps 1 and 2. Repeat this as many times as necessary. [Special thanks to Lucian Sasu, Nadia Comanici, Andrei Ciobanu, Monica Balan and Lavinia Tanase for reviewing my CV!]
  3. Make it short and give only relevant facts. Initially, I come up with a 5 pages CV because I wrote every single technology with which I worked. There were a lot irrelevant things, I wrote Windows Workflow Foundation just because I played with it for a few days. I added extensive descriptions for every project, made a personal details section (name, birth date, address, etc) of 1/2 page. Others suggested to cut everything that was not necessary. You don’t need to give extensive descriptions, just add a few words. For example, I wrote “VS Image Visualizer – Visual Studio 2008 debug visualizer for images” and added a link to the project’s page – you submit formatted andcan embed links.
  4. Add something that makes it different. I don’t know if this makes a difference, but I added some lines to separate items just like in the picture below. Definitely Microsoft gets thousands of CVs per day. You need something special.
  5. Don’t lie! Tell exactly what you did and what is the proficiency level of your skills. For example, don’t write “Advanced” for UML if you don’t know the difference between composition and aggregation. Be realistic and don’t under/over estimate yourself.
  6. Use the spell chick. Make sure everything is written in correct English and there are no grammar/spelling mistakes. Noddy likes a WC with grammar mi takes. You mght fail just because of that.

Once you completed your CV, choose the job that suits best your needs, apply and wait… The waiting is a problem because all these big companies like Microsoft, Google, Mozilla, Apple, etc. will contact you only if they find something interesting in your application. If you’re not suitable, then no rejection is sent.

I applied for an Intern Software Development Engineer position at Microsoft Redmond. I cannot apply for a full time position because I want to finish the master program on time, in the next summer – an internship is just what I need.

2. E-Mail Interview

January 20, 2010. Two months since I submitted the CV. I wasn’t hoping anymore that MS will contact me, when I got an e-mail titled: “Victor Hurdugaci ES DK” from Holly Peterson saying:

Hi there,
My name is Holly and I work with the Microsoft International Internship recruitment program.
We recently received your CV and would like to consider you for one of our technical internship positions in Denmark in 2010.
[...]
Please respond by the end of the day if possible
[...]

Wow! Now this was a good news. The possible bad side was that the internship was going to take 12 months. This might be a problem. However, it solved really well after talking to my professors. They understood the value of this internship and considered that will be possible to go for 12 months in Denmark and do my thesis there.

The e-mail also contained a set of 15 questions that I was supposed to answer when sending the response. The topic of the questions was not the same. Some asked HR questions like:

  • In what city/country will you be residing in June 2010?
  • Describe your ideal job
  • Have you interviewed with Microsoft before?

Others were a little tricky and technical:

  • How many lines of code would you estimate you personally have written in the last year?
  • How would you test a function that is supposed to calculate the factorial up to 1000?

I tried to be as specific as possible, but still give exhaustive answers, trying to cover all possible uncertainties present in the question’s text. By the way, you can’t send an e-mail back to ask for more details or clarifications. I don’t think I am allowed to post my answers to questions. I will just leave them as homework for you.

Replied the same day (actually the next day at 00:20 in the morning) and I waited again. Now was better because they are going inform me about the decision, no matter if is positive or negative. It was just a matter of time.

You might have more than one e-mail interview. I met someone who had two with less questions.

Few days later, another e-mail arrived. They continue to consider me as a candidate. Someone from Microsoft Development Center Copenhagen (MDCC) will contact me to schedule a phone interview.

3. Phone Interview

This is where it gets interesting. Until now everything was asynchronous and for all questions I had time to think. During a phone interview you have to come up with (almost) instant solutions.

Read the rest of this post »

Life at TUDelft

(0 rating, 0 votes)

Feb 20th, 2010

QRIt is hard to believe that six months passed since I was stepping, worried, out of the terminal in the Schiphol airport, in Amsterdam. Meanwhile I went home once but this is another story. What am I doing at the Technical University of Delft? Well… It’s simple: “I’m busy”. I work, now, on 4 projects, all software related. Mostly sure you are not interested in what I do with distributed algorithms or domain specific languages. So, I’ll present the most interesting project. The QR.

The QR is a Quad Rotor Unmanned Aerial Vehicle developed by another department from the University. We are struggling to do doing the software that will make it fly. The software is made of two parts: one part running on a PC and another running inside the QR. The device is not yet advanced. Currently it has the minimum necessary in order to stabilize in air (gyroscopes, accelerometers and engines). In future, cameras, wireless interfaces and other gadgets will become available.

Today was an important day because we had the first engine test. The first try was not successful but after a bug was fixed we managed to throttle a little the engines (see the movie below). The device cannot fly yet because the stabilization routines are not implemented and no human will be able to control it for more than a few seconds without digital assistance. You may wonder how can one control it: through a joystick that is connected to a PC.

Read the rest of this post »

Part of my plan: Books (Update 1)

(+2 rating, 2 votes)

Feb 16th, 2010

In December I was writing about my plan of reading 16 books. Today I offer a status of the plan and my impression about two books.

From that list I read “The Inmates are Running the Asylum” and “The Design of Everyday Things”. Actually from the last one I read just 3/4 because the the book is too theoretical (psychology) and, even though I like psychology, it was a little too boring much – don’t get me wrong, the author offers valuable information about the things used every day and how simple is/should be the design, but the mix of practical and theoretical has a wrong ratio.

However, the former book is absolutely fascinating. Alan Cooper shows that whatever has a computer is actually a computer (or at least has that complexity). He also shows that programmers tend to increase the complexity of the software in order to make the development easier or, just because they can and see this a challenge. One of the main ideas of the book is that programmer cannot design simple software; they always design software that is supposed to be used by people with a lot of technical knowledge, who enjoy complicated things. Cooper’s suggestion is that developers should create software that is as simple as possible to operate, for the end user and, usually, the design must be made by a designer (a person who is able to understand how an experienced person will act).

Currently, my attention is on “Surely You’re Joking, Mr Feynman”. The book has a nice introduction about the first years of Richard Feynman’s life, but things may change as I progress with the lecture. I postpone any conclusion until I read the last page of the book.

The Game of Life

(+3 rating, 3 votes)

Feb 14th, 2010

A few days ago I was getting bored and decided to to something fun. Yes, you can get bored at TUDelft… Well, actually you don’t get bored, you get sick of all the work you have to do (apologies to anyone offended by this). So in a quest of something fun but still productive I found Conway’s Game of Life.

Game Of Life

The Game of Life is not really a game because no one plays it. It is a cellular automaton, a zero player game in which the only human interaction is the initial setup. Then the game evolves by itself under the constraint of some rules, until it gets to a stable state.

The game’s world is a 2D grid in which each node is a place that can hold one individual. Each individual can be dead or alive. Basically you can see the world as a black, white grid in which black represents living individuals and white dead ones (the figure on right).

Each grid represents a single generation. In order to get a new generation the current one must evolve. This process implies that every individual (dead or alive) node will be evaluated and, based on the result, will survive, die or resurrect. The evaluation function takes in consideration the number of alive neighbors a node has (a node can have up to 8 neighbors):

  1. If one individual has less than two neighbors alive, it will be dead in the next generation because of underpopulation.
  2. If one individual has more than three neighbors alive, it will be dead in the next generation because of overcrowding.
  3. If one individual is alive and has two or three neighbors alive, it will survive in the next generation.
  4. If one individual is dead and has three neighbors alive, it will resurrect (be alive) in the next generation.

That’s all. Based on this transition, each individual gets from one state to another, from generation to generation. There are two situations in which no more changes will be made between generations:

  1. All individuals are dead.
  2. All individuals are in a stable state from which they cannot die. The square on right is a stable population that cannot change because all alive individuals have two or three neighbors and all dead individuals have at most two neighbors alive so they cannot resurrect.

The fun part was to implement this myself. I created a small MFC application that is able to load files with predefined configuration or to generate random populations up to 10.000 individuals. If you download the sample be aware it will crash if you load invalid files and do strange things – it is just a prototype.

Read the rest of this post »

Using UAC with C# – Part 3

(+6 rating, 10 votes)

Jan 6th, 2010

After a long period since I wrote part 2 of this article I decided to add some extra information. There is one thing that was missed by the previous two articles: the design of UAC enabled applications.

If you use Windows Vista/7 then you know that buttons and links which elevate privileges are preceded by a shield icon. This is the way Microsoft decided to inform the user about the effect of clicking that control.

The first idea that might pop up is the reinvention of the wheel (or shield). In other words you could draw the shield on a button. This is OK except that:

  1. Is not easy
  2. Will require you to recompile the interface if Microsoft decides to change the icon
  3. You need the icon in many sizes 16×16, 24×24, 32×32, etc. (extract it from MS’ DLLs)
  4. Will create a lot of overhead with layout (position icon relative to text size/position)

The second method is easier, safer and recommended by MS. All you need to do is send a specific message (BCM_SETSHIELD) to the button if the user has limited privileges and pressing that button will trigger the UAC window. Actually there is a second, tricky, thing that must be done: the style of the button must be “System” (in C# “System.Windows.FlatStyle.System”). Without this you will not be able to see the shield.

The code provided in part 1 of this article will be modified in order to display the shield on the two buttons. Moreover, the shield will be displayed only when the user runs under an account with limited privileges or non-elevated administrator.

In order to display the shield one needs to send the BCM_SETSHIELD (=0x0000160C) message to the button. This can be done by using the SendMessage function from user32.dll. This article will not cover what is and how to use SendMessage, if you need more information about it follow the previous link.

To set the shield of the “Elevate this application” button one needs to send the message in the following way:

SendMessage(btnElevate.Handle, BCM_SETSHIELD, 0, 1);

The first parameter is the handle of the button, the second one is the message, the third one is not used and must be ’0′ and the last argument must be non-zero in order to draw the shield, zero otherwise.

If you try this it will not work :) Remember the ‘tricky’ thing told before? This is the full code to display the shield for btnElevate:

btnElevate.FlatStyle = FlatStyle.System;
SendMessage(btnElevate.Handle, BCM_SETSHIELD, 0, 1);

There is only one thing that must be done in order to work properly. Remove the shield if the user has elevated privileges. I don’t know if this is against MS’ recommendation but in my opinion one must not be shown information that cannot be used in that context; in our case don’t show the elevate shield if there is nothing to elevate.

Part 1 described how to check if a user has full rights. Now we are just using that boolean variable:

if (!hasAdministrativeRight)
    SetUACShields();

Where SetUACShields will send the message to all buttons that require the shield drawn.

The full updated code from Part 1: Download IconUAC Code 3 (10.13 KB)

« Older Posts Newer Posts »