Archive for August 2010

Installing and Using Postgresql on FreeBSD

Postrgesql is an excellent alternate to MySQL.

It is BSD Licensed instead of GPL, which is especially more attractive if you need to distribute the database software.  In fact, if you are reselling a product, and paying a license fee to MySQL you probably should save your company the money and move to Postgresql.  LANDesk, the company I work for, had just such an experience with our Management Gateway device.

Setting up FreeBSD

Ok, so I already have a post on this here:

How do I install FreeBSD?

Also, make sure to download ports.

What are the first commands I run after installing FreeBSD

Installing Postgresql on FreeBSD

As always, installing software on FreeBSD is simple using the ports system.

#
#
cd /usr/ports/databases/postgresql84-server
make WITH_OPTIMIZED_CFLAGS=true BATCH=yes install clean

Post-installation Setup

There a few post-installation steps.

Initialize the database

# /usr/local/etc/rc.d/postgresql initdb

Or starting with FreeBSD 8.1, you can now run this command:

# service postgresql initdb

Make any changes to the postgresql.conf

The postgresql.conf is located in /usr/local/pgsql/data.

Open the file and read through it and make any desired changes.

Common Changes

Here are two common changes.

Enabled remote connections

If the database is to be accessed by the network, then you should at least uncomment the setting #listen_addresses = 'localhost' and change it to listen_addresses = '*'.

Changing the default TCP Port

Uncomment the setting #port = 5432 and change the port number to the desired value.

Configure password authentication

  1. Change to the /usr/local/etc/pgsql/data directory.
  2. Edit the pg_hba.conf and change the default authentication method to something more secure, such as md5.
    # TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD# "local" is for Unix domain socket connections only
    local   all         all                               md5
    # IPv4 local connections:
    host    all         all         127.0.0.1/32          md5
    # IPv6 local connections:
    host    all         all         ::1/128               md5
    

Configure the postgresql server service to start on reboot

Add the string postgresql_enable="YES" to /etc/rc.conf.

# echo ‘postgresql_enable=”YES”‘ >> /etc/rc.conf

Creating a Database

To create a database, su to pgsql and run createdb.

#
$
su pgsql
createdb MyDBName

Note: Similarly, use dropdb to drop a database.

Creating a User or Role

To create a user, su to pgsql and run createuser.

#
$
su pgsql
createuser -P

Enter name of role to add: MyUserOrRoleName
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) y

If you are not using password authentication, then you can exclude the -P.

Using psql client

A simple way to connect to postgresql is using the shell-based client, psql.

Connecting

To connect, use the following command.

$ psql MyDBName

MyDBName-#

You are now at the psql prompt.

Connecting as a different user

To connect, use the following command.

$ psql MyDBName MyUserOrRoleName

MyDBName-#

You are now at the psql prompt.

Changing a a User or Role Password

To change a password, use the following syntax.

MyDBName-# alter role pgsql with password ‘pw’;

Show Databases

To show database, simply type \l, (which is the lowercase letter L not the number 1) which is short hand in psql for this query:
SELECT datname FROM pg_database;

MyDBName-# \l

Show Tables

To show tables in the current database, simply type \d, which is short hand in psql for this query:
SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';

MyDBName-# \d

Show Tables

To show columns of a table in the current database, simply type \d table, which is short hand in psql for this query:
SELECT column_name FROM information_schema.columns WHERE table_name ='table';

MyDBName-# \d MyTableName

Creating a Table

To create a table, use the following syntax:

MyDBName-# CREATE TABLE Users (
FirstName text,
LastName text,
DateOfBirth date
);

Inserting data into a Table

To insert data into a table, use the following syntax:

MyDBName-# CREATE TABLE Users (
UserId serial,
FirstName text,
LastName text,
DateOfBirth date
);

Ok, from here you should be able to start figuring everything out on your own.

Resources:
http://www.postgresql.org/docs/8.4/interactive/index.html


Copyright ® Rhyous.com – Linking to this page 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.

FreeBSD now has the service command

So I was reading the FreeBSD 8.1 release notes today and noticed that FreeBSD added the services command.

Basically there are a lot of start up scripts in /etc/rc.d that are enabled in the /etc/rc.conf.  However, if you wanted to restart them, you always had to run /etc/rc.d/someservice start.  And if you didn’t remember the exact service syntax, you had to ls the dir.

Anyway, now with the services command, you can simple type this:

service someservice start

service someservice restart

I am familiar with this already as that is how Red Hat gets things done with their services.

If it was just /etc/rc.d scripts that this worked for, then this command doesn’t really save much typing. But it also works for installed services or services from ports. The start up scripts for services for ports are in /usr/local/etc/rc.d.

So after installing something you can use the simple service command to start it, instead of typing in the longer path.

Of course, service has other features, such as listing the start up scripts in the two directories: /etc/rc.d and /usr/local/etc/rc.d

Take a second to run man service to see all its options.

Comments on 7 Reasons to Switch to the Dvorak Keyboard Layout

Ok, so I recently read an article that had seven resason to switch to the Dvorak keyboard layout

I will list the authors reasons and comment on them.  Please see the original post for the complete detail:
7 Reasons to Switch to the Dvorak Keyboard Layout

Reason 1 -QWERTY was designed for the typewriter, not the typist.

This is true.  But I am not a typewriter or a typist.  I am a developer.  While the Dvorak keyboard is probably faster for a typist that writes normal words and text, is it faster for a computer programmer ? Maybe.  Is it the fastest possible layout for a programmer?  I doubt it.

Also, a semicolon is common for programming languages and in QWERTY, it is on the home row.  In Dvorak, it is not.  Dvorak makes some common programming keys harder to reach.

So no, Dvorak was not designed for a programmer.

There is a programming version of the Dvorak layout: Programmer Dvorak layout.

This may address some of these issues.  Is that what the article intends? I don’t think so, because I couldn’t find an option on Windows 7 64 bit to use that layout.

Reason 2. Dvorak increases your speed.

Also, you forget about people who don’t really type that much, or that type the special keys of a programming language often.  Think of all the keyboard shortcuts.  CTRL + C, CTRL + P, CTRL + A.  WIN + E, WIN + D, etc…

Look at this quote from the article:

70% of keystrokes are on the home row; 22%, on the top row; 8%, on the bottom.

Does a programming language have the same the percentages?

So really, shouldn’t there be a separate keyboard layout for programmers than for typist?

if (keyboardLayout == sucks)
{
    DoChangeLayout();
    DoLearnKnewLayout();
}
else
{
    IgnoreTheSuck();
}

Reason 3 – Dvorak lessens your mistakes.

I have to agree, that having the most common keys on the home row might decrease mistakes.

However, many of my mistakes are usually due to the really far away keys, such as _ – + | \ } ] ~ ` ^ and both layouts leave those characters in the same place.  What if there was another button, like Caps Lock or SHIFT or CTRL or ALT, that moved those characters to the home row.

Reason 4. Dvorak is more comfortable and better for your health.

I think I agree with this.  I wonder if people with Carpal Tunnel Syndrome could be prevented or lessened by this.

I do have to say that a natural keyboard helps but is not enough.

A better shape for a keyboard would be if the keyboard were cut in half and the two piece leaned together like this: /\

Or maybe some type of ball with keys:  ( )

Reason 5. Switching to Dvorak is easier than ever.

Really.  I think it is harder than ever.  I agree that some of the features mentioned make it easier in ways, but other features, such as keyboard shortcuts make it harder. When I switch to Dvorak, does my keyboard shortcut keys switch too?  Unfortunately no, they don’t.

Until that is fixed, it is going to be difficult to move.

Also, I have used QWERTY since i was 7.  Yes, in 1983 my family had a computer and a digital typewriter. Both were QWERTY.  So after 26 years of using QWERTY, it is not easy to switch to anything.

Everybody has a computer.  Sometimes you use a friends computer. Of course they use QWERTY, so you have to remember how to use that.  Or if you have to fix someone’s computer.  Or worse, you remote control someone who is using QWERTY but you are using Dvorak.  How would that work?

Reason 6. Dvorak is cool.

Is this really a reason?  This is an opinion.

And this quote is just silly.

You’ll also be in the company of some cool people, including Bram Cohen, inventor of BitTorrent; Matt Mullenweg, lead developer of WordPress; and Barbara Blackburn, world’s fastest typist.

Yeah, but if I use QWERTY, the company is even that much better since it encompasses 99.99999% of the English typing world, right?

Reason 7. Using Dvorak is a noble cause.

Ok, so digging wells in Africa is a noble cause.  Manning a soup kitchen in your spare time is a noble cause.  But using a different type of keyboard than everyone else is just annoying. Sorry.

My Experience

Ok, sorry to rag on this article, but I tried Dvorak for a while and I am less than impressed.

Lets get down to what we really want.  We want to be faster at typing whatever it is we type. We want to have more comfort when we type.

My Keyboard Gripes

  1. Why is the spacebar so frieking wide. Why can’t it be half the size, so my thumbs can be used to push CTRL or ALT or the Windows key or SHIFT. Why do both my thumbs need access to the space bar?
  2. My hands do not fit well on a keyboard. Not even an ergonomic or natural keyboard. A better shape for a keyboard would be if the keyboard were cut in half and the two piece leaned together like this: /\I can’t find one.
  3. There should be a mouse pad, like on laptops, but directly below the space bar. Why do I have to have to separate interface devices that are both inefficient.  Taking my right hand away from the keyboard to use the mouse is one of the biggest faults of keyboards.Oh…there is: http://www.newegg.com/Product/Product.aspx?Item=N82E16823166076&nm_mc=OTC-Froogle&cm_mmc=OTC-Froogle-_-Keyboards-_-Adesso-_-23166076

    But is there an ergonomic version? I guess so.
    http://www.newegg.com/Product/Product.aspx?Item=N82E16823166028

    But they are expensive.

  4. As a programmer, I want to be able to have one key for these three keys: ();Most the improvements in speed for programming are in the IDEs and so the keyboard layout is becoming insignificant.  Especially with IDEs with Intellisense.

Maybe you like Dvorak and maybe you have never heard of it.  Either way.

All right.  I am done rambling.

A windows annoyance: Copying folders with thousands of files

Ok, so have you ever started to copy a folder from the network and had it crash on you? And the folder of course has hundreds of subfolders and thousands of files, so you have to copy it again.

Why doesn’t windows handle this better. Why don’t I get a nice prompt that says: The copy failed…do you want to try again?  Yes / No

If i drag the folder over again, it seems to copy everything and give me annoying prompts for whether I want to overwrite the folder and the prompts can be endless.

I know I could avoid this by zipping the directory first, but really, zipping 1.5 GB of thousands of files takes even longer.

Sorry to drop a complaint today, but restarting massive folder transfers seems like an area where Microsoft has really not put any effort.

I will say that on my Windows 7 64 bit box, the number of annoying prompts to copy and replace were far less if I checked the box to not copy, so that is a positive.

Why my categories span such broad topics?

Hey all,

I recently noticed I was added to another blogs blogroll.  It was funny what he said:

Rhyous’s 127.0.0.1 or ::1 covers an interesting mix of FreeBSD, PC-BSD, C# and even Windows 7 issues. An unusual mix…

Well, here is the deal.

  1. One of my hobbies is learning about open source.  My operating system of choice is FreeBSD and I am partial to PC-BSD for desktop use as well.  So naturally, I am going to post about that.
  2. However, I work for LANDesk as a Developer (previously I was a Level III support engineer), using primarily C#.  We have a lot of legacy C++ code as well.  LANDesk is mostly a Microsoft shop.  Our server software runs on Windows Servers only and we support MS SQL and Oracle back ends.  Our agent goes to Windows, Mac, Linux, and some Unix devices.  Yes, I have had the Linux version of the LANDesk agent running on FreeBSD using Linux Emulation.  I am probably going to post more on WIX now as I am on a development team that deals with our product installation.
  3. I am also just a techie in the first place so I have lots of tech interests outside my career and my hobbies.
  4. I now realize the importance of experience and I want to increase my experience and make my experience more meaningful by retaining it.  I used to do support for Nortel Networks equipment before coming to LANDesk so I have intense networking skills that are uncommon among developers. I also have 10+ years of support and troubleshooting which gives my a unique perspective.  The more you do something the easier it is.  I have a lot of experiences where some support guys were working on something for days and I walk over and solve the problem in five minutes.  It is something that comes with 10+ years experience or troubleshooting. No, that is not unique to me.  LANDesk tech support engineers are some of the best out there and there are a number of guys that can do that here.  But all of them have about 10+ years experience.
  5. I help others with their computer issues which often leads to figuring things out.

My one regret is that I didn’t start my blog in 10+ years ago.  Think of the content I would have had.

So anytime I have to look something up or research a problem and the first or second Google link is not an exact answer, I am going to blog about it, whatever the topic may be.

Netcraft: Most Reliable Hosting Company Sites in July 2010

Netcraft has posted the most reliable hosting companies and of the top ten, five are using FreeBSD.
Most Reliable Hosting Company Sites in July 2010

I was informed about this by an email to the FreeBSD advocacy mailing list: advocacy@freebsd.org

Seems that this is something worth pointing out to companies who ask whether to use Windows, Linux, or BSD.

A DottedDecimal Object

Ok, so there are a lot of objects that are represented in dotted decimal notation.  The most common are versions and IP addresses.

Version 3.1.9.27

IP Address: 192.168.0.1

I have to wonder why I have never found in the Standard C++ Library, or in the C# libraries an object for these?  Are they there and I just don’t know how to find them.  It seems they are always just treated as Strings and this makes no sense to me.  Also these seem common enough that they should be standard objects in all languages.

Which IP Address is greater?

192.168.0.2
192.168.0.100

Well, since these are usually treated as strings, then .2 is greater than .100.  Unfortunately that is not correct.  We all know that .100 is greater.

So I created some objects that overload the >,>=,<=,<,==,!= functions.  Maybe these are completely finished, but hey, they are a start. I created C# and C++ versions.  The C# is first, scroll down if you are looking for C++ versions. This is really great for versions that can be different.  However, I think that for an IP address object, that because it is limited to three characters and each section is one byte and only can be seen as 0-255, that a very efficient object could be created, but for now, a more generic DottedDecimal object is fine, though if you had a large list of IP addresses, you may want that efficiency. Also, this is only tested with digits 0-9, not hex, so there is plenty more work to do, but usually version are just 0-9, though sometimes people throw in an "a" or "b" build such as 1.0.0.1a.  That is not handled yet.  So again, much more work to do.  But for my needs these are more than enough for now. If there are already objects like this that are awesome, efficient, tested, and free, let me know.

C# DottedDecimal object for IP address and Versions

For C#, I implement a lot of interfaces too as you can see in the object.

DottedDecimal.cs

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

namespace DottedDecimalProject
{
    public class DottedDecimal
        : IComparable, IComparable<DottedDecimal>, IComparable<String>, ICloneable, IEquatable<string>, IEquatable<DottedDecimal>
    {
        #region Member Variables
        List<long> _DecimalValues;
        CompareDirection _CompareDirection = CompareDirection.LeftToRight;
        #endregion

        #region Constructors
        /*
         * The default constuctor.
         * The default compare direction is left to right
         * No values are added by default.
         */
        public DottedDecimal()
        {
        }

        /*
         * This constructor takes a string in this regex format
         * [0-9]+(\.[0-9])*
         */
        public DottedDecimal(String inDottedDecimalString)
        {
            _DecimalValues = null;
            _DecimalValues = StringToDottedDecimalList(inDottedDecimalString);
        }

        /*
         * This constructor takes two parameters.
         * Parameter 1 is a string in this regex format: [0-9]+(\.[0-9])*
         * Parameter 2 sets the compare direction. See this.Direction.
         */
        public DottedDecimal(String inDottedDecimalString, CompareDirection inCompareDirection)
        {
            _CompareDirection = inCompareDirection;
            _DecimalValues = null;
            _DecimalValues = StringToDottedDecimalList(inDottedDecimalString);
        }
        #endregion

        #region Properties
        /*
         * Returns the dotted decimal object in string format.
         */
        public String DottedDecimalString
        {
            get { return DottedDecimalListToString(); }
            set { _DecimalValues = StringToDottedDecimalList(value); }
        }

        /*
         * The decimal values are stored in order.  If LeftToRight, the left
         * most value is first.  If RightToLeft, the right most value is first.
         */
        public List<long> DecimalValues
        {
            get { return _DecimalValues; }
            set { _DecimalValues = value; }
        }

        /*
         * Determines whether to compare left to right or right to left.
         *
         * LeftToRight - 1.0.0.1 is greater than 1.0.0.0
         *               1.0.10 is greater than 1.0.0.27
         *
         * RightToLeft - 1.0.0.1 is less than 1.0.0.0
         *             - 1.0.10 is less than 1.0.0.27
         */
        public CompareDirection Direction
        {
            get { return _CompareDirection; }
            set
            {
                if (!(this.Direction == value))
                {
                    this._DecimalValues.Reverse();
                }
                _CompareDirection = value;
            }
        }
        #endregion

        #region Functions
        /*
         * Verifies that the CompareDirection values match between two DottedDecimal objects.
         */
        private static bool CompareDirectionsMatch(DottedDecimal left, DottedDecimal right)
        {
            if (left.Direction == right.Direction)
                return true;
            else
                return false;
        }

        /*
         * Overloads the greater than operator (>) to allow for a syntax as follows:
         *
         *      bool b = dd1 > dd2;
         */
        public static bool operator >(DottedDecimal left, DottedDecimal right)
        {
            int count = (left.DecimalValues.Count > right.DecimalValues.Count) ? right.DecimalValues.Count : left.DecimalValues.Count;

            for (int i = 0; i < count; i++)
            {
                // If left side is greater then true;
                if (left.DecimalValues&#91;i&#93; > right.DecimalValues[i])
                {
                    return true;
                }
                // If right side is greater then false;
                if (left.DecimalValues[i] < right.DecimalValues&#91;i&#93;)
                {
                    return false;
                }
                // If it is equal, check the next decimal values over
                if (left.DecimalValues&#91;i&#93; == right.DecimalValues&#91;i&#93;)
                {
                    continue;
                }
            }

            if (left.DecimalValues.Count > right.DecimalValues.Count)
            {
                // If the left side has the same values as the right,
                // but then has more values, true.
                return true;
            }

            if (left.DecimalValues.Count < right.DecimalValues.Count)
            {
                // If the left side has the same values as the right,
                // but then the right side has more values, false.
                return false;
            }
            // If we get here both sides are equals, so false
            return false;
        }

        /*
         * Overloads the less than operator (<) to allow for a syntax as follows:
         *
         *      bool b = dd1 < dd2;
         */
        public static bool operator <(DottedDecimal left, DottedDecimal right)
        {
            int count = (left.DecimalValues.Count > right.DecimalValues.Count) ? right.DecimalValues.Count : left.DecimalValues.Count;
            for (int i = 0; i < count; i++)
            {
                // If right side is greater then true;
                if (left.DecimalValues&#91;i&#93; < right.DecimalValues&#91;i&#93;)
                {
                    return true;
                }
                // If the left is greater then false;
                if (left.DecimalValues&#91;i&#93; > right.DecimalValues[i])
                {
                    return false;
                }
                // If it is equal, check the next decimal values over
                if (left.DecimalValues[i] == right.DecimalValues[i])
                {
                    continue;
                }
            }

            if (left.DecimalValues.Count > right.DecimalValues.Count)
            {
                // If the left side has the same values as the right,
                // but then has more values, false.
                return false;
            }

            if (left.DecimalValues.Count < right.DecimalValues.Count)
            {
                // If the left side has the same values as the right,
                // but then the right side has more values, true.
                return true;
            }
            // If we get here both sides are equals, so false
            return false;
        }

        /*
         * Overloads the equals operator (==) to allow for a syntax as follows:
         *
         *      bool b = dd1 == dd2;
         */
        public static bool operator ==(DottedDecimal left, DottedDecimal right)
        {
            // If there are more values in either side, they aren't equal
            if (!(left.DecimalValues.Count == right.DecimalValues.Count))
            {
                return false;
            }
            for (int i = 0; i < left.DecimalValues.Count; i++)
            {
                // If any one value is not equal, then false
                if (left.DecimalValues&#91;i&#93; != right.DecimalValues&#91;i&#93;)
                {
                    return false;
                }
            }
            // If you get here they are all equal so true
            return true;
        }

        /*
         * Overloads the not equals operator (!=) to allow for a syntax as follows:
         *
         *      bool b = dd1 != dd2;
         */
        public static bool operator !=(DottedDecimal left, DottedDecimal right)
        {
            // If there are more values in either side, they aren't equal
            if (!(left.DecimalValues.Count == right.DecimalValues.Count))
            {
                return true;
            }
            for (int i = 0; i < left.DecimalValues.Count; i++)
            {
                // If any one value is not equal, then true
                if (left.DecimalValues&#91;i&#93; != right.DecimalValues&#91;i&#93;)
                {
                    return true;
                }
            }
            // If you get here they are all equal so false
            return false;
        }

        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

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

        public override string ToString()
        {
            return DottedDecimalString;
        }

        /*
         * Appends a new string value to the left side of a DottedDecimal object.  Adding "12" to
         * 1.0.0 makes it 1.0.0.12.  Because it is a string, you can also add multiple values at
         * a time so adding the string "12.24" makes 1.0.0.12.24.
         */
        void AddToLeftSide(String inVal)
        {
            foreach (string decimalString in inVal.Split('.'))
            {
                _DecimalValues.Insert(0, Convert.ToInt64(inVal));
            }
        }

        /*
         * Appends a new value to the left side of a DottedDecimal object.  Adding 12 to
         * 1.0.0 makes it 1.0.0.12
         */
        public void AddToLeftSide(long inVal)
        {
            _DecimalValues.Insert(0, inVal);
        }

        /*
         * Appends a new string value to the right side of a DottedDecimal object.  Adding "12" to
         * 1.0.0 makes it 12.1.0.0.  Because it is a string, you can also add multiple values at
         * a time so adding the string "12.24" makes 12.24.1.0.0.
         */
        public void AddToRightSide(String inVal)
        {
            foreach (string decimalString in inVal.Split('.'))
            {
                _DecimalValues.Add(Convert.ToInt64(decimalString));
            }
        }

        /*
         * Appends a new value to the right side of a DottedDecimal object.  Adding 12 to
         * 1.0.0 makes it 12.1.0.0
         */
        public void AddToRightSide(long inVal)
        {
            _DecimalValues.Add(inVal);
        }

        private string DottedDecimalListToString()
        {
            string retVal = "";
            if (this.Direction == CompareDirection.LeftToRight)
            {
                foreach (long l in _DecimalValues)
                {
                    if (!retVal.Equals(""))
                    {
                        retVal += ".";
                    }
                    retVal += l;
                }
            }
            else
            {
                for (int i = _DecimalValues.Count - 1; i >= 0; i--)
                {
                    if (!retVal.Equals(""))
                    {
                        retVal += ".";
                    }
                    retVal += _DecimalValues[i];
                }
            }
            return retVal;
        }

        private List<long> StringToDottedDecimalList(String inString)
        {
            List<long> retList = new List<long>();
            foreach (string decimalString in inString.Split('.'))
            {
                retList.Add(Convert.ToInt64(decimalString));
            }
            if (this.Direction == CompareDirection.RightToLeft)
            {
                retList.Reverse();
            }
            return retList;
        }

        #endregion

        #region Interface Functions

        #region IComparable Members
        public int CompareTo(object inOjbect)
        {
            DottedDecimal dd = (DottedDecimal)inOjbect;
            return CompareTo(dd);
        }
        #endregion

        #region IComparable<DottedDecimal> Members
        public int CompareTo(DottedDecimal inDottedDecimal)
        {
            if (this < inDottedDecimal)
                return -1;
            if (this == inDottedDecimal)
                return 0;
            if (this > inDottedDecimal)
                return 1;
            return -2; // Should never get here.
        }
        #endregion

        #region IComparable<string> Members
        public int CompareTo(string inString)
        {
            DottedDecimal dd = new DottedDecimal(inString);
            return CompareTo(dd);
        }
        #endregion

        #region ICloneable Members
        public object Clone()
        {
            return new DottedDecimal(this.DottedDecimalString, this.Direction);
        }

        #endregion

        #region IEquatable<string> Members
        public bool Equals(string inString)
        {
            DottedDecimal dd = new DottedDecimal(inString);
            return this == dd;
        }

        #endregion
        #region IEquatable<DottedDecimal> Members
        public bool Equals(DottedDecimal inDottedDecimal)
        {
            return this == inDottedDecimal;
        }
        #endregion

        #endregion

        #region Enums
        public enum CompareDirection
        {
            LeftToRight,
            RightToLeft
        }
        #endregion
    }
}

C++ DottedDecimal object for IP address and Versions

I tried to overload the common operators, if there is one you would like overloaded, let me know.

DottedDecimal.h

#pragma once
#include <vector>
#include <iostream>
#include "windows.h"

using namespace std;

class DottedDecimal
{
public:
	// Constructors
	DottedDecimal(); // Default constructor
	DottedDecimal(DottedDecimal & inDottedDecimal); // Copy constructor
	DottedDecimal(string inString);
	DottedDecimal(char * inString);
	DottedDecimal(LPTSTR inString);

	// Destructors
	~DottedDecimal();

	// Public functions
	string GetDottedDecimal();

	template <class T>
	void SetDottedDecimal(const T& t);

	vector<long> GetDecimals();

	// Functions Overloading Operators
	friend ostream &operator<<(ostream & dataStream, DottedDecimal & dd);

	friend bool operator==(DottedDecimal & left, DottedDecimal & right);
	friend bool operator!=(DottedDecimal & left, DottedDecimal & right);

	friend bool operator>(DottedDecimal & left, DottedDecimal & right);
	friend bool operator>=(DottedDecimal & left, DottedDecimal & right);

	friend bool operator<(DottedDecimal & left, DottedDecimal & right);
	friend bool operator<=(DottedDecimal & left, DottedDecimal & right);

private:
	// Member Variables
	vector<long> _decimals;

	// Private Functions
	void StringSplit(string inString, string inDelim, vector<string> * outResults);

	template <class T>
	string AnyTypeToString(const T& t);

	template <class T>
	void StringToAnyType(T& t, std::string inString);
};

DottedDecimal.cpp

#include "StdAfx.h"
#include "DottedDecimal.h"
#include <iostream>
#include <sstream>

using namespace std;

DottedDecimal::DottedDecimal()
{
}

DottedDecimal::DottedDecimal(DottedDecimal & inDottedDecimal)
{
	SetDottedDecimal(inDottedDecimal.GetDottedDecimal());
}

DottedDecimal::DottedDecimal(string inString)
{
	SetDottedDecimal(inString);
}

DottedDecimal::DottedDecimal(LPTSTR inString)
{
	wstring ws = wstring(inString);
	string s;
	s.assign(ws.begin(), ws.end());
	SetDottedDecimal(s);
}

DottedDecimal::DottedDecimal(char * inString)
{
	SetDottedDecimal(inString);
}

DottedDecimal::~DottedDecimal()
{
}

string DottedDecimal::GetDottedDecimal()
{
	string retVal = "";
	for (unsigned short i = 0; i < _decimals.size(); i++)
	{
		if (retVal.compare("") != 0)
		{
			retVal += ".";
		}
		retVal += AnyTypeToString(_decimals.at(i));
	}
	return retVal;
}

template <class T>
void DottedDecimal::SetDottedDecimal(const T& t)
{
	_decimals.clear();
	string valueString = AnyTypeToString(t);
	vector<string> * values = new vector<string>();
	StringSplit(valueString, ".", values);
	for (unsigned short i = 0; i < values->size(); i++)
	{
		long l;
		StringToAnyType(l, values->at(i));
		_decimals.push_back(l);
	}
	delete values;
}

vector<long> DottedDecimal::GetDecimals()
{
	return _decimals;
}

ostream &operator<<(ostream & dataStream, DottedDecimal & dd)
{
	dataStream << dd.GetDottedDecimal();
	return dataStream;
}

bool operator==(DottedDecimal & left, DottedDecimal & right)
{
	// If the value count isn't the same, then false
	if (left.GetDecimals().size() != right.GetDecimals().size())
		return false;

	for (unsigned short i = 0; i < left.GetDecimals().size(); i++)
	{
		// If at any time values don't match, return false
		if (left.GetDecimals().at(i) != right.GetDecimals().at(i))
			return false;
	}

	// If you get here they are the same.
	return true;
}

bool operator!=(DottedDecimal & left, DottedDecimal & right)
{
	// If the value count isn't the same, then true
	if (left.GetDecimals().size() != right.GetDecimals().size())
		return true;

	for (unsigned short i = 0; i < left.GetDecimals().size(); i++)
	{
		// If at any time values don't match, return true
		if (left.GetDecimals().at(i) != right.GetDecimals().at(i))
			return true;
	}

	// If you get here they are the same.
	return false;
}

bool operator>(DottedDecimal & left, DottedDecimal & right)
{
	// If one has three values and the other has four, only check three
	short count = (left.GetDecimals().size() <= right.GetDecimals().size() ? left.GetDecimals().size() : right.GetDecimals().size());
	for (unsigned short i = 0; i < count; i++)
	{
		if (left.GetDecimals().at(i) > right.GetDecimals().at(i))
			return true;
	}

	// If you get here, then the checked values were the same.
	// Return true if the left side has more values than the right side.
	if (left.GetDecimals().size() > right.GetDecimals().size() )
		return true;
	else
		return false;
}

bool operator>=(DottedDecimal & left, DottedDecimal & right)
{
	// If one has three values and the other has four, only check three
	short count = (left.GetDecimals().size() <= right.GetDecimals().size() ? left.GetDecimals().size() : right.GetDecimals().size());
	for (unsigned short i = 0; i < count; i++)
	{
		// If any compared value is greater, return true;
		if (left.GetDecimals().at(i) > right.GetDecimals().at(i))
			return true;
	}

	// If you get here, then the checked values were the same.
	// Return true if the left side has more values than or the same values as the right side.
	return left.GetDecimals().size() >= right.GetDecimals().size();
}

bool operator<(DottedDecimal & left, DottedDecimal & right)
{
// If one has three values and the other has four, only check three
	short count = (left.GetDecimals().size() <= right.GetDecimals().size() ? left.GetDecimals().size() : right.GetDecimals().size());
	for (unsigned short i = 0; i < count; i++)
	{
		// If any compared value is less, return true;
		if (left.GetDecimals().at(i) < right.GetDecimals().at(i))
			return true;
	}

	// If you get here, then the checked values were the same.
	// Return true if the left side has less values than the right side.
	return left.GetDecimals().size() < right.GetDecimals().size();
}

bool operator<=(DottedDecimal & left, DottedDecimal & right)
{
	// If one has three values and the other has four, only check three
	short count = (left.GetDecimals().size() <= right.GetDecimals().size() ? left.GetDecimals().size() : right.GetDecimals().size());
	for (unsigned short i = 0; i < count; i++)
	{
		// If any compared value is greater, return true;
		if (left.GetDecimals().at(i) > right.GetDecimals().at(i))
			return true;
	}

	// If you get here, then the checked values were the same.
	// Return true if the left side has less values than or the same values as the right side.
	return left.GetDecimals().size() <= right.GetDecimals().size();
}

// Private

template <class T>
string DottedDecimal::AnyTypeToString(const T& t)
{
	std::stringstream ss;
	ss << t;
	return ss.str();
}

template <class T>
void DottedDecimal::StringToAnyType(T& t, std::string inString)
{
	std::stringstream ss(inString);
	ss >> t;
}

void DottedDecimal::StringSplit(string inString, string inDelim, vector<string> * outResults)
{
	int cutAt;
	while( (cutAt = inString.find_first_of(inDelim)) != inString.npos )
	{
		if(cutAt > 0)
		{
			outResults->push_back(inString.substr(0,cutAt));
		}
		inString = inString.substr(cutAt+1);
	}
	if(inString.length() > 0)
	{
		outResults->push_back(inString);
	}
}

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

How to enumerate installed MSI Products and their MSP Patches using C#

Hey all,

I already have a C++ example of doing this, but that isn’t much help when you have to do it in C#. So it is good to know how to do this in both languages.

Here is what I did:

  1. Created a new C# Console Application project in Visual Studio and named it WindowsInstallerTest.
  2. I added a reference to Microsoft.Deployment.WindowsInstaller. I found this by installing Windows Installer XMl and looking here:
    C:\Program Files (x86)\Windows Installer XML v3.6\bin\Microsoft.Deployment.WindowsInstaller.dll
  3. I added a using statement to the same Microsoft.Deployment.WindowsInstaller.
  4. I enumerated the installed MSI products and then with a foreach loop output data on each.
  5. I enumerated the MSP patches for each product and used another foreach loop to output data on each product.

Here is the simple code.  Hopefully this is enough to get your started with writing code to work with installed MSI products.

using System;
using System.Collections.Generic;
using System.Linq;
// 
// Step 1 - Add a reference to Microsoft.Deployment.WindowsInstaller
//          and then add this using statement.
using Microsoft.Deployment.WindowsInstaller;

namespace WindowsInstallerTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Step 2 - Get the installed MSI Products
            IEnumerable<ProductInstallation> installations = Microsoft.Deployment.WindowsInstaller.ProductInstallation.GetProducts(null, "s-1-1-0", UserContexts.All);
            int i = 0;

            // Step 3 - Loop through the installed MSI Products and output information

            foreach (ProductInstallation installation in installations)
            {
                Console.WriteLine("Id: " + ++i);
                Console.WriteLine("Name: " + installation.ProductName);
                Console.WriteLine("ProductVersion: " + installation.ProductVersion);
                Console.WriteLine("Install Source: " + installation.InstallSource);
                Console.WriteLine("Patches: ");

                // Step 4 - Get the installed MSP Patches for the current installation
                IEnumerable<PatchInstallation> patches = PatchInstallation.GetPatches(null, installation.ProductCode, "s-1-1-0", UserContexts.All, PatchStates.All);

                // Step 5 - Loop through the installed MSP Patches and output information
                int j = 0;
                foreach (PatchInstallation patch in patches)
                {
                    Console.WriteLine("  " + ++j + ": " + patch.DisplayName);
                    Console.WriteLine("  Cache: " + patch.LocalPackage);
                }
                Console.WriteLine();
            }
        }
    }
}