Oriented Button in Windows Forms

imageThe following class is a button which can be oriented both horizontally and vertically (like in the picture of your left). Text and image react properly to this orientation and so do their Alignments. The control also includes customizable margins for the text and image inside the button and a SizePercent property for the Image. Please keep in mind that if Orientation is set to “Horizontal”, the normal Draw methods from the base class are called, so all of this properties are ignored. However, you can change this behavior or add any other property very easily if you need it.
The whole thing has been designed to use as less resources as possible (accept suggestions on this of course ;), but you can very easily add other features, like a PictureBox for the image rendering (re-using all the PictureBox features as: BorderStyle, SizeMode, etc).
You can just copy-paste the following parts of code into a class which inherits from Button, and you´ll have it. Something like this:
public class OrientedButton : Button
Hope it helps:

Declaration of variables and props

        private Orientation mOrientation = Orientation.Horizontal;
        private int mTextMargin = 5;
        private int mImageMargin = 5;
        private int mImageScalingPercent = 100;
        private System.Windows.Forms.VisualStyles.PushButtonState mState = System.Windows.Forms.VisualStyles.PushButtonState.Normal;
 
        #region Props
        [Category("Appearance")]
        [DefaultValue(5)]
        public int TextMargin
        {
            get { return mTextMargin; }
            set { mTextMargin = value; }
        }
        [Category("Appearance")]
        [DefaultValue(5)]
        public int ImageMargin
        {
            get { return mImageMargin; }
            set { mImageMargin = value; }
        }    
        [Category("Appearance")]
        [DefaultValue(Orientation.Horizontal)]
        public Orientation Orientation
        {
            get { return mOrientation; }
            set { mOrientation = value; }
        }
        [Category("Appearance")]
        [DefaultValue(100)]      
        public int ImageScalingPercent
        {
            get { return mImageScalingPercent; }
            set { mImageScalingPercent = value; }
        }
        #endregion

Mouse Events Handling

       #region Mouse Events
        /// <summary>
        ///
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
            mState = System.Windows.Forms.VisualStyles.PushButtonState.Pressed;
            Invalidate();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
            mState = System.Windows.Forms.VisualStyles.PushButtonState.Hot;
            Invalidate();
        }       
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            mState = System.Windows.Forms.VisualStyles.PushButtonState.Normal;
            Invalidate();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            mState = System.Windows.Forms.VisualStyles.PushButtonState.Hot;
            Invalidate();
        }
        #endregion

OnPaint Method

        /// <summary>
        /// Some code parts were taken from here: http://msdn.microsoft.com/es-es/library/f0ys5025.aspx
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
 
            if (mOrientation == Orientation.Horizontal)
                return;
 
            // Base Button Draw
            if (mState == System.Windows.Forms.VisualStyles.PushButtonState.Pressed)
            {
                // Set the background color to the parent if visual styles 
                // are disabled, because DrawParentBackground will only paint 
                // over the control background if visual styles are enabled.
                this.BackColor = Application.RenderWithVisualStyles ?
                    Color.Azure : this.Parent.BackColor;
 
                // If you comment out the call to DrawParentBackground,
                // the background of the control will still be visible
                // outside the pressed button, if visual styles are enabled.
                ButtonRenderer.DrawParentBackground(pevent.Graphics,
                    ClientRectangle, this);
                ButtonRenderer.DrawButton(pevent.Graphics, this.ClientRectangle,
                    "", this.Font, true, mState);
            }
            else
            {
                // Draw the bigger unpressed button image.
                ButtonRenderer.DrawButton(pevent.Graphics, ClientRectangle,
                    "", this.Font, false, mState);
            }
 
            // Draw Text
            if (this.Text != "")
                this.DrawText(pevent.Graphics);
 
            // Draw Image
            if (this.Image != null)
                this.DrawImage(pevent.Graphics);
        }

The DrawText method

        /// <summary>
        ///
        /// </summary>
        private void DrawText(System.Drawing.Graphics pGraphics)
        {
            // Calc size of text (la func devuelve el size horizontal)
            SizeF sizeOfText = pGraphics.MeasureString(this.Text, this.Font);
            float temp = sizeOfText.Width;
            sizeOfText.Width = sizeOfText.Height;
            sizeOfText.Height = temp;
 
            // Calc X coord of Text           
            float x = mTextMargin;
            switch (this.TextAlign)
            {
                case ContentAlignment.MiddleCenter:
                case ContentAlignment.TopCenter:
                case ContentAlignment.BottomCenter:
                    x = (this.Width / 2) - (sizeOfText.Width / 2);
                    break;
                case ContentAlignment.MiddleRight:
                case ContentAlignment.BottomRight:
                case ContentAlignment.TopRight:
                    x = this.Width - mTextMargin - sizeOfText.Width;
                    break;
            }
 
            // Calc Y coord of Text
            float y = mTextMargin;
            switch (this.TextAlign)
            {
                case ContentAlignment.BottomCenter:
                case ContentAlignment.BottomLeft:
                case ContentAlignment.BottomRight:
                    y = this.Height - mTextMargin - sizeOfText.Height;
                    break;
                case ContentAlignment.MiddleCenter:
                case ContentAlignment.MiddleLeft:
                case ContentAlignment.MiddleRight:
                    y = (this.Height / 2) - (sizeOfText.Height / 2);
                    break;
            }
 
            // Draw text
            System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(this.ForeColor);
            System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
            drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
            pGraphics.DrawString(this.Text, this.Font, drawBrush, x, y, drawFormat);
            drawBrush.Dispose();
        }

The DrawImage Method

        /// <summary>
        ///
        /// </summary>
        /// <param name="pGraphics"></param>
        private void DrawImage(System.Drawing.Graphics pGraphics)
        {
            float imageScaling = (float)mImageScalingPercent / 100f;
            float finalWidth = (float)this.Image.Width * imageScaling;
            float finalHeight = (float)this.Image.Height * imageScaling;
            float halfFinalWidth = finalWidth / 2f;
            float halfFinalHeight = finalHeight / 2f;
 
            float x = mImageMargin;
            float y = mImageMargin;
            switch (this.ImageAlign)
            {
                case ContentAlignment.MiddleCenter:
                case ContentAlignment.TopCenter:
                case ContentAlignment.BottomCenter:
                    x = (this.Width / 2f) - halfFinalWidth;
                    break;
                case ContentAlignment.MiddleRight:
                case ContentAlignment.BottomRight:
                case ContentAlignment.TopRight:
                    x = this.Width - mImageMargin - finalWidth;
                    break;
            }
            switch (this.ImageAlign)
            {
                case ContentAlignment.BottomCenter:
                case ContentAlignment.BottomLeft:
                case ContentAlignment.BottomRight:
                    y = this.Height - mImageMargin - finalHeight;
                    break;
                case ContentAlignment.MiddleCenter:
                case ContentAlignment.MiddleLeft:
                case ContentAlignment.MiddleRight:
                    y = (this.Height / 2f) - halfFinalHeight;
                    break;
            }
            System.Drawing.Drawing2D.Matrix rotMat = new System.Drawing.Drawing2D.Matrix();
            PointF rotationCenter = new PointF(x + halfFinalWidth, y + halfFinalHeight);           
            rotMat.RotateAt(90, rotationCenter);
            pGraphics.Transform = rotMat;
 
            System.Drawing.Rectangle destRect = new Rectangle((int)x, (int)y, (int)finalWidth, (int)finalHeight);
            System.Drawing.Rectangle srcRect = new Rectangle(0, 0, this.Image.Width, this.Image.Height);
            pGraphics.DrawImage(this.Image, destRect, srcRect, GraphicsUnit.Pixel);
        }

References

http://msdn.microsoft.com/es-es/library/f0ys5025.aspx

You duplicating? You wrong…

This is not the best example for the issue, but here we go: imagine your are going to sweep your house, and that you have one of those modern brooms that can be disassembled to fit into small broom cupboards. When you start, let’s say in the kitchen, you take the broom from the cupboard, assemble it, and start sweeping. When you are done with the kitchen, you don’t disassemble it and place it into the cupboard, because you have other rooms left, do you? You would feel stupid assembling and disassembling the broom for each room, wouldn’t you?
Then why you do that when you write software?

First, some misprints of the writer

Thanks to M. Barnett for pointing out the “mounting and dismounting broom” issue. It seems that I wrote this post too fast (or too late), and I didn’t notice that “mounting a broom”, in English is more something like this:

So I changed it to “assemble” and “disassemble” which is what I meant to write the first time, I promise… ;)

Intro

This post is about some very basic thoughts, about engineering behaviors students tend to forget… It might sound obvious for experienced people, so if you are one of them, better go get a beer with friends instead of reading it. So, back to what students forget. Among others, one of the most important things to keep in mind, one that will always guide you through the right path is:
DUPLICATION IS THE SOURCE OF ALL EVIL IN THE WORLD
And this applies not only to software development, but also to content management, organization and engineering in general. Damn! it even applies when you tiny up a wardrobe or clean the kitchen, as we saw in the preface.

Why is this misbehavior so usual in software?

Easy. Assembling a broom and taking it to the cupboard takes time, and therefore it is trivial for any dumb-ass to see that’s a waste of time. In software, many times it takes less time duplicating things than thinking how to avoid the duplication. It’s just a matter of Copy-Paste, when avoiding duplication can take weeks of work. That’s why duplication appears all around in bad software.

The student counter part

At this point, it’s when students tend to say:
- <<Weeks! then it is indeed better to duplicate code than loosing weeks>>
And then you play the role of the old, wise grandpa, telling them:
- <<Believe me, it’s better this way. You will thank it, when you grow older…>>

Example 1: Duplication of work - Duplication of decisions

Chronicles of a bad developer

  • Company “A”
  • Project “B”
  • You are the “Bad developer #1”
  • A simple operation: obtain the concatenated name of a user, composed as: “Name” + “Second Name” + “Last Name”
It’s just one line of code, so you come to the conclusion that it’s not worthy to think much about it. So, you leave it like that and copy-paste the line when you need it.
Some months later, project “B” has grown, becoming “big”, with let’s say 150 sub-projects. Your famous name composition operation is scattered all around, in more than 1500 places. Now imagine that the worst happens (the worst always happens), and that your boss demands a change in the way you are concatenating names. Let’s say, he wants it in the form: “Last Name, First Name”, instead of the previous. You are screwed, easy as that.
As you are a bad bad developer, first thing you say is:
- <<Boss, are you sure we need that change? I like names in the way they are right now>>
- <<Our clients don’t>> He replies…
No prob, you are a bad, but clever developer, so you think:
- <<Visual Studio will fix this, I heard somewhere that there’s a thing for these cases called Refactoring>>
Then you try to Refactor a whole sentence, and of course, it doesn’t work.
- <<Damn Visual Studio! Damn Microsoft! For things like these is why I prefer Linux! Sure that Linux knows how to Refactor a whole sentence>>
And then, you end up making massive text replacements, investing one week trying to find all the instances of the operation, and in fixing the resulting compilation errors. Of course, you forget to change 3 or 4 instances of it. That’s when you go to your boss again and say:
- <<Hey, not that bad. 3 out of 1500, that’s a 99% of success!>> 
- <<You are a 99% of an idiot>>… He replies…

Diary of the good developer

<<Monday, 36 of October of year 2124
Today, I slept well. I had breakfast and went to the office. My job for today was changing the way names are concatenated in our software. As I was clever enough a couple of months ago, and I isolated this task inside a method, all my job for today has been done in 10 secs. The rest of the day I was in the beach, getting some sun… No phone calls from my boss. No phone calls from clients… No news is good news. Life is wonderful…>>
You learned the lecture?

What lecture?

Pay attention Padawan to the following line of code:
FullName = Name + SecondName + LastName;
It doesn’t only make a name composition. It also decides how this concatenation will be done, and this is the key point. That’s what you need to understand and learn. Because duplicating work is bad, but duplicating decisions is one of the worst things you can ever do in software. I´ll put that in big so you remember it:
Remember: Duplicating work is bad, but duplicating decisions is one of the worst things you can ever do in software

Example 2: Duplication of Contents

As we mentioned, duplication is not only a software development issue.
Copy-pasting files is slower than copy-pasting code, but for sure it’s still faster than assembling and disassembling the broom ;)
That’s why many dumb-asses still don’t get it, making duplication of contents to be pretty frequent too. It’s the same stuff. You make a texture, you use it in hundreds of 3D models, and then one day you need to change it. You are screwed again man!
How to avoid duplication in this cases?
You should think about having global repositories for contents that are shared between many entities: texts, textures, sounds, xml files, whatever. Remember, duplication is the source of all evil in the world. If you find yourself doing a Copy-Paste, ask yourself if it is really necessary or if there’s a way to avoid it.

Example 3: Other, not so obvious, forms of Duplications

Imagine we have a game with the class Player, and with a property which holds his status. You know, things like “Dead”, “Alive”, “Running”, etc. At design time, we need to choose how to store that status:
  • The core C++ kings that want his player to be dead and alive at the same time (may be a zombies game?), will want his status to be an Hexadecimal Flag
  • Those who want their game to be fast and “cross-platform with their toaster”, will want his status to be an Int, you know: 0, 1, 2, etc
  • A Visual Basic developer will probably want it to be a String, actually writing the value “Dead”, “Alive”, etc.
Anyone is right? Nope (unless special circumstances). Let’s debate

Speed vs Comprehensiveness

- << Who said to use strings? Oh yes, the VisualBasic people… Come on man, do you know how slow is to compare two strings? Player is a core class, that is used all along the game, even in performance critical parts. You cannot be serious… strings… The solution is to use an Int, or an Hex flag.>>
- <<Who said to use an Int or an Hex Flag? Of course! The native people. Why not we all just turn back into binary, you prehistoric monkeys? Will we need to check what “State = 0” is every time we need to use it?  Man you cannot be serious… ints… The solution is to use strings…>>
As you probably already notice, one can stay like that forever, when there’s a much better solution… Yes, you clever boy, we will use Enumerations. But, do you know why? And what is more important, are you aware of all the benefits (and cons) of an Enumeration? Will talk more on this later, by now lets get deeper in the cons of the other alternatives.

Anyone said duplication again?

We have seen duplication of work (bad), and duplication of decisions (very bad). What none of the contenders in the previous discussion have seen is that both solutions imply a new, hidden form of duplication. It’s what I call: duplication of abilities.

Duplication of Abilities (also very bad)

When you choose to store the status of your player as an Int, or as a String, with values like “Dead” or “Alive”, you are delegating in all your developers the responsibility of writing that values correctly. In other words, you are duplicating the need for the ability to write it correctly in many many different places, and by many different people, instead of centralizing in a single point.
It’s quite likely that, from time to time, someone makes a mistake. So if any of them misspells a string value (“Deaz” instead of “Dead”), or simply misplaces an int, writing “1” instead of “0”, the compilator won’t say a thing, and the application will seem to run just fine. But... What are the effects of setting the status of a player incorrectly in a single, deep point of your code? Absolutely unpredictable. That can manifest as any kind of error in the application, what makes it very difficult to debug, trace and fix.
You must try to avoid this kind of duplication too, centralizing decisions, and reducing the number of places where a mistake can be made. 

Strong-type, baby

We said it before, but let’s just analyze another alternative for storing the “State” of the player in our game. What about Enums?
  • Enums are as fast at runtime as an Int or an hex flag
  • Enums are as comprehensible as Strings at debug time
  • Enums are convertible to and form Int, what makes cross-platform easy
  • Enums are convertible to and from String, what makes printing information easy
  • Enums are much more confortable to use, thanks to the Intellisense, you don´t need to remember the values, nor type them, just select them on the list
  • Enums are strongly typed, which will:
    • Minimize the so-called Duplication of Abilities. Since you select values on a list (thanks to the Intellisense) instead of typing them, you centralize the responsibility of making no mistakes in the enumeration
    • Even if any one ignores the Intellisense, and types the values manually, the presence of “hard to find” errors is also minimized, as a miswritten Enum value will be detected at compilation
So, YES! Enums ROCK! And thanks to the static helper methods found in the .Net Framework (Enum.Parse, Enum.GetNames, Enum.GetUnderlyingType, Enum.GetValues, Enum.IsDefined, etc), they are very versatile too.
So… Use them!!!

An additional security shield

Even if you are already strong-typing, there’s another thing you should always do, specially in big projects that cannot be tested completely too frequently… exactly, that’s correct: UNIT TESTING.
Again, designing good unit tests can take weeks, and again, students will say: <<Weeks! The it’s better not to use unit tests>>, and again you will have to tell no no no…
Unit test are the definitive way for giving big projects the robustness they need, going into places where the compiler can’t go, and testing deeper that just syntax. A unit test can detect a wrong player State, but of course, it has to be well designed. If you want to know more about unit testing, please refer to the following article by Roy Osherove:

Conclusion

Every time you find yourself doing a Copy-Paste, ask yourself:
  • Why are you doing it
  • Is it really necessary to duplicate what you are duplicating
  • Can it come to problems if something changes in the future
  • Is there any way of avoiding that duplication
Try to find implicit or hidden duplications, that can be problematic in the future. Try to reduce the number of places where mistakes can be made. Try to reduce the number of people that can make mistakes (and I mean something deeper than firing dumb-asses!). Strong-type and Unit-Test when possible!
Finally, apply some common sense to all of this. Sometimes, you should do just the opposite to what I´m telling… ja ja… ;)
Take Care!

C# 3.0 Method Extenders

“Method Extenders” is one of the most useful features introduced in the third version of .Net, which allows us to extend the features of a pre-built class. This small post will show you why you want them, how to use them, and the different versions of .Net you will need to deploy.

Why you want extension methods? Just an example

Imagine a collection of the type HashTable or Dictionary. It is sometimes convenient to retrieve ANY element of the collection. You don’t want the first, you don’t want the last (it would be a non-sense talking about order in a hash table). You don’t want any element in special, just one. The default HashTable or Dictionary collections don’t have a GetAnyElement() method, so you would typically iterate through the collection getting the first element you find.
If this operation is done frequently over your code, and you are a clever developer, you won’t be duplicating the same piece of code all around, and you will end up with some sort of static code like this:
        public static string GetAnyValueOfDictionary(Dictionary<string, string> pDictionary)
        {
            foreach (string value in pDictionary.Values)
                return value;
            return "";
        }
This is nice, but… Wouldn’t it be muuuuuuch nicer if that method was shown as an instance method (automatically recognized by the IntelliSense) instead of a static one inside a different class?
That is what extension methods give you. Pretty much like ExtenderProviders do at design time, adding properties to controls, ExtensionMethods tell the environment that the GetAnyValue() method is in fact a part of the specific Dictionary instance, so the invocation to the method is through each instance, not through an additional class.
The following is the old way of doing this stuff. Create a static class with some utility methods, and invoke them:
            // Old fashioned
            Dictionary<string, string> myDictionary = new Dictionary<string, string>();
            string anyValue = DictionaryUtils.GetAnyValueOfDictionary(myDictionary);
This forces you to remember that there’s a DictionaryUtils class holding that code. Not anymore… This is the new way:
            // Extension Methods
            Dictionary<string, string> myDictionary = new Dictionary<string, string>();
            string anyValue = myDictionary.GetAnyValue();
Fancy uh?

What do I need to use them?

Theoretically, Extension Methods were introduced in .Net 3.0. However, if you try to use them directly, your compiler will say that needs a reference to System.Core.Dll. If you add that reference, you will realize that in fact is a .Net 3.5 dll. So, here you can follow two different paths:
1.- Add a reference to System.Core.Dll, deploy your application as .Net 3.5, and jump to the next chapter
2.- If deploying your application as .Net 3.5 is not an option, you can always add the following code snippet:
namespace System.Runtime.CompilerServices
{
    public class ExtensionAttribute : Attribute { }
}
This will allow the compiler to work with Extension Methods even in a .Net 2.0 application, as this blog post explains deeply.

How to write them?

Now you are ready to write your own extenders. Let’s see the example code for the GetAnyValue extension method:
 public static string GetAnyValue(this Dictionary<string, string> source)
        {
            foreach (string key in source.Values)
                return key;
            return "";
        }
As you can see, Method Extenders are written as static methods (inside a static class). Of course, you need a reference to the calling instance object, that’s why the infrequent word “this” appears in the parameters declaration.
The Type you specify on the right of the “this” keyword will state the kind of objects this method extends. Once you have the code, just add a “using” sentence (to the namespace you used for your “extenders” class) in every source file where you want to use them. Et voilĆ”! Method extended… Easy as that:
image
Of course, you can combine this with Generics (using the <T> generic type), to make things even more functional.

Another useful example: A WordCount method for the string type

In this MSDN page, you can find another useful example: a WordCount method for the “string” type.
            public static int WordCount(this string str)
            {
                return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
            }
Of course, you can put there your own split char preferences, but it’s indeed a handy method, isn’t it?
Take Care!

Detecting if a piece of code is running in Design Time

Sometimes, you will want to distinguish if a certain piece of your code is being executed at run-time or as a part of the Visual Studio Design-Time calls.

A very easy way of checking this is:

bool isDesignTime = (this.Site != null && this.Site.DesignMode == true);

Why to explain this?

This is something quite obvious, but some people ask in the forums why they are receiving an exception when trying to open the design view of a form, in Visual Studio. Those exceptions can be caused by many things, but the one explained here is one of the frequent reasons.

Use case

Imagine you have developed a custom UserControl class, which needs to do something everything it´s made visible. You will probably override the OnVisibleChanged event to perform that operation. Now, imagine that operation can only be done at runtime. For example, if you need in that operation some data to be loaded, data that won´t be available at Design-Time. Now, imagine that you insert that UserControl into one of your forms.

As you can probably imagine, each time you open that form in the Design View of VisualStudio, the overriden OnVisibleChanged event will be fired, and as that data is not available, you will receive an exception, ruining the initialization of the DesignView, and banning you from editing anything at design time in your form.

In such cases, using the above check will save you from this behavior. Just put that check in the OnVisibleChanged event, and use the mentioned data only if not in Design Time. Et voilĆ”. You have design view operative again.

Cheers!

Fixing the “nVidia Control Panel gone”, the “System has not been modified” issue, and other nVidia drivers installation problems

Today, coming from nowhere, my nVidia control panel stopped working. It is not the first time that happens to me, so I did not worry so much. Things got worse when I rebooted the machine, and saw that my Windows lost the entire nVidia Display Driver. The strange thing was that it didn´t switch back to the VGA adapter. Instead, in the adapter settings you could just read: “Plug&Play monitor in “ and a very scaring whitespace behind. Things got even worse when I saw that, no matter which version I tried, the nVidia display drivers refused to re-install again.

Honestly, I saw myself re-installling windows (something I haven´t done in a long time, I must admit), but as one grows older, accumulates more and more “essential” applications that would take ages to re-install. And now I simply didn´t have the time, so I started investigating. It has taken me the whole afternoon and evening, so hope it helps someone else as well…

Things I tried

Reading forums and forums, I think I tried everything:

  • Change the regional settings before trying to install, as it seems some drivers presented issues with non-U.S. regional settings
  • Uninstall all nVidia drivers, reboot in safe mode, and install the new ones there
  • Use driver cleaner applications, like Driver Sweeper (from Guru3D) or Driver Cleaner Pro, to check if I had wrong, partial installations which refused to remove. Be careful with this kind of applications however. It´s pretty easy that they screw up your PC.
  • I tried switching back to the most recent Restoring Point, but the effects appeared again.
  • And many others…

All of them with no luck.

What was finally the cause of all evil

In one of the previous steps trying, I noticed that one of the nVidia applications installed (the nVidia PhysX Plugin for MAX), refused to un-install using the standard method. Luckily, it gave a bit more of information: “Access denied trying to access the key HKLM\Software\NVIDIA Corporation\PhysXPluginsForMax”. That caught my attention, as it was obvious that there was a problem with the registry.

Opening Regedit.exe, and navigating to that key, I realized that was not the only nVidia registry key with “Denied Access” problems. You just couldn´t read or modify the contents of keys like: “Global”, “Installers”, or “PhysXPluginsForMax” itself. The only thing you could do with those keys was seeing their security permissions.

The funny thing was that when selecting “Permissions”, you could see a message like the following:

image

Translation: “You don’t have permissions to read the actual permissions of key “XXX”, but you can make changes in the permissions”. Apart from the fact that seems a tongue twister, it seems a joke. You cannot read your permissions but you can write them? May be it’s a translation bug in the Spanish version of Windows Vista, because you actually can see the permissions, but cannot change them (much more logical).

After opening the window, you will find something like this:

image

The main problem here is that the box with the label “Names of groups or users” is empty. It means that no-one can actually access this key, and that´s why the Control Panel and the Display Drivers installers are failing.

Note: If this also happens to you, you would worry about what is happening in your system, as having that keys this way is a serious security problem, as any user could get control of them. Please read the conclusions chapter, about what can lead to this situation

Other Symptoms that could lead to the same cause

  • nVidia control panel gone
  • nVidia display driver gone
  • Some nVidia applications refused to un-install
  • nVidia display drivers refusing to install
  • Some nVidia applications refusing to install
  • nVidia display drivers showing the message: “The system was not modified”
  • Others.

How to fix it

Obviously, the way to fix it is just re-assign users permissions to those keys. But how to do that if you don´t have permissions to change the permissions?

Thankfully, one day God thought it was a good idea to create Mark Russinovich and David A. Solomon, the two genius behind SysInternals. Mark also created PS Tools, a suite of tools that help managing Windows. One of the small apps distributed with PSTools is PsExec, which can run processes remotely, and is able to run them inside the SYSTEM account, instead of the current user. And that’s the key to be able to change the registry in this circumstances.

So:

  1. Download PSTools
  2. Run the Command Prompt in Administrator mode (Start->Programs->Accesories->Command Prompt with the right button->Run as Administrator)
  3. Navigate to the folder where you downloaded PSTools
  4. Run the RegEdit process from the SYSTEM account, and allowing it interact with the windows desktop, typing:

“psexec –s –i regedit.exe”

Regedit will open, but this time from the SYSTEM account, where you should be able to change those keys permissions. Now:

  1. Go to each key you want to change (do this at least for all nVidia keys), adding where necessary the usual users or groups to each key: your own user, SYSTEM itself, Administrators, etc.
  2. Grant to those users permissions to read and write in those keys
  3. Close RegEdit.
Now, you should be able to un-install and re-install everything again, and your drivers won´t complain anymore.
 

Conclusions

 
As I mentioned, having those keys with no users is a serious security problem. But, who is responsible for leaving them like that? A virus? I honestly don´t know, and I´m going to investigate it.
 
In my case, I think it is more due to a too dirty computer than a virus. But who knows! If anyone of you can give me a hand with this, it would be much appreciated.
 
Cheers!

Moyea PowerPoint to Video converter. An amazing, essential tool

Have you ever tried to add a simple logo at the beginning of a video, with just a fade-in, fade-out effect? What software would you use for such a task? Adobe Premiere? Combustion? May be, but for such a simple task, there is a much better (and cheaper) solution: Moyea ppt to video converter.

Convert PowerPoint to video, PowerPoint to avi, mp4 - Moyea PPT to Video Converter

This small application takes a Power Point presentation and converts it into a video (avi, divx, xvid, dvd, and many others), in two simple steps, keeping all the ppt animations, videos inside, etc. I have tried it today and have worked flawlessly with a Power Point 2007 (pptx) presentation.

I find it specially useful for (among many other things): easy credits making, logo animations, fast video joining, etc. It´s also very useful the “Save as VOB” feature, allowing you to create DVDs with no need for additional authoring software. You can try the TRIAL version (adds a watermark to the video) and buy it if you like it (it´s less than 50 US$).

I love this kind of tiny, but extremely useful applications, which cost a few money but can save you a lot of time.

Cheers!

Cómo desactivar el molesto lector de SMS en modo conversación en tu Samsung Omnia

Desde que lo compré, estoy bastante contento con mi Samsung Omnia, aunque siempre me molestó algo que traía configurado de serie: el lector de SMS en modo conversación.

Lo que hace este modo es guardar relaciones entre SMS, de forma que el telƩfono identifica (mƔs o menos) conversaciones enteras. Asƭ, cuando abres un SMS, no solo muestra Ʃste, sino que muestra tambiƩn los anteriores y sus posibles respuestas.

La idea no es mala, pero lo cierto es que en cuanto tu número de SMS crece un poquito, la cosa se ralentiza mucho. Y si ya de por sí el Omnia (o su relación con Windows Mobile) es lento (no entiendo cómo puede serlo con semejante pepino de procesador), con este modo abrir un SMS podía llegar a costar unos interminables 2 o 3 segundos. No obstante, he de reconocer que hasta ahora nunca había borrado mis SMS, y ya acumulaba mÔs de 800 (entre enviados y recibidos).

En fin, a lo que vamos: cómo desactivar este modo, y leer los mensajes como toda la vida.

Aunque he encontrado mÔs de un método (algunos de ellos editando el registro), parece ser que con algunos de ellos aparece algún efecto colateral, como la pérdida del aviso sonoro en la recepción de un SMS. Así que de entre los métodos encontrados, me he quedado con el siguiente (a mi me ha funcionado a la perfección).

  1. DescÔrgate la aplicación: SK Tools. Aunque es un magnífico producto que te recomiendo compres para mantener al día tu móvil, con la versión TRIAL te bastarÔ para lo que vamos a hacer hoy. En esta pÔgina tienes los links de descarga. Escoge la opción para WM5 o WM6, en el formato que prefieras (Zip o CAB).
  2. InstÔlala en tu Omnia (te recomiendo resolución VGA) y lanzala
  3. Ve a la sección “Tune Up” en el menĆŗ principal
  4. Una vez allí, navega con las pestañas de la parte inferior, moviéndote hacia la derecha hasta que encuentres la opción llamada: PIM
  5. De entre todas las opciones, en la parte baja encontrarĆ”s una llamada: “Disable threaded conversation view (SMS)”
  6. Pulsa sobre ella para cambiar su valor (en la parte superior aparecerÔ un combo de selección). Pon el valor YES.
  7. Resetea tu Omnia

Et voilĆ”, threaded conversation is off.

MysticThumbs, a good thumbnail generator for your explorer

Are you (like me) one of those that extensively use pictures and textures, in several formats? Then you are interested in this product…

I used to have installed the nVidia DDS Thumbnail viewer to be able to see thumbnails of my DDS textures in the explorer. Also used to have another application for TGA files, etc. Apart from the fact that I was still missing some formats, like Photoshop PSD, one day, without notice, my TGA and DDS viewers starting to collide. I could only make one of them working.

Struggling with that problem, I found the MysticThumbs viewer: an application that combines viewing of all the usual formats, including PSD. In addition to that, it has versions for both x86 and x64, something not too frequent in this kind of viewers.

I have tried it for a couple of days, and seems to work pretty well. I´d suggest you to give it a try.

Link:

http://mysticcoder.net/mysticthumbs/

Cheers!

.Net Compilation, registry accessing and application deployment for both x86 and x64 environments

What a long title, isn’t it?
That’s because, in some cases, there are a bunch of things related to make your .Net applications ready to work in both x86 and x64 environments. Specially if you need to use some unmanaged code or access the registry.
Let´s start!

Compiling and running assemblies

Native code (C++, etc) is compiled into platform-specify binary. That´s why you need to specify the target platform to be x86 or x64…
In .Net, things are a bit different. As you probably know, your VB or C# code will not be compiled into binary, but into MSIL. It´s the JIT Compiler (Just-in-time) who translates this intermediate code into binary, at load time. During this process, it can decide to compile it into 32 bits code or into 64 bits code, to adapt it to the underlying O.S., basing on it’s bitness.
That´s the reason why, in .Net, you can choose not to specify the target platform of your assembly, by choosing the option “Any CPU”. The following picture is a snapshot of VisualStudio project’s properties page, where you select the target platform.
image
Then, why would I want to specify a target platform? Isn’t “Any CPU” the best option always?
Most of the times, yes, as developers usually build pure .Net applications, with no dependency on unmanaged code and no further optimizations, so “Any CPU” would be the best option, letting the JIT compiler decide.
But in there are some situations where you will want to specify the target platform. Some examples:
  • If you use any unmanaged code (which is platform-specific). You will have be careful with this, as your application could fail to load if the JIT compiler decides to put it into a mode not compatible with your unmanaged assemblies.
  • If you want to optimize your assembly for an specific platform. For instance, if you need an application to access huge amounts of RAM memory, you will probably want to make it 64 bits only.
Also, there are other issues related to the target platform that you should be aware of, like registry accessing. We will talk about this later.
This blog post explains target platforms a bit further.

Platforms compatibility

  • If my assembly is compiled as Any CPU:
    • Will it run in a x86 environment –> Yes (JIT compiles it into x86 binary)
    • Will it run in a x64 environment –> Yes (JIT compiles it into x64 binary)
  • If my assembly is compiled as x64 only:
    • Will it run in a x86 environment –> No
    • Will it run in a x64 environment –> Yes
  • If my assembly is compiled as x86 only:
    • Will it run in a x86 environment –> Yes
    • Will it run in a x64 environment –> Yes (In WOW64 Mode)

WOW64 Mode

As backwards compatibility is good for selling licenses, Windows 64 bits offer a compatibility mode for 32bit applications. It´s the so called WOW Mode (Windows on Windows).
Have you played videogames on your PC with a machine emulator, like MAME or a Spectrum emulator? Well, this is something similar. WOW64 is a 32bit emulator that runs on 64bits machines, allowing 32bit applications to run in such environments.
Although we will talk about some issues regarding WOW64 later, you can find more details about WOW mode here.

Detecting the bitness of a “Any CPU” process

What happens if you compiled your assembly to “Any CPU”?
If you know that it will always run in a (modern) x86 environment, you have no doubt about this. Your process is 32 bits.
But what´s its bitness if it can run in both x86 and x64 environments? Even further, if it´s in a x64 O.S., what bitness mode will the JIT compiler decide for it? Normally x64, but… better make sure. How to do this? Extremely easy:
                if (IntPtr.Size == 4)
                    return eArchitecture.x86;
                else if (IntPtr.Size == 8)
                    return eArchitecture.x64;
System.IntPtr is designed to hold a memory address, right? And in .Net, it has a very convenient property that tells us its size. So, if this size is 8 bytes, then we have 64 bits addresses, if it´s 4 bytes, we are in a 32 bits environment. Easy as that.
Please note: If your assembly was compiled as x86, its bitness will always be 32 bits, as even when running in 64-bit environments, it will do in WOW64 mode (emulated 32 bits), and its bitness will remain 32.

Windows 64-bit infrastructure

As mentioned above, Windows 64 bits offers the WOW64 mode, to allow running x86 applications.
Microsoft engineers decided not to mix up 32bits and 64bits applications inside your hard disk, so x64 applications are installed by default in the “Program Files” folder, as usual, and x86 applications are by default installed in a new “Program Files (x86)” folder.

The Windows x64 registry

In Windows x64, WOW64 mode offers separate logical view of the registry for 32-bit and 64-bit applications. It´s like if they were accessing to two separate registries. This is something important to take into account when developing for x64 and when deploying applications, as x86-targeted Setup Projects will also access differently to the registry.
Basically, this works in the following way: Windows intercepts registry calls made by assemblies and applications, and redirects them to the appropriate logical view of the registry, being this completely transparent to the application. More info about registry redirection here.
Although this naming convention is not 100% correct, we will call this two separate logical views as “the 32-bit registry” and “the 64-bit registry”.

Application deployment with Visual Studio setup projects

If you, like many other people, use Visual Studio setup projects and Windows Installer to deploy your applications, there are several things that you need to take care of, if your software needs to be both x86 and x64 compatible.

Target platform in VisualStudio Setup Projects

Just like normal assemblies, setup projects also have a target platform property. You can see it in the properties tab, when you select your project in the solution explorer.
image
Unfortunately, we don´t have the “Any CPU” option here, so we will have to choose the platform manually. This platform selection affects the following aspects (among others):
  • The default installation folder for your application: “Program Files”, or “Program Files (x86)”
  • If the setup project modifies the registry, it will affect the so-called-by-us “64-bit registry”, or the redirected “32-bits registry” for WOW64 processes. This is something important to keep in mind if you are using registry keys in your setup project.

So, which platform should I choose for the Setup Project?

It depends on how well your application is prepared for x64, and on how it is compiled:
  • If your application is compiled as x86: choose x86
  • If your application is compiled as x64: choose x64
  • If your application is compiled as “Any CPU”:
    • If you know it is prepared to be run as x64 (i.e. registry accessing code is aware of WOW64 mode): choose x64
    • If you cannot assure that your application will deal fine with x64 infrastructure: choose x86

Dude, where are my registry keys?

As mentioned above, if your Setup Project is compiled as x86, it won´t modify the “64-bit registry”, but the 32-bit, redirected version, provided by the WOW64 mode. So, if you open regedit.exe normally (which shows you the “64-bit” version of the registry), you won´t find anything.
Instead, you need to open the special version of RegEdit which shows you the “32-bit registry”, using:
[Windows Installation path]\SysWOW64\regedit.exe
Please note: Windows Installation Path is normally “C:\Windows”.

Accessing the registry from C#

Now that you understand how Windows x64 infrastructure is designed, and how your applications will be installed by Setup Projects, you will probably need to learn how to deal with both versions of the Windows x64 Registry. Specially if your application will be compiled as x86, and therefore run in WOW64 mode.

The usual, .Net way to access the registry

Imagine you want to read a certain key inside the “LOCAL_MACHINE\Software” node of the registry. In .Net, you normally do that by using a code similar to the following (using namespace: Microsoft.Win32):

RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\[Your Key Here]");
 
Doing so in a Windows x64 machine will only access the “64-bit” version of the registry, even if your process is running in WOW64 mode. So, if our application was installed with a x86 Setup Project, the keys we are looking for won´t be there. We need to access the “32-bit registry” instead, provided by the WOW64.

Accessing the 32-bit, WOW64 registry from C#

Unfortunately (and by now) there is no way in .Net to specify that we wont to access the “32-bit”, WOW64, version of the registry. To do so, we will have to deal with native API calls, dll-importing “advapi32.dll” and using the RegOpenKeyEx.
This function has a parameter called samDesired, which can be a combination of flags specifying access options for the key. One of those possible options, is specifying we want to open the WOW64 view of the registry.

Table of flags for samDesired

Value Meaning
KEY_ALL_ACCESS (0xF003F) Combines the STANDARD_RIGHTS_REQUIRED, KEY_QUERY_VALUE, KEY_SET_VALUE, KEY_CREATE_SUB_KEY, KEY_ENUMERATE_SUB_KEYS, KEY_NOTIFY, and KEY_CREATE_LINK access rights.
KEY_CREATE_LINK (0x0020) Reserved for system use.
KEY_CREATE_SUB_KEY (0x0004) Required to create a subkey of a registry key.
KEY_ENUMERATE_SUB_KEYS (0x0008) Required to enumerate the subkeys of a registry key.
KEY_EXECUTE (0x20019) Equivalent to KEY_READ.
KEY_NOTIFY (0x0010) Required to request change notifications for a registry key or for subkeys of a registry key.
KEY_QUERY_VALUE (0x0001) Required to query the values of a registry key.
KEY_READ (0x20019) Combines the STANDARD_RIGHTS_READ, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, and KEY_NOTIFY values.
KEY_SET_VALUE (0x0002) Required to create, delete, or set a registry value.
KEY_WOW64_32KEY (0x0200) Indicates that an application on 64-bit Windows should operate on the 32-bit registry view. For more information, see Accessing an Alternate Registry View.
This flag must be combined using the OR operator with the other flags in this table that either query or access registry values.
Windows 2000: This flag is not supported.
KEY_WOW64_64KEY (0x0100) Indicates that an application on 64-bit Windows should operate on the 64-bit registry view. For more information, see Accessing an Alternate Registry View.
This flag must be combined using the OR operator with the other flags in this table that either query or access registry values.
Windows 2000: This flag is not supported.
KEY_WRITE (0x20006) Combines the STANDARD_RIGHTS_WRITE, KEY_SET_VALUE, and KEY_CREATE_SUB_KEY access rights.

The code

The next code is a personal translation to C# of several pieces of code I found, mostly from here:
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegOpenKeyEx")]
static extern int RegOpenKeyEx(IntPtr hKey, string subKey, uint options, int sam, out IntPtr phkResult);
 
[Flags]
public enum eRegWow64Options : int
{
    None =              0x0000,
    KEY_WOW64_64KEY =   0x0100,
    KEY_WOW64_32KEY =   0x0200,
    // Add here any others needed, from the table of the previous chapter
}
 
[Flags]
public enum eRegistryRights : int
{
    ReadKey =  131097,
    WriteKey = 131078,
}
 
public static RegistryKey OpenSubKey(RegistryKey pParentKey, string pSubKeyName, bool pWriteable,
                                     eRegWow64Options pOptions)
{
    if (pParentKey == null || GetRegistryKeyHandle(pParentKey).Equals(System.IntPtr.Zero))
        throw new System.Exception("OpenSubKey: Parent key is not open");
 
    eRegistryRights Rights = eRegistryRights.ReadKey;
    if (pWriteable)
        Rights = eRegistryRights.WriteKey;
 
    System.IntPtr SubKeyHandle;
    System.Int32 Result = RegOpenKeyEx(GetRegistryKeyHandle(pParentKey), pSubKeyName, 0,
                                      (int)Rights | (int)pOptions, out SubKeyHandle);
    if (Result != 0)
    {
        System.ComponentModel.Win32Exception W32ex = new System.ComponentModel.Win32Exception();
        throw new System.Exception("OpenSubKey: Exception encountered opening key", W32ex);
    }
 
    return PointerToRegistryKey(SubKeyHandle, pWriteable, false);
}
 
private static System.IntPtr GetRegistryKeyHandle(RegistryKey pRegisteryKey)
{
    Type Type = Type.GetType("Microsoft.Win32.RegistryKey");
    FieldInfo Info = Type.GetField("hkey", BindingFlags.NonPublic | BindingFlags.Instance);
 
    SafeHandle Handle = (SafeHandle)Info.GetValue(pRegisteryKey);
    IntPtr RealHandle = Handle.DangerousGetHandle();
 
    return Handle.DangerousGetHandle();
}
 
private static RegistryKey PointerToRegistryKey(IntPtr hKey, bool pWritable, bool pOwnsHandle)
{
    // Create a SafeHandles.SafeRegistryHandle from this pointer - this is a private class
    BindingFlags privateConstructors = BindingFlags.Instance | BindingFlags.NonPublic;
    Type safeRegistryHandleType = typeof(SafeHandleZeroOrMinusOneIsInvalid).Assembly.GetType(
                                             "Microsoft.Win32.SafeHandles.SafeRegistryHandle");
 
    Type[] safeRegistryHandleConstructorTypes = new Type[] { typeof(System.IntPtr), typeof(System.Boolean) };
    ConstructorInfo safeRegistryHandleConstructor = safeRegistryHandleType.GetConstructor(privateConstructors,
                                                               null, safeRegistryHandleConstructorTypes, null);
    Object safeHandle = safeRegistryHandleConstructor.Invoke(new Object[] { hKey, pOwnsHandle });
 
    // Create a new Registry key using the private constructor using the safeHandle - this should then behave like
    // a .NET natively opened handle and disposed of correctly
    Type registryKeyType = typeof(Microsoft.Win32.RegistryKey);
    Type[] registryKeyConstructorTypes = new Type[] { safeRegistryHandleType, typeof(Boolean) };
    ConstructorInfo registryKeyConstructor = registryKeyType.GetConstructor(privateConstructors, null,
                                                                    registryKeyConstructorTypes, null);
    RegistryKey result = (RegistryKey)registryKeyConstructor.Invoke(new Object[] { safeHandle, pWritable });
    return result;
}

How to use the code

The OpenSubKey will return the searched key, allowing you to specify reading from the normal registry, or from the alternative 32-bit, WOW64 registry. The following example reads from the 32-bit WOW64 registry:
try
{
    RegistryKey key = OpenSubKey(Registry.LocalMachine,"Software\\[Key]",false,eRegWow64Options.KEY_WOW64_32KEY);
}
catch
{
    // Parent key not open, exception found at opening (probably related to security permissions requested)
}
You just need to place your key name where “[Key]” is.

How to make your application a bulletproof registry reader

Now that your have a way to read both versions of the registry in x64 environments, I´d suggest you to proceed the following way:
  1. Try to find your key in the usual, standard, .Net way, using the build-in “Registry.LocalMachine.OpenSubKey”. This case will cover your application running in a 32-bit Windows, or running in a 64-bit Windows, but not in WOW64 mode.
  2. If key not found, proceeding in one of the following paths:
    1. Check Windows version
      1. 32 bits: Exception, key not found
      2. 64 bits: Try to find the key in the WOW64 registry with the above code, and launch exception if not found.
    2. Try to read the WOW64 registry directly, enclosing your code in try-catch statement, if any exception is caught, or if key not found, launch exception
In a next article, I´ll talk about detecting which version of Windows your software is running in, and many other things.
Stay tuned!