WPF Label, TextBox, and Mnemonics

Hello. Today I had to get mnemonic to work. Mnemonic is the ability to navigate to a control using a key stroke combination. One of the most common mnemonics is Alt + F. Alt + F usually navigates to the File drop down menu in any Window, and most everyone is familiar with this mnemonic.

In WPF, to get mnemonics, you pretty much just put an underscore in front of a word. For example, for Alt + F, you would enter: _File

Well, in WPF I needed to do the Label, TextBox matching, so that the mnemonics is on a Label but the key strokes navigate the keyboard focus to the TextBox.

Lets say you have a simple form in WPF like this:

The XAML to make these work together using mnemonics is simple. The important property is Target.

<Window x:Class="Mnemonics.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" >
    <Grid FocusManager.FocusedElement="{Binding ElementName=textBoxFirstName}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Target="{Binding ElementName=textBoxFirstName}" HorizontalContentAlignment="Right">
            <AccessText TextWrapping="WrapWithOverflow">First _Name:</AccessText>
        </Label>
        <TextBox Name="textBoxFirstName" Width="200" Height="20" Grid.Column="1"/>
        <Label Target="{Binding ElementName=textBoxLastName}" HorizontalContentAlignment="Right" Grid.Row="1">
            <AccessText TextWrapping="WrapWithOverflow">_Last Name:</AccessText>
        </Label>
        <TextBox Name="textBoxLastName" Width="200" Height="20" Grid.Row="1" Grid.Column="1"/>
    </Grid>
</Window>

Lets discuss the code above briefly.

  1. Notice line 5 first.  Here I am setting the FocusManager.FocusedElement to the first text box so that you can immediately type in the first field when arriving at the window.
  2. Notice the use of AccessText instead of TextBlock (lines 15, 19).  You don’t have to do it this way.  You could have just set the Content property on the label. You don’t need the AccessText at all unless you want to wrap.  If you want to wrap, you have to do it this way.
  3. Notice the use of Target (lines 18,14) and how it is bound to the element that should receive focus when the mnemonic keys are pressed.

Hope this helps you.


Review: GhostBSD 2.0 Beta 2 – A FreeBSD LiveCD

GhostBSD 2.0 – Beta 2 Review

Today I downloaded GhostBSD 2.0.  GhostBSD 2.0 is a live CD that you boot off and your operating system loads to a desktop environment from the CD itself.

It is a CD not a DVD. It was 645.3 MB.  That fits one a single CD.  So it is still small and downloaded rather quickly compared to multi-gigabyte DVD images.

The Good

It didn’t take long to find some good features.

  1. VMWare resolution wasn’t too bad 1280×720.
  2. Decent backgrounds
  3. Thunderbird setup to take your account immediately and the autodetect is cool.
  4. Firefox worked first time.
  5. Has AbiWord and Gnumeric Spreadshet installed.
  6. Has burning software
  7. As a CD ISO, it is still small and downloaded rather quickly compared to multi-gigabyte DVD images.

What Needs works

The first thing that needs work is this review.  I didn’t test much.  I booted, and looked at features, but as I only use a Live CD when something goes terribly wrong and nothing has, I didn’t really do anything too geeky.  So let me just say there is much more to review.  But here are some things I noticed in just my short time clicking away at this.

There wasn’t much bad to speak of that was bad. I had to get nit picky.

  1. There was a problem that delays boot related to the hostname.
  2. No Flash 10 in Firefox, though the linux kernel was there.
  3. Thunderbird is not configured to open links with Firefox by default.
  4. pkg_info shows the pages but says that the package info is corrupt for every package installed.

Other FreeBSD Live CDs

  • Frenzy – A much smaller CD. Here is a quote from the home page: Frenzy is a “portable system administrator toolkit,” LiveCD based on FreeBSD. It generally contains software for hardware tests, file system check, security check and network setup and analysis. Size of ISO-image is 200 MBytes (3″ CD)
  • FreeSBIE – No longer maintained

I didn’t install Frenzy and I have never used it, so I cannot compare it.  However, it is supposedly only 200 MB, which leaves me wondering what it has.

FreeSBIE has been useful in the past, but is no longer maintained.

The Conclusion

GhostBSD 2.0 is the FreeBSD live CD that I currently recommend. I first came across it when it was 1.5, and 2.0 shows it moving forward quite nicely.

Hopes for the future

I do believe there is a place for them to hook into pc-sysinstall and create an installer.  There is not a GNOME-based FreeBSD distribution that I know about, though PC-BSD might add GNOME as an option, but many of the GNOME users would probably jump on this if an installer was built. They may even convert me from KDE to GNOME.


Social Media Widget

I just configured a new widget called Social Media Widget, as you can see on the right of my page.

I links to Facebook, Twitter, Linked in, Google Buzz, and RSS and shows these links.

Follow Us on FacebookFollow Us on TwitterFollow Us on LinkedInFollow Us on BuzzFollow Us on RSS

I rather like it. It supports a lot more too, but those are the only ones I enabled.

I created a new Facebook Fan page for Rhyous, to sort of separate it from my personal page.

Also, I have a subscribe box in the same widget.

It is actually part of the plugin, but it is really easy to add.  It is just html added to the Social Media Widget plugin.

  1. Go to Feedburner.com and sign your sign up if you haven’t already. That is what I use for the email subscription.
  2. Click on the Publicize tab.
  3. Click om the Email subscriptions link on the left. It gives you HTML.
  4. Copy the HTML.
  5. Go to your WordPress widgets.
  6. In the Text section of the Social Media Widget, paste the HTML.
    (I think I new lines to make it look good using <br /> .. pasted html … <br />, which I probably should have done with margin in css)
  7. Play with the HTML as desired…I might have removed the “Deliver by Feedburner” line.  Which is just deleted from the HTML this HTML code which is near the bottom.<p>Delivered by <a href=”http://feedburner.google.com” target=”_blank”>FeedBurner</a></p>

Hope that helps you out.


FreeBSD News: What version of FreeBSD are you using (Poll)?

Over at FreeBSD News, they have a survey asking what version of FreeBSD you are using.

Take a moment and hit this link and enter your FreeBSD version.

What version of FreeBSD are you using (Poll)?

www.wpftutorial.net – just found a new WPF Tutorial site

I just found http://www.wpftutorial.net.

Just another resource for WPF information.


Article: Speed Up Windows by Stripping It Down

I found this article on stripping down Windows.  I have wanted to remove unnecessary services from Windows 7 for a while.  I want to save it for later.

I explains how to remove unnecessary prettiness and services that take a lot of CPU power.

Speed Up Windows by Stripping It Down


Just bought the XBox 250 GB Kinect bundel

It will be here in two days!

Check it out.

Xbox 360 250GB Console with Kinect


Debian one step closer to making the kernel optional

You have heard of options during install, but have you ever thought of swapping out the Kernel? Maybe, for a similar kernel with a different configuration, but what about a completely different kernel, say a Linux or FreeBSD kernel? Well, Debian has thought about this for quite some time, and with their new release of Debian 6, they now have a

Quote on Slashdot: “After two years of work, the Debian Project has announced the release of Debian 6.0. ‘There are many goodies in Debian 6.0 GNU/Linux, not the least of which is the new completely free-as-in-freedom Linux kernel, which no longer contains firmware modules that Debian developers found troublesome,’ says blogger Brian Proffitt. And in addition to Debian GNU/Linux, Debian GNU/kFreeBSD is introduced as a technology preview. ‘Debian GNU/kFreeBSD will port both a 32- and 64-bit PC version of the FreeBSD kernel into the Debian userspace, making them the first Debian release without a Linux kernel,’ says Proffitt. ‘The Debian Project is serious about the technology preview label, though: these FreeBSD-based versions will have limited advanced desktop features.’ The release notes and installation manual have been posted, and installation images may be downloaded right now via bittorrent, jigdo, or HTTP.”

Since FreeBSD is my platform of choice, this idea sounds pretty good to me.

This isn’t hte first time I have commented on this Debian GNU/kFreeBSD release. However, it is in the new again and it is interesting to say the least.


A Hello World template of a WPF application using MVVM

Hey all,

I created an MVVM project and then saved it as a template.  This way when I create new projects, I can simple choose MVVM Base and save a ton of time.

You can use my template if you want.  You can download it here: MVVM Base.zip

How to add the MVVM Base template to your Visual Studio

If you want this to be available as a new project type in you Visual Studio install, follow these steps.

  1. Download this template: MVVM Base.zip
  2. Place this zip file into the %USERPROFILE%\Documents\Visual Studio 2010\Templates\ProjectTemplates\Visual C#.
  3. Create a new Visual Studio Project and choose MVVM Base from the list of C# projects.


Getting the Microphone to work in FreeBSD on a Lenovo T61p

Hey all,

I am trying to get my Microphone to work on my Lenovo T61p.

Problem

If I talk into my microphone while capturing sound, when I play back, I cannot hear my voice.

Troubleshooting

Let me answer some questions for you:

Does the sound work?
Yes. The sound card works great.

Does the hardware work?
The Microphone works well if I boot to windows 7. I have a hard drive with Windows 7 that used to be in this laptop.

What tools am I using to capture sound?
audacity
xvidcap

Can I get any sound to record?
Yes. If I tap hard with my finger on the microphone, the taps are caught, though faint, by both audacity and xvidcap.

What have I done?

Read the Handbook.
Read the man pages for mixer and snd_hda

  1. Loaded and unloaded snd_hda.ko and sound.ko
  2. Changed every mixer setting there is.
  3. Tested tapping and yelling into the mic, tapping barely registers.
  4. Test hardware on Windows 7, works.
  5. Checked systctl -a |grep snd and sysctl -a |grep hda
  6. Changed the mixer to use monitor instead of mic.

Resolution

Well, it was the last step. Once I changed the mixer to use monitor instead of mic, it worked like a charm.

mixer -rec monitor

mixer monitor 35:35


TortoiseSVN Addin for Visual Studio – Disappointing

I wanted TortoiseSVN for Visual Studio 2010, so I searched and I was so excited to find one here: http://tsvnaddin.codeplex.com/SourceControl/list/changesets

I wanted this to work so bad, but it didn’t.  It doesn’t support Visual Studio 2010.

There is a bug you can vote for here: http://tsvnaddin.codeplex.com/workitem/16368

However, I checked an there hasn’t been an update to this project in in almost a year.

I sent an email to the person who did all the commits and asked if the project was dead.  If he doesn’t respond, I have to assume the answer is yes.


Twelve windows applications Microsoft should make open source on their CodePlex site

Microsoft is a big company and has a lot of developers, and it wouldn’t seem they need help from the community. However, the have a lot of code and a lot of different projects that keep the developers plenty busy. At least they are too busy to provide some simple features that exist in almost every other operating system.

There are obvious shortcomings in many of the included Microsoft applications.  Open source communities are excellent at rounded out an application.

If Microsoft were to open source some of their software, and allow for at least for read access on whatever source control they use, then allow for not only submitting bugs but submitting patches, that can be reviewed and applied or rejected, then they could see a wave of improvements come from the many different windows users: Enterprise admins, developers, power users, etc.

It is also nice to be able to debug an application for Microsoft.  It is not always easy to duplicate bugs in house, so if a developer experiences an issue that is annoying enough, they would fix it themselves and while yes, Microsoft benefits, more importantly, all the other users of that application benefit.

http://www.CodePlex.com

Microsoft started ww.CodePlex.com, and it is very similar to SourceForge.net.  They have just under five thousand projects that claim to have stable releases.

Why doesn’t Microsoft make more of their own projects available on CodePlex?

Microsoft could have many open source projects that could become large communities.  One open source community that continues to gain popularity is Windows Install XML. It seems to be a large community now and is growing rapidly.  Strangely it is actually hosted at Sourceforge.net, though it has some sort of presence on CodePlex.

How many Open Source project could Microsoft have?

More people develop using Windows than any other operating system.  (Yes, phone OSes may have or may someday take that over…but that is besides the point).  Think of all the software companies in the world. How many of them don’t develop for Windows.  Even Apple spends a lot of time making iTunes work on Windows.  There are so many developers that might contribute that it is impossible to count them all. Some may end up contributing as part of their job. Many developers might contirubte to a project because it makes their development lives easier. Many enterprise customers might contribute to lower the costs of managing their environment. Many consulting and contract companies might contribute for their own reasons.

So what applications should Microsoft start with?

Here are a list of applications that Microsoft should open source immediately.

#1 – Notepad.exe

Yes, this is the most simple editor that we never use. One of the first thing my colleagues and I do whenever we install windows, is install Notepad++.  While we love Notepad++, we don’t love that we have to install all the time.

After more than 15 years since Windows 95 (or is it more, was notepad.exe in Windows 3.11?) notepad.exe should be one of the most functional and efficient editors in the industry.  However, notepad lacks features that are critical to a good text editor. The most important being syntax coloring. Others features such as supporting regular expressions in find and replace, supporting plugins (especially the XML plugin), are features we use in Notepad++ often. There is really no excuse for Microsoft not having these features. The community has provided these feature for you, all you have to do is include them in your base system.

There are actually many different Notepad-like projects on CodePlex. Maybe some as as mature as Notepad++ maybe some arent. The question is whether any of them will ever become a standard part of the base windows install?

Maybe Microsoft keeps notepad.exe simple, but adds a second more advanced application like Notepad++ in the base system. Or why not just include Notepad++, it is free, after all.

#2 – Internet Explorer

Ok, so Mozilla Firefox and Google Chrome are open source. Microsoft is losing ground to both browsers and this is not going to change in the near future.

Brand is important, however, is really anything left to gain by winning the browser war. Maybe using IE to kill Netscape in the late 90s was important back then. But would anyone really care if IE went away and windows shipped with your choice of the other browsers available? We don’t think so. This is actually how Europe installs of Windows are today. The homepage and search engine war is what is important and Microsoft could still control that on its operating system by having the homepage be msn.com and the default search engine be Bing.

So what use is it for Microsoft to spend time and money developing IE? None really.  It is a waste of time and money they could spend elsewhere.  Release this software to the community. Besides, the new IE development community would not be hampered by being part of an organization that is too large to care enough about IE to release in a timely manner anyway.

If IE became open source, it would be rejuvenated in a way Microsoft can’t do in any other way.

For example, This blog’s home page has a boxes with rounded corners. IE is the only browser that doesn’t support the rounded corners feature in CSS. This feature would have been implements years ago were IE Open Source, as many other features would have as well.

#3 – Windows Explorer

If there is anything that needs debugging help from the community, it is Windows Explorer. Like Notepad.exe, Explorer.exe just hasn’t evolved the way an application should. Even on Windows 7 we all have experienced issues including constant unreproducible hangs, crashes, and failure to refresh.Whether these are due to explorer or something that is plugged into explorer, the inability for a user to do anything about it annoying, especially if that use, like me, is a developer and may or may not have the ability to debug and fix the issue.

Also like Notepad.exe, there is a lack of features and plugins.  After almost two decades of existence, Explore.exe should be one of the most feature rich file browsers in the industry.  But instead, Explorer is difficult to work with if you want to do anything other than just work with the file system. One example of this: TortoiseSVN/TortoiseCVS cannot install and plugin to Explorer without prompting for a reboot. If released as a community project, Explorer.exe would become on of the most feature rich tools in windows.

#4 – Windows Live Messenger

So Windows Live Messenger hasn’t worked for me in months. Microsoft updated it with Windows Update.  Guess what.  It hasn’t worked since. And this is true for a lot of people. Many in my office have seen this as well as many others who are posting to their support site. See my previous article: Windows Live Messenger update broke again: Error 8100030d. This isn’t my only complaint, though the fact that they are releasing a product that doesn’t work is quite tainting to the Windows name.

I kindly offered to debug this issue for them, but my post to their support bulletin was ignored.

Even more annoying than the fact that Windows Live Messenger isn’t working for many people missing feature is the conceited attitude that users of Windows Live Messenger don’t also use other messaging tools. Most messenger applications now support logging into AIM, MSN, Yahoo Messenger, and many others.  But not Windows Live Messenger. If released as an open source project, the Windows Live Messenger community wouldn’t let this missing feature remain absent from the project.

#5 – MSPaint

MSPaint finally had its first bump in features with Windows 7.  However, it is still a shell of what it could be. What could it be? It could be Paint.NET and users love Paint.NET. Paint.NET is everything that MSPaint should be but isn’t. Paint.exe should be a well developed, feature rich application by now. However, Microsoft doesn’t spend any time on it.

So why not put the Paint.net project out there for the community. I don’t quite install Paint.NET as often as Notepad++, but I use it and install it quite often. Why do we have to go out and download and install Paint.NET.  Why after more than a decade and a half can MSPaint be such a basic featureless application.  Because there is no plan for it. Nobody is assigned to create a project plan and drive improvement for it. Even a one person project would be more developed than this after a decade and a half. If there hadn’t been a push to make it more Vista/Windows 7 like, it wouldn’t have gotten the recent update it received.

Look at the successful projects that exist: Paint.NET, Gimp, Pinta, and others. If Microsoft had sponsored such a project years ago or when MSPaint first came to existence, it might be rivaling Adobe Photoshop by now.

#6 – Hyperterminal / Telnet / FTP, etc…

Microsoft doesn’t have an SSH tool of an SFTP tool by default. Are you as frustrated as I am when downloading PuTTY and WinSCP for the hundredth time. Almost every open source operating system has these features as part of their base operating system. Yet Microsoft has somehow found a way to not include them.

An ssh/sftp tool set, both a command line and gui version, should be an open source project on CodePlex. Sure, leave them off of Windows Home Edition and make them have to be installed late. But these features should be included in a standard installation of Windows Ultimate.

#7 – Windows Command Prompt / PowerShell

The PowerShell is a little late to the game. Look at all the shell scripting abilities in open source operating system with sh, csh, ksk, bash, tcsh, etc…  Even OS X can use these.  Microsoft is so far behind when it comes to shell scripting. Let the shell be managed by the community.  Or replace it with a shell, or include a shell.

We can of course install Cygwin but again, why should we have to?

#8 – Hashing tools

On any other platform, we can easily download a file, and check the md5, sha1, or sha256 hash on that file. This is important because when you download software, especially from sites such as SourceForge.net, they always include a list of hashes you can use to make sure that the file was downloaded correctly and that it wasn’t tampered with.

Open a command prompt in windows and type md5, sha1, or sha256 and you will be told that such files are not found.

#9 – .NET Framework

Microsoft is trying to port .NET Framework and one source at Novell SUSE informed me that Microsoft was paying them to deliver open source versions of Silverlight (Moonlight) and .NET Framework (Mono) for open source platforms.  If they open sourced .NET Framework, porting it over would be much more simple and the project might have more community developers.

The ability for software vendors who have so many .NET applications already developed to be able to port them to run on MAC and Linux is going to drive this technology into the forefront over the next decade.

The entire .NET Framework library doesn’t have to be released, but if it was, it would be beneficial to more than just developers for Mono. Developers for Windows would also like to know how something is done in code, and Microsoft has thousands of examples, they just aren’t sharing them.

#10 – Windows Media Player

We want one Media app to rule them all. Windows users are tired of having multiple media players installed. Nobody wants to have all the following apps on their box: WMP, QuickTime, RealPlayer, MPlayer, FlashPlayer, and other one off players.

We are all tired of not being able to play a specific file or media type. If the WMP project were open sourced, it could become the one and only player a user would ever need, with the ability to play media types other codexes that it cannot play now.

Someone would surely write code for it to manage the music on iPods and other MP3 players in a quality manner. This is also strategic for the brand and tactical business act in that this would essentially kill iTunes, and lead the music buying industry back to whatever site Windows Media Player delivered, probably something that gives Microsoft a percentage of the purchase price.

#11 – An Application update service for all applications

Every single application thinks it needs to write its own software to update itself.  And they all have to run their own icon in my task bar, and check for their own software. There should be a project started called the application update services and applications register with this service.  Then one single icon in the task bar can look up the update needs for any application on the system.  No more separate updates from Java, Adobe, and others.

#12 – Regedit.exe

Many of you who are like me are in the registry all the time, developing it, testing it, troubleshooting it, checking settings, changing settings, tweaking performance.  There are dozens of simple features that Regedit.exe is lacking.  Such as the ability to past in a long registry key and have the tool browse to that key, like Explorer.exe allows for directories.  No, you have to click on every last key yourself.

Not to mention that there could be dozens of plugins that allow for making all the things we do in the registry easier (remember the earlier list: developing it, testing it, troubleshooting it, checking settings, changing settings, tweaking performance).

Why is each root key on the same screen, maybe they should be separate tabs?  If this were open source, someone could create that option.

Why can’t we go to an Ethernet tab and have a some easy options to optimize for 100 M or 1GB or the dozens of other advanced but common settings for Ethernet controllers, such as MTU.

Why can’t we do any of this?  Because the Microsoft developers are not being told to develop these in their road maps. But system administrators and desktop administrators and power users who live in the Windows world would love such features and many would add them if they only could have access to the source to do so.

Conclusion

Microsoft is starting to come around to the benefits of open source. They are much more open to it than they have been in the past and they can get the vision of open sources projects that they don’t need to maintain, then everyone will benefit in their own way.


C# DataMeasurement object for comparing, converting, and adding or substracting data sizes

I created a C# DataMeasurement object a while ago but recently really started to use it. I wrote unit tests for it and fixed comparison and add/substract of nulls and wrote Unit tests for it.

The intent of the object is to be able to use any Data size and do the following:

  1. Convert it to any other data size type, 1 GB converts to 1024 MB.
  2. Compare it to any other data size type, 1 GB = 1024 MB.
  3. Add any other data size type and have it add correctly, 1 GB + 1024 MB = 2GB.

So you can easily compare things like 12343 bytes.

You can create the object in a lot of different ways as I have multiple constructors, and it is easy to add your own constructor.

Here is the object:

using System;
using System.Collections.Generic;
using System.Text;

namespace LANDesk.Install.Common
{
    public class DataMeasurement
    {
        #region Member Variables
        // This is the size in the current type. For example, 1 GB = 1.0. But
        // if you convert the Type to Megabyte, the value should change to 1024.0;
        protected Double _DataSize;  
        protected DataType _DataType;
        #endregion

        #region Constructors

        /// <summary>
        /// The default constructor.  This initializes and object with a
        /// DataSize of 0 and a DataType of Byte.
        /// </summary>
        public DataMeasurement()
        {
            _DataSize = 0;
            _DataType = DataType.Byte;
        }

        /// <summary>
        /// This constructor takes a value and assumes the data measurement
        /// type is in bytes.
        /// </summary>
        /// <param name="inValue">The measurement value as a ulong.</param>
        public DataMeasurement(UInt64 inSizeInBytes)
        {
            _DataType = DataType.Byte;
            _DataSize = inSizeInBytes * 8;
        }

        /// <summary>
        /// This constructor takes a value and a data measurement type.
        /// </summary>
        /// <param name="inValue">The measurement value as a ulong.</param>
        /// <param name="inType">The measurement type.</param>
        public DataMeasurement(UInt64 inValue, DataType inType)
        {
            _DataType = inType;
            _DataSize = inValue;
        }

        /// <summary>
        /// This constructor takes a double value and defaults to megabyte.
        /// </summary>
        /// <param name="inValue"></param>
        public DataMeasurement(double inValue)
        {
            _DataType = DataType.Megabyte;
            _DataSize = inValue;
        }

        /// <summary>
        /// This constructor takes a value and a data measurement type.
        /// </summary>
        /// <param name="inValue">The measurement value as a double.</param>
        /// <param name="inType">The measurement type.</param>
        public DataMeasurement(double inValue, DataType inType)
        {
            _DataType = inType;
            _DataSize = inValue;
        }

        /// <summary>
        /// This constructor takes a value and a data measurement type.
        /// </summary>
        /// <param name="inValue">The measurement value as a double.</param>
        /// <param name="inType">The measurement type as a string, "GB", "Gigabyte".</param>
        public DataMeasurement(UInt64 inValue, String inType)
        {
            _DataSize = inValue;
            GetDataTypeFromString(inType);
        }

        /// <summary>
        /// This constructor takes a string representation of a data measurement.
        /// </summary>
        /// <param name="inValueAndType">The measurement value and type in a  
        /// single string such as "49 GB" or "49 GBSI" or "49 Gigabyte". There
        /// must be a space as it is used to split the value from the type, so 
        ///  "49GB" is invalid because there is no space delimeter.</param>
        public DataMeasurement(String inValueAndType)
        {
            char[] split = { ' ' };
            String[] data = inValueAndType.Split(split);
            _DataSize = Convert.ToUInt64(data[0]);
            GetDataTypeFromString(data[1]);
        }
        #endregion

        #region Properties
        public Double Size
        {
            get { return _DataSize; }
            set { _DataSize = value; }
        }

        public DataType DataSizeType
        {
            get { return _DataType; }
            set { _DataType = value; }
        }

        public ShortName DataSizeTypeShortName
        {
            get { return (ShortName)_DataType; }
            set { _DataType = (DataType)value; }
        }

        public ulong Bit
        {
            get { return (ulong)ConvertToType(DataType.Bit, false); }
            set
            {
                _DataType = DataType.Bit;
                _DataSize = value;
            }
        }

        public Double Byte
        {
            get { return ConvertToType(DataType.Byte, false); }
            set
            {
                _DataType = DataType.Byte;
                _DataSize = value;
            }
        }

        public Double KilobitSI
        {
            get { return ConvertToType(DataType.KilobitSI, false); }
            set
            {
                _DataType = DataType.KilobitSI;
                _DataSize = value;
            }
        }

        public Double Kilobit
        {
            get { return ConvertToType(DataType.Kilobit, false); }
            set
            {
                _DataType = DataType.Kilobit;
                _DataSize = value;
            }
        }
        public Double KilobyteSI
        {
            get { return ConvertToType(DataType.KilobyteSI, false); }
            set
            {
                _DataType = DataType.KilobyteSI;
                _DataSize = value;
            }
        }

        public Double Kilobyte
        {
            get { return ConvertToType(DataType.Kilobyte, false); }
            set
            {
                _DataType = DataType.Kilobit;
                _DataSize = value;
            }
        }

        public Double MegabitSI
        {
            get { return ConvertToType(DataType.MegabitSI, false); }
            set
            {
                _DataType = DataType.MegabitSI;
                _DataSize = value;
            }
        }

        public Double Megabit
        {
            get { return ConvertToType(DataType.Megabit, false); }
            set
            {
                _DataType = DataType.Megabit;
                _DataSize = value;
            }
        }
        public Double MegabyteSI
        {
            get { return ConvertToType(DataType.MegabyteSI, false); }
            set
            {
                _DataType = DataType.MegabyteSI;
                _DataSize = value;
            }
        }

        public Double Megabyte
        {
            get { return ConvertToType(DataType.Megabyte, false); }
            set
            {
                _DataType = DataType.Megabyte;
                _DataSize = value;
            }
        }

        public Double GigabitSI
        {
            get { return ConvertToType(DataType.GigabitSI, false); }
            set
            {
                _DataType = DataType.GigabitSI;
                _DataSize = value;
            }
        }

        public Double Gigabit
        {
            get { return ConvertToType(DataType.Gigabit, false); }
            set
            {
                _DataType = DataType.Gigabit;
                _DataSize = value;
            }
        }
        public Double GigabyteSI
        {
            get { return ConvertToType(DataType.GigabyteSI, false); }
            set
            {
                _DataType = DataType.GigabyteSI;
                _DataSize = value;
            }
        }

        public Double Gigabyte
        {
            get { return ConvertToType(DataType.Gigabyte, false); }
            set
            {
                _DataType = DataType.Gigabyte;
                _DataSize = value;
            }
        }

        public Double TerabitSI
        {
            get { return ConvertToType(DataType.TerabitSI, false); }
            set
            {
                _DataType = DataType.TerabitSI;
                _DataSize = value;
            }
        }

        public Double Terabit
        {
            get { return ConvertToType(DataType.Terabit, false); }
            set
            {
                _DataType = DataType.Terabit;
                _DataSize = value;
            }
        }
        public Double TerabyteSI
        {
            get { return ConvertToType(DataType.TerabyteSI, false); }
            set
            {
                _DataType = DataType.TerabyteSI;
                _DataSize = value;
            }
        }

        public Double Terabyte
        {
            get { return ConvertToType(DataType.Terabyte, false); }
            set
            {
                _DataType = DataType.Terabyte;
                _DataSize = value;
            }
        }

        public Double PetabitSI
        {
            get { return ConvertToType(DataType.PetabitSI, false); }
            set
            {
                _DataType = DataType.PetabitSI;
                _DataSize = value;
            }
        }

        public Double Petabit
        {
            get { return ConvertToType(DataType.Petabit, false); }
            set
            {
                _DataType = DataType.Petabit;
                _DataSize = value;
            }
        }
        public Double PetabyteSI
        {
            get { return ConvertToType(DataType.PetabyteSI, false); }
            set
            {
                _DataType = DataType.PetabyteSI;
                _DataSize = value;
            }
        }

        public Double Petabyte
        {
            get { return ConvertToType(DataType.Petabyte, false); }
            set
            {
                _DataType = DataType.Petabyte;
                _DataSize = value;
            }
        }

        public Double ExabitSI
        {
            get { return ConvertToType(DataType.ExabitSI, false); }
            set
            {
                _DataType = DataType.ExabitSI;
                _DataSize = value;
            }
        }

        public Double Exabit
        {
            get { return ConvertToType(DataType.Exabit, false); }
            set
            {
                _DataType = DataType.Exabit;
                _DataSize = value;
            }
        }

        public Double ExabyteSI
        {
            get { return ConvertToType(DataType.ExabyteSI, false); }
            set
            {
                _DataType = DataType.ExabyteSI;
                _DataSize = value;
            }
        }

        public Double Exabyte
        {
            get { return ConvertToType(DataType.Exabyte, false); }
            set
            {
                _DataType = DataType.Exabyte;
                _DataSize = value;
            }
        }
        #endregion

        #region Functions
        /// <summary>
        /// Writes the DataMeasurement to string, such as "1 GB" or "1024 KB".
        /// It always uses the DataSize and DataType values.
        /// </summary>
        override public string ToString()
        {
            return String.Format("{0} {1}", _DataSize, ((ShortName)_DataType).ToString());
        }

        /// <summary>
        /// Writes the DataMeasurement to string, such as "1 GB" or "1024 KB".
        /// However, it does so in the DataType that you specify.
        /// </summary>
        /// <param name="inDataType">The type to output to string. For example
        /// If the measurement is "4 Gigabyte" but you pass in Megabyte, it 
        /// will output "4096 MB".</param>
        /// <returns></returns>
        public string ToString(DataType inDataType)
        {
            Double size = ConvertToType(inDataType, false);
            return String.Format("{0} {1}", size, ((ShortName)inDataType).ToString());
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            DataMeasurement right = obj as DataMeasurement;
            if ((object)right == null)
                return false;

            return this == right;
        }

        private void GetDataTypeFromString(String inType)
        {
            if (inType.Length == 2 || inType.Length == 4)
            {   // Short name was used so get the ShortName and case it to a DataType
                // KB and Kb are not the same, so case is Important
                _DataType = (DataType)Enum.Parse(typeof(ShortName), inType, false);
            }
            else
            {   // Long name was used so get 
                // We can ignore case because Kilobit and Kilobyte are different.
                _DataType = (DataType)Enum.Parse(typeof(DataType), inType, true);
            }
        }

        /// <summary>
        /// This function converts the stored measurement from its current
        /// value to a new value using the new DataType.
        /// </summary>
        /// <param name="inNewType">The new DataType. For example If the
        /// measurement is "4 Gigabyte" but you pass in Megabyte, it will
        /// change the value to "4096 Megabyte".</param>
        /// <returns>The new DataSize for the new type is returned.</returns>
        public Double ConvertToType(DataType inNewType)
        {
            return ConvertToType(inNewType, true);
        }

        /// <summary>
        /// This function converts the stored measurement from its current
        /// value to a new value using the new DataType.
        /// </summary>
        /// <param name="inNewType">The new DataType. For example If the
        /// measurement is "4 Gigabyte" but you pass in Megabyte, it will
        /// change the value to "4096 Megabyte".</param>
        /// <param name="inChangeObjectMeasurementType">A bool value that
        /// specifies whether to change the whole object.</param>
        /// <returns>The new DataSize for the new type is returned.</returns>
        public Double ConvertToType(DataType inNewType, bool inChangeObjectMeasurementType)
        {
            double ret = 0;
            bool isSet = false;
            if (inNewType == _DataType)
            {
                ret = _DataSize;
                isSet = true;
            }
            else if (inNewType == DataType.Bit)
            {
                ret = (ulong)_DataType * _DataSize * 8;
                isSet = true;
            }
            else if (_DataType == DataType.Bit)
            {
                ret = _DataSize / 8 / (ulong)inNewType;
                isSet = true;
            }
            
            if (!isSet)
                ret = (ulong)_DataType * _DataSize / (ulong)inNewType;

            if (inChangeObjectMeasurementType)
            {
                _DataType = inNewType;
                Size = ret;
            }

            return ret;
        }
        #endregion

        #region Operator Override Functions
        public static bool operator ==(DataMeasurement left, DataMeasurement right)
        {
            if (null == (object)left && null == (object)right)
                return true;
            if (null == (object)right || null == (object)left)
                return false;

            if (left.DataSizeType == right.DataSizeType)
            {
                return left.Size == right.Size;
            }
            else
            {
                return left.Byte == right.Byte;
            }
        }

        public static bool operator !=(DataMeasurement left, DataMeasurement right)
        {
            if (null == (object)left && null == (object)right)
                return false;
            if (null == (object)right || null == (object)left)
                return true;

            if (left.DataSizeType == right.DataSizeType)
            {
                return left.Size != right.Size;
            }
            else
            {
                return left.Byte != right.Byte;
            }
        }

        public static bool operator >(DataMeasurement left, DataMeasurement right)
        {
            if (null == (object)left && null == (object)right)
                return false;
            if (null == (object)right)
                return true;
            if (null == (object)left)
                return false;

            if (left.DataSizeType == right.DataSizeType)
            {
                return left.Size > right.Size;
            }
            else
            {
                return left.Byte > right.Byte;
            }
        }

        public static bool operator >=(DataMeasurement left, DataMeasurement right)
        {
            if (left.DataSizeType == right.DataSizeType)
            {
                return left.Size >= right.Size;
            }
            else
            {
                return left.Byte >= right.Byte;
            }
        }

        public static bool operator <(DataMeasurement left, DataMeasurement right)
        {
            if (null == (object)left && null == (object)right)
                return false;
            if (null == (object)right)
                return false;
            if (null == (object)left)
                return true;
            if (left.DataSizeType == right.DataSizeType)
            {
                return left.Size < right.Size;
            }
            else
            {
                return left.Byte < right.Byte;
            }
        }

        public static bool operator <=(DataMeasurement left, DataMeasurement right)
        {
            if (left.DataSizeType == right.DataSizeType)
            {
                return left.Size <= right.Size;
            }
            else
            {
                return left.Byte <= right.Byte;
            }
        }

        public static DataMeasurement operator +(DataMeasurement left, DataMeasurement right)
        {
            return left + right.ConvertToType(left.DataSizeType, false);
        }

        public static DataMeasurement operator +(DataMeasurement left, double right)
        {
            if ((null == (object)left && null == (object)right) || null == (object)right)
                return left;
            if (null == (object)left)
                return new DataMeasurement(right);
            return new DataMeasurement(left.Size + right, left.DataSizeType);
        }

        public static DataMeasurement operator -(DataMeasurement left, DataMeasurement right)
        {
            return left - right.ConvertToType(left.DataSizeType, false);
        }

        public static DataMeasurement operator -(DataMeasurement left, double right)
        {
            if ((null == (object)left && null == (object)right) || null == (object)right)
                return left;
            if (null == (object)left)
                return new DataMeasurement(right);
            return new DataMeasurement(left.Size - right, left.DataSizeType);
        }
        #endregion

        #region Enums
        public enum ShortName : ulong
        {
            b = 0, // Bit must be handled special
            // Everything after is in bytes
            B = 1,
            KbSI = 125,
            Kb = 128,
            KBSI = 1000,
            KB = 1024,
            MbSI = 125000,
            Mb = 131072,
            MBSI = 1000000,
            MB = 1048576,
            GbSI = 125000000,
            Gb = 134217728,
            GBSI = 1000000000,
            GB = 1073741824,
            TbSI = 125000000000,
            Tb = 137438953472,
            TBSI = 1000000000000,
            TB = 1099511627776,
            PbSI = 125000000000000,
            Pb = 140737488355328,
            PBSI = 1000000000000000,
            PB = 1125899906842624,
            EbSI = 125000000000000000,
            Eb = 144115188075855872,
            EBSI = 1000000000000000000,
            EB = 1152921504606846976
        }

        public enum DataType : ulong
        {
            Bit = 0, // Bit must be handled special
            // Everything after is in bytes
            Byte = 1,
            KilobitSI = 125,
            Kilobit = 128,
            KilobyteSI = 1000,
            Kilobyte = 1024,
            MegabitSI = 125000,
            Megabit = 131072,
            MegabyteSI = 1000000,
            Megabyte = 1048576,
            GigabitSI = 125000000,
            Gigabit = 134217728,
            GigabyteSI = 1000000000,
            Gigabyte = 1073741824,
            TerabitSI = 125000000000,
            Terabit = 137438953472,
            TerabyteSI = 1000000000000,
            Terabyte = 1099511627776,
            PetabitSI = 125000000000000,
            Petabit = 140737488355328,
            PetabyteSI = 1000000000000000,
            Petabyte = 1125899906842624,
            ExabitSI = 125000000000000000,
            Exabit = 144115188075855872,
            ExabyteSI = 1000000000000000000,
            Exabyte = 1152921504606846976
        }
        #endregion
    }
}

I could probably do a better job of testing this. Here is a test class that succeeds. If you want more testing add it.

Hopefully this helps you out.

using LANDesk.Install.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace LANDesk.Install.Common.Tests
{
    /// <summary>
    ///This is a test class for DataMeasurementTest and is intended
    ///to contain all DataMeasurementTest Unit Tests
    ///</summary>
    [TestClass()]
    public class DataMeasurementTest
    {
        List<DataMeasurement> sizes = new List<DataMeasurement>();

        [TestMethod()]
        public void DataMeasurementTestAdditionAndGreaterThanLessThan()
        {
            DataMeasurementEqualityTest();
            DataMeasurement OneGB = new DataMeasurement("1 GB");
            DataMeasurement OneGBinMB = new DataMeasurement("1024 MB");
            DataMeasurement twoGB = new DataMeasurement(2, DataMeasurement.DataType.Gigabyte);
            DataMeasurement twoGBadded = OneGB + OneGBinMB;
            TestEqualValues(twoGB, twoGBadded);

            foreach (DataMeasurement dm in sizes)
            {
                TestLeftIsGreater(twoGB, dm);
                TestRightIsGreater(dm, twoGB);
            }
        }

        [TestMethod()]
        public void DataMeasurementTestSubtractionAndGreaterThanLessThan()
        {
            DataMeasurementEqualityTest();
            DataMeasurement tenGB = new DataMeasurement(10, DataMeasurement.DataType.Gigabyte);
            DataMeasurement NineGB = new DataMeasurement(9, DataMeasurement.DataType.Gigabyte);
            DataMeasurement NineGBbySubtraction = tenGB - sizes[0];
            TestEqualValues(NineGB, NineGBbySubtraction);
            TestLeftIsGreater(tenGB, NineGB);
            TestLeftIsGreater(tenGB, NineGBbySubtraction);
            TestRightIsGreater(NineGB, tenGB);
            TestRightIsGreater(NineGBbySubtraction, tenGB);
        }
        
        [TestMethod()]
        public void TestConversion()
        {
            DataMeasurement dm = new DataMeasurement("4 GB");
            dm.ConvertToType(DataMeasurement.DataType.Megabyte);
            Assert.IsTrue(dm.Size == (double)4096);
            Assert.IsTrue(dm.ToString() == "4096 MB");            
        }

        [TestMethod()]
        public void DataMeasurementOtherTest()
        {
            sizes = new List<DataMeasurement>();
            int i = -1;

            // Test 1 - Check that conversion is working
            i++;
            sizes.Add(new DataMeasurement("1024 bit"));
            Assert.IsTrue(sizes[i].Bit == 1024, "Test 1, to Bit.");
            Assert.IsTrue(sizes[i].Byte == 1024 / 8, "Test 1, to Byte.");
            double expected = 1024 / 8 / 1024.0;
            Assert.IsTrue(sizes[i].Kilobyte == expected, "Test 1, " + sizes[i].Kilobyte + " should equal " + expected);

            // Test 2 - Check that conversion is working
            i++;
            sizes.Add(new DataMeasurement(4, DataMeasurement.DataType.Gigabyte));
            Assert.IsTrue(sizes[i].Bit == 34359738368, "Test 2, to bit.");
            Assert.IsTrue(sizes[i].Byte == 4294967296, "Test 2, to Byte.");
            Assert.IsTrue(sizes[i].Kilobyte == 4 * 1024 * 1024, "Test 2, to Kilobyte.");
            expected = 4.0 / 1024;
            Assert.IsTrue(sizes[i].Terabyte == expected, "Test 2, " + sizes[i].Terabyte + " should equal " + expected);

            // Test 3 - Comparisons
            Assert.IsTrue(sizes[i - 1] < sizes[i], "Test 3, " + sizes[i - 1] + " is less than " + sizes[i]);
            Assert.IsTrue(sizes[i - 1] <= sizes[i], "Test 3, " + sizes[i - 1] + " is less than or equal to " + sizes[i]);
            Assert.IsFalse(sizes[i - 1] > sizes[i], "Test 3, " + sizes[i - 1] + " is greater than " + sizes[i]);
            Assert.IsFalse(sizes[i - 1] >= sizes[i], "Test 3, " + sizes[i - 1] + " is greater than or equal to " + sizes[i]);
            Assert.IsFalse(sizes[i - 1] == sizes[i], "Test 3, " + sizes[i - 1] + " is equal to " + sizes[i]);
            Assert.IsTrue(sizes[i - 1] != sizes[i], "Test 3, " + sizes[i - 1] + " is not equal to " + sizes[i]);
        }

        public void DataMeasurementEqualityTest()
        {
            DataMeasurement last = null;
            DataMeasurement OneGB = new DataMeasurement("1 GB");
            foreach (DataMeasurement.DataType dt in Enum.GetValues(typeof(DataMeasurement.DataType)))
            {
                DataMeasurement next = new DataMeasurement(OneGB.ConvertToType(dt, false), dt);
                sizes.Add(next);
                if (last != null)
                    TestEqualValues(next, last);
                last = next;
            }
        }

        private void TestEqualValues(DataMeasurement left, DataMeasurement right)
        {
            Assert.IsFalse(left > right);
            Assert.IsFalse(left < right);
            Assert.IsTrue(left >= right);
            Assert.IsTrue(left <= right);
            Assert.IsTrue(left == right);
            Assert.IsFalse(left != right);
            Assert.IsTrue(left.Equals(right));
        }

        private void TestLeftIsGreater(DataMeasurement left, DataMeasurement right)
        {
            Assert.IsTrue(left > right);
            Assert.IsFalse(left < right);
            Assert.IsTrue(left >= right);
            Assert.IsFalse(left <= right);
            Assert.IsFalse(left == right);
            Assert.IsTrue(left != right);
        }

        private void TestRightIsGreater(DataMeasurement left, DataMeasurement right)
        {
            Assert.IsFalse(left > right);
            Assert.IsTrue(left < right);
            Assert.IsFalse(left >= right);
            Assert.IsTrue(left <= right);
            Assert.IsFalse(left == right);
            Assert.IsTrue(left != right);
        }
    }
}

I hope this object helps save you time.


How to read the 64 bit registry from a 32 bit application or vice versa

I found out that I needed to read the 64 bit registry from a 32 bit app today.

Why you might ask?

Well, I need to get the RegisteredOrganization value from HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion and unfortunately Microsoft has a bug where the WOW6432 version of this key always says Microsoft, so a 32 bit application would always return Microsoft as the RegisteredOrganization, regardless of what the user entered when they installed the OS. This is hardly desired.

Note: This is also why all Visual Studio projects created in Windows 7 64 bit have Microsoft in the project’s Assembly Information. Change the WOW6432 version of the RegisteredOrganization and you fix this Visual Studio issue.
Well, turns out C# doesn’t have functionality until .NET 4, so prior to .NET 4, to choose the 64 bit hive when running a 32 bit app, so we have to do use a DLLImport and use RegOpenKeyEx, RegCloseKey, and RegQueryValueEx.

I don’t have this well commented, and it is not very newbie proof, but here are three different ways to do this. Hopefully you can understand one or more of these.

Example 1 – .NET 4 Example

Here is how to do this in .NET 4.

using Microsoft.Win32;

namespace Read64bitRegistryFrom32bitApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string value64 = string.Empty;
            string value32 = string.Empty;

            RegistryKey localKey = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
            localKey = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
            if (localKey != null)
            {
                value64 = localKey.GetValue("RegisteredOrganization").ToString();
            }
            RegistryKey localKey32 = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
            localKey32 = localKey32.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
            if (localKey32 != null)
            {
                value32 = localKey32.GetValue("RegisteredOrganization").ToString();
            }
        }
    }
}

.NET 3.5 SP1 and Prior

This can also be done in .NET 3.5 and prior but it is not easy.
We have to do use a DLLImport and use RegOpenKeyEx, RegCloseKey, and RegQueryValueEx. Here are some examples.

Example 1 – A console application to read the 64 bit registry from a 32 bit application or vice versa

Here is the code in a simple one file project:
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Read64bitRegistryFrom32bitApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string value64 = GetRegKey64(RegHive.HKEY_LOCAL_MACHINE, @"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "RegisteredOrganization");
            Console.WriteLine(value64);
            string value32 = GetRegKey32(RegHive.HKEY_LOCAL_MACHINE, @"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "RegisteredOrganization");
            Console.WriteLine(value32);
        }

        public enum RegSAM
        {
            QueryValue = 0x0001,
            SetValue = 0x0002,
            CreateSubKey = 0x0004,
            EnumerateSubKeys = 0x0008,
            Notify = 0x0010,
            CreateLink = 0x0020,
            WOW64_32Key = 0x0200,
            WOW64_64Key = 0x0100,
            WOW64_Res = 0x0300,
            Read = 0x00020019,
            Write = 0x00020006,
            Execute = 0x00020019,
            AllAccess = 0x000f003f
        }

        public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);
        public static UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u);

        #region Member Variables
        #region Read 64bit Reg from 32bit app
        [DllImport("Advapi32.dll")]
        static extern uint RegOpenKeyEx(
            UIntPtr hKey,
            string lpSubKey,
            uint ulOptions,
            int samDesired,
            out int phkResult);

        [DllImport("Advapi32.dll")]
        static extern uint RegCloseKey(int hKey);

        [DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx")]
        public static extern int RegQueryValueEx(
            int hKey, string lpValueName,
            int lpReserved,
            ref uint lpType,
            System.Text.StringBuilder lpData,
            ref uint lpcbData);
        #endregion
        #endregion

        #region Functions
        static public string GetRegKey64(UIntPtr inHive, String inKeyName, String inPropertyName)
        {
            return GetRegKey64(inHive, inKeyName, RegSAM.WOW64_64Key, inPropertyName);
        }

        static public string GetRegKey32(UIntPtr inHive, String inKeyName, String inPropertyName)
        {
            return GetRegKey64(inHive, inKeyName, RegSAM.WOW64_32Key, inPropertyName);
        }

        static public string GetRegKey64(UIntPtr inHive, String inKeyName, RegSAM in32or64key, String inPropertyName)
        {
            //UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002;
            int hkey = 0;

            try
            {
                uint lResult = RegOpenKeyEx(RegHive.HKEY_LOCAL_MACHINE, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out hkey);
                if (0 != lResult) return null;
                uint lpType = 0;
                uint lpcbData = 1024;
                StringBuilder AgeBuffer = new StringBuilder(1024);
                RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, AgeBuffer, ref lpcbData);
                string Age = AgeBuffer.ToString();
                return Age;
            }
            finally
            {
                if (0 != hkey) RegCloseKey(hkey);
            }
        }
        #endregion
    }
}

Example 2 – A static class to read the 64 bit registry from a 32 bit application or vice versa

Or if you want this in its own separate class, here is a static class you can add to your project.
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Read64bitRegistryFrom32bitApp
{
    public enum RegSAM
    {
        QueryValue = 0x0001,
        SetValue = 0x0002,
        CreateSubKey = 0x0004,
        EnumerateSubKeys = 0x0008,
        Notify = 0x0010,
        CreateLink = 0x0020,
        WOW64_32Key = 0x0200,
        WOW64_64Key = 0x0100,
        WOW64_Res = 0x0300,
        Read = 0x00020019,
        Write = 0x00020006,
        Execute = 0x00020019,
        AllAccess = 0x000f003f
    }

    public static class RegHive
    {
        public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);
        public static UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u);
    }

    public static class RegistryWOW6432
    {
        #region Member Variables
        #region Read 64bit Reg from 32bit app
        [DllImport("Advapi32.dll")]
        static extern uint RegOpenKeyEx(
            UIntPtr hKey,
            string lpSubKey,
            uint ulOptions,
            int samDesired,
            out int phkResult);

        [DllImport("Advapi32.dll")]
        static extern uint RegCloseKey(int hKey);

        [DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx")]
        public static extern int RegQueryValueEx(
            int hKey, string lpValueName,
            int lpReserved,
            ref uint lpType,
            System.Text.StringBuilder lpData,
            ref uint lpcbData);
        #endregion
        #endregion

        #region Functions
        static public string GetRegKey64(UIntPtr inHive, String inKeyName, String inPropertyName)
        {
            return GetRegKey64(inHive, inKeyName, RegSAM.WOW64_64Key, inPropertyName);
        }

        static public string GetRegKey32(UIntPtr inHive, String inKeyName, String inPropertyName)
        {
            return GetRegKey64(inHive, inKeyName, RegSAM.WOW64_32Key, inPropertyName);
        }

        static public string GetRegKey64(UIntPtr inHive, String inKeyName, RegSAM in32or64key, String inPropertyName)
        {
            //UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002;
            int hkey = 0;

            try
            {
                uint lResult = RegOpenKeyEx(RegHive.HKEY_LOCAL_MACHINE, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out hkey);
                if (0 != lResult) return null;
                uint lpType = 0;
                uint lpcbData = 1024;
                StringBuilder AgeBuffer = new StringBuilder(1024);
                RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, AgeBuffer, ref lpcbData);
                string Age = AgeBuffer.ToString();
                return Age;
            }
            finally
            {
                if (0 != hkey) RegCloseKey(hkey);
            }
        }
        #endregion

        #region Enums
        #endregion
    }
}
Here is an example of using this class.
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Read64bitRegistryFrom32bitApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string value64 = RegistryWOW6432.GetRegKey64(RegHive.HKEY_LOCAL_MACHINE, @"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "RegisteredOrganization");
            string value32 = RegistryWOW6432.GetRegKey32(RegHive.HKEY_LOCAL_MACHINE, @"SOFTWARE\Microsoft\Windows NT\CurrentVersion", "RegisteredOrganization");
        }
    }
}

Example 3 – Adding extension methods to the managed RegistryKey object that read the 64 bit registry from a 32 bit application or vice versa

You know what else is a cool idea? Making it an extension class to the normal managed registry C# code. So you can create a regular managed RegistryKey and then just call an extension function off it.
using System;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;

namespace Read64bitRegistryFrom32bitApp
{
    /// <summary>
    /// An extension class to allow a registry key to allow it to get the
    /// registry in the 32 bit (Wow6432Node) or 64 bit regular registry key
    /// </summary>
    public static class RegistryWOW6432
    {
        #region Member Variables
        #region Read 64bit Reg from 32bit app
        public static UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);
        public static UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u);

        [DllImport("Advapi32.dll")]
        static extern uint RegOpenKeyEx(
            UIntPtr hKey,
            string lpSubKey,
            uint ulOptions,
            int samDesired,
            out int phkResult);

        [DllImport("Advapi32.dll")]
        static extern uint RegCloseKey(int hKey);

        [DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx")]
        public static extern int RegQueryValueEx(
            int hKey, 
            string lpValueName,
            int lpReserved,
            ref RegistryValueKind lpType,
            StringBuilder lpData,
            ref uint lpcbData);

        [DllImport("advapi32.dll", CharSet = CharSet.Unicode, EntryPoint = "RegQueryValueEx")]
        private static extern int RegQueryValueEx(
            int hKey,
            string lpValueName,
            int lpReserved,
            ref RegistryValueKind lpType,
            [Out] byte[] lpData,
            ref uint lpcbData);
        #endregion
        #endregion

        #region Functions
        public static string GetRegKey64(this RegistryKey inKey, String inPropertyName)
        {
            string strKey = inKey.ToString();
            string regHive = strKey.Split('\\')[0];
            string regPath = strKey.Substring(strKey.IndexOf('\\') + 1);
            return GetRegKey64(GetRegHiveFromString(regHive), regPath, RegSAM.WOW64_64Key, inPropertyName);
        }

        public static string GetRegKey32(this RegistryKey inKey, String inPropertyName)
        {
            string strKey = inKey.ToString();
            string regHive = strKey.Split('\\')[0];
            string regPath = strKey.Substring(strKey.IndexOf('\\') + 1);
            return GetRegKey64(GetRegHiveFromString(regHive), regPath, RegSAM.WOW64_32Key, inPropertyName);
        }

        public static byte[] GetRegKey64AsByteArray(this RegistryKey inKey, String inPropertyName)
        {
            string strKey = inKey.ToString();
            string regHive = strKey.Split('\\')[0];
            string regPath = strKey.Substring(strKey.IndexOf('\\') + 1);
            return GetRegKey64AsByteArray(GetRegHiveFromString(regHive), regPath, RegSAM.WOW64_64Key, inPropertyName);
        }

        public static byte[] GetRegKey32AsByteArray(this RegistryKey inKey, String inPropertyName)
        {
            string strKey = inKey.ToString();
            string regHive = strKey.Split('\\')[0];
            string regPath = strKey.Substring(strKey.IndexOf('\\') + 1);
            return GetRegKey64AsByteArray(GetRegHiveFromString(regHive), regPath, RegSAM.WOW64_32Key, inPropertyName);
        }

        private static UIntPtr GetRegHiveFromString(string inString)
        {
            if (inString == "HKEY_LOCAL_MACHINE")
                return HKEY_LOCAL_MACHINE;
            if (inString == "HKEY_CURRENT_USER")
                return HKEY_CURRENT_USER;
            return UIntPtr.Zero;
        }

        static public string GetRegKey64(UIntPtr inHive, String inKeyName, RegSAM in32or64key, String inPropertyName)
        {
            //UIntPtr HKEY_LOCAL_MACHINE = (UIntPtr)0x80000002;
            int hkey = 0;

            try
            {
                uint lResult = RegOpenKeyEx(inHive, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out hkey);
                if (0 != lResult) return null;
                RegistryValueKind lpType = 0;
                uint lpcbData = 1024;
                StringBuilder strBuffer = new StringBuilder(1024);
                RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, strBuffer, ref lpcbData);
                string value = strBuffer.ToString();
                return value;
            }
            finally
            {
                if (0 != hkey) RegCloseKey(hkey);
            }
        }

        static public byte[] GetRegKey64AsByteArray(UIntPtr inHive, String inKeyName, RegSAM in32or64key, String inPropertyName)
        {
            int hkey = 0;

            try
            {
                uint lResult = RegOpenKeyEx(inHive, inKeyName, 0, (int)RegSAM.QueryValue | (int)in32or64key, out hkey);
                if (0 != lResult) return null;
                RegistryValueKind lpType = 0;
                uint lpcbData = 2048;

                // Just make a big buffer the first time
                byte[] byteBuffer = new byte[1000];
                // The first time, get the real size
                RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, byteBuffer, ref lpcbData);
                // Now create a correctly sized buffer
                byteBuffer = new byte[lpcbData];
                // now get the real value
                RegQueryValueEx(hkey, inPropertyName, 0, ref lpType, byteBuffer, ref lpcbData);

                return byteBuffer;
            }
            finally
            {
                if (0 != hkey) RegCloseKey(hkey);
            }
        }
        #endregion

        #region Enums
        public enum RegSAM
        {
            QueryValue = 0x0001,
            SetValue = 0x0002,
            CreateSubKey = 0x0004,
            EnumerateSubKeys = 0x0008,
            Notify = 0x0010,
            CreateLink = 0x0020,
            WOW64_32Key = 0x0200,
            WOW64_64Key = 0x0100,
            WOW64_Res = 0x0300,
            Read = 0x00020019,
            Write = 0x00020006,
            Execute = 0x00020019,
            AllAccess = 0x000f003f
        }
        #endregion
    }
}
Here is an example of using these extension functions.
using Microsoft.Win32;

namespace Read64bitRegistryFrom32bitApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string value64 = string.Empty;
            string value32 = string.Empty;

            byte[] byteValue64 = new byte[1024];
            byte[] byteValue32 = new byte[1024];
            RegistryKey localKey = Registry.LocalMachine;
            
            localKey = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
            if (localKey != null)
            {
                value32 = localKey.GetRegKey32("RegisteredOrganization");
                value64 = localKey.GetRegKey64("RegisteredOrganization");

                // byteValue32 = localKey.GetRegKey32AsByteArray("DigitalProductId"); // Key doesn't exist by default in 32 bit
                byteValue64 = localKey.GetRegKey64AsByteArray("DigitalProductId");
            }
        }
    }
}
If anything is confusing please comment.
Resources:
  • RegOpenKeyEx Function – http://msdn.microsoft.com/en-us/library/ms724897%28v=VS.85%29.aspx
  • RegQueryValueEx Function – http://msdn.microsoft.com/en-us/library/ms724911%28VS.85%29.aspx
  • http://www.pinvoke.net/default.aspx/advapi32/RegQueryValueEx.html
  • http://www.pinvoke.net/default.aspx/advapi32/RegOpenKeyEx.html
  • http://www.pinvoke.net/default.aspx/advapi32/RegCreateKeyEx.html
  • http://www.pinvoke.net/default.aspx/advapi32/RegCloseKey.html
  • http://stackoverflow.com/questions/1470770/accessing-registry-using-net
  • http://connect.microsoft.com/VisualStudio/feedback/details/400597/registeredorganization-reg-key-on-x64-vista-7

Copyright ® Rhyous.com – Linking to this post is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.


Installing and testing PC-BSD 8.2 RC2

I am installing PC-BSD 8.2 RC2.

I really love that I can just choose ZFS as the file system and I don’t have to really make a huge effort to get it to work.

I found polish issues, but nothing serious yet.

Take a moment to install and share your experience here:

http://forums.pcbsd.org/showthread.php?p=85374&posted=1#post85374