Executing commands when starting cmd.exe

If you use the command prompt in Windows often, you might find it useful to setup a script to fire when it’s opened. Something similar to a bashrc file getting executed in Linux when you login. You can setup something like this using the registry.

Crack open regedit and navigate to:
HKEY_CURRENT_USER\Software\Microsoft\Command Processor

Once here, you can add a value called AutoRun and provide the path to the script to run. For example, if you want to run C:\myScript.bat when the prompt is open, you’d create a new string value called AutoRun with a value of C:\myScript.bat

Click here for more information about cmd.exe

Taking a screenshot using XNA 4.0 and C#

I’ve been working a lot on an old game that a few friends and I made in college. We ported it to C# using the XNA framework and it’s working great! You can read more about that project by clicking here.

The old code was C++ with OpenGL. One of the functions I had a hard time porting was taking a screenshot. Older versions of XNA seemed to have a way to do it, but the newest version, 4.0, didn’t have an easy way to do it. Here’s what I ended up doing:
public void ScreenShot(string prefix) {
    #if WINDOWS
    int w = GraphicsDevice.PresentationParameters.BackBufferWidth;
    int h = GraphicsDevice.PresentationParameters.BackBufferHeight;

    //force a frame to be drawn (otherwise back buffer is empty)
    Draw(new GameTime());

    //pull the picture from the buffer
    int[] backBuffer = new int[w * h];
    GraphicsDevice.GetBackBufferData(backBuffer);

    //copy into a texture
    Texture2D texture = new Texture2D(GraphicsDevice, w, h, false, GraphicsDevice.PresentationParameters.BackBufferFormat);
    texture.SetData(backBuffer);

    //save to disk
    Stream stream = File.OpenWrite(prefix + "_" + Guid.NewGuid().ToString() + ".png");
    texture.SaveAsPng(stream, w, h);
    stream.Close();

    #elif XBOX
    throw new NotSupportedException();
    #endif
}

Here’s an article you can check out that explains the new behavior:
http://blogs.msdn.com/b/shawnhar/archive/2010/03/30/resolvebackbuffer-and-resolvetexture2d-in-xna-game-studio-4-0.aspx

ASP.NET resources

I haven’t done a lot of .NET since back in the good old 2.0 days.

The jobs I held right after graduating from college were extremely heavy on .NET and MS SQL technologies. I joined Intel in 2005, about a year before the .NET 3 framework was released (with the WCF, WWF, WPF, etc). At Intel I mostly worked with COM technologies; C++ and VBScript. I got to do some .NET development, but it was pretty limited.

I joined Go Daddy in 2008 where I mostly did C++ and PHP on Linux. I really enjoyed it but when I got the chance to switch teams at the end of 2010, I was really excited. All of the code for my new team is in C#/.NET. Since then, I’ve been digging in and figuring out all the new features I’ve missed out on.

I found a lot of great resources when I was looking at all the new features of .NET 4.
Click here for a blog entry with a TON of info about .NET 4
Click here for an article which summarizes the new .NET 4 Base Class Library
Click here for an article about new releases, including the ASP.NET MVC 3 framework
Click here to check out a Search Engine Optimization tool

Of all those links above, the one I’d recommend the most is the last one (the Search Engine Optimization tool). This tool is awesome. You enter the landing page URL and it’ll crawl and download your entire site. After it’s done that, It’ll examine what was downloaded for issues.

It breaks problems it finds into categories. For example, it’ll find any pages you forgot to put a title or description. It’ll also help find broken links.

Most of what the tool does is what’s described in the Google document I linked to in a previous article about optimizing your website (It had a couple tips about getting started; like using Google Analytics and different kinds of webmaster tools).
Click here to check out my previous article about optimizing your website

However, having the tool readily available (which takes about 30 seconds or less to run against my website) is great. It saves everything it finds so you can run it multiple times and visibly see the progress you’re making. No matter how good a job you do, you’re going to miss something (and this’ll help you find it).

I recently upgraded to .NET 4 on my Go Daddy account and I’ve really been liking it.
Click here to read about my .NET 4 migration experience

One of the features I am starting to fall in love with (which I hated when it first came out) is LINQ. This is such a great query language. My favorite part so far is dealing with XML using LINQ. My co-worker showed me this sweet tool called LINQPad. It has a lot of great LINQ examples and works similar to Query Analyzer for MS SQL.
Click here to check out LINQPad

That’s all for now, time to get some sleep!

Getting the assembly version in ASP.NET

I typically update the .NET assembly for this website by hand whenever I have a new major release and I’d like to be able to get the version.

Unlike a Windows Forms application, there is no Application.ProductVersion. I Googled for answers and it looked like I needed to do this:
using System.Reflection;
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();

However, that causes a SecurityException to be thrown. I’m running my website at Go Daddy in a shared hosting environment and the permissions are limited.

Here’s how I solved the problem:
using System.Reflection;
Type t = typeof(*pick any class that's in the assembly you want the version of*);
AssemblyName an = new AssemblyName(Assembly.GetAssembly(t).FullName);
string version = an.Version.ToString();

Some examples that I found use Assembly.GetExecutingAssembly(), but I found that this doesn’t work. It’ll compile, but always returns back 0.0.0.0 as the version. That’s why you’ll have to use Assembly.GetAssembly() and provide a type as shown above.

Simple object pool in C++

This is a simple object pool I use in my C++ code. It provides good performance and avoids memory fragmentation. I based the code on an example from Game Programming Gems 4.

#include <stdlib.h>
#include <list>

using namespace std;

class TestObject {
    public:
        int _TestValue;
        TestObject() {
            _TestValue = 0;
        }
};

template<class OPDataType> class ObjectPool {
    protected:
        OPDataType* _ObjectData;
        OPDataType** _ObjectFree;
        int _ObjectCount, _Top;

    protected:
        void FreeAll() {
            int i = (_ObjectCount - 1);

            for (_Top = 0; _Top < _ObjectCount; _Top++) {
                _ObjectFree[_Top] = &_ObjectData[i--];
            }
        }

    public:
        void FreeInstance(OPDataType* instance) {
            if (instance && (_Top < _ObjectCount) && (instance >= &_ObjectData[0]) && (instance <= &_ObjectData[_ObjectCount - 1])) {
                _ObjectFree[_Top++] = instance;
            }
        }

        OPDataType* NewInstance() {
            if (_Top > 0) {
                return _ObjectFree[--_Top];
            }
            return 0;
        }

        ObjectPool(int count) {
            _ObjectData = new OPDataType[count];
            _ObjectFree = new OPDataType*[count];

            _ObjectCount = count;

            FreeAll();
        }

        virtual ~ObjectPool() {
            delete[] _ObjectData;
            delete[] _ObjectFree;
        }
};

#define TEST_POOL_SIZE 200

int main (int argc, char** argv) {
    ObjectPool<TestObject> pool(TEST_POOL_SIZE);
    list<TestObject*> objects;

    for (int i = 0; I < TEST_POOL_SIZE; i++){
        TestObject* test = pool.NewInstance();
        test->_TestValue = rand();
        objects.push_back(test);
    }

    list<TestObject*>::iterator it = objects.begin();
    while (it != objects.end()) {
        pool.FreeInstance( (*it) );
        ++it;
    }

    objects.clear();
    return 0;
}

Handling NIC information on Windows

If you need to programmatically get info about the NIC, you can find it in the registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}

You can enumerate through all of the sub-keys; they’re labeled as 0000, 0001, 0002, etc. If you’re on Windows Vista / Server 2008, be sure to catch exceptions because there is a sub-key called Properties that you will get an access violation on.

For example, in my Shuttle SG33G5, I have my primary NIC under a sub-key called 0004. I can tell it’s my primary NIC because of the DriverDesc value. From this key, you can set duplex, buffer sizes, and other NIC driver settings.

Just as an example, lets say you want to set your NIC to be 100Mbps full duplex. Before you make an automated solution, you can see the values passed to the NIC driver in the sub-key called NDI under the sub-key Params. In my case, I would need to open this key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0004\Ndi\Params\*SpeedDuplex

There is a value here called default and it’s set to 0. Looking at the sub-key called enum, there are some values I could use:
0 = Auto Negotiation
1 = 10 Mbps Half Duplex
2 = 10 Mbps Full Duplex
3 = 100 Mbps Half Duplex
4 = 100 Mbps Full Duplex

So when it’s time for automating this, my code would:
1) Open this key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}
2) Go through each sub-key (0000,0001,0002…), until it finds a value DriverDesc called “Generic Marvell Yukon Chipset based Ethernet Controller”.
3) I know for this card, the value 4 is 100Mbps. So I can set the value data for “*SpeedDuplex” to 4. The full path to this registry value is
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0004\*SpeedDuplex
4) After making the change you can restart your network interface by making a shell execute call to netsh.exe
netsh interface set interface "Local Area Connection" DISABLED
netsh interface set interface "Local Area Connection" ENABLED

This article is basically documentation of some code my friend Daymion wrote. You can probably achieve the same result with WMI, but it’s a lot nastier to do that in C or C++.

chroot and mounting

Recently I’ve ran into problems when I am using a 32-bit OS and dealing with 64-bit files. In my case, I have a layout like this:
/tmp/64-bit-files-here

The main system I have is 32-bit and I untar a 64-bit root file system to the folder /tmp/64-bit-files-here. At this point, I want to chroot and run a shell script but I’m unable to. Since I would be chroot-ed, the binaries that would be used are coming from /tmp/64-bit-files-here/bin and /tmp/64-bit-files-here/sbin and they are 64-bit and won’t run.

Symlinks won’t work because as soon as you chroot, that path is invalid. I had to read some man pages and do some searching, but I found a solution. The way I got around this is by using mount. I made the appropriate folders in my 64-bit file system folder:
mkdir /tmp/64-bit-files-here/tmp-root
mkdir /tmp/64-bit-files-here/tmp-root/bin
mkdir /tmp/64-bit-files-here/tmp-root/etc
mkdir /tmp/64-bit-files-here/tmp-root/lib
mkdir /tmp/64-bit-files-here/tmp-root/sbin
#create other directories here
cp /tmp/MyShellScript.sh /tmp/64-bit-files-here/tmp-root

Then I used mount to provide a link-like functionality. I mount the 32-bit executable directories but I load the 64-bit file directories such as etc. Note the –bind argument:
mount --bind /bin /tmp/64-bit-files-here/tmp-root/bin
mount --bind /tmp/64-bit-files-here/etc /tmp/64-bit-files-here/tmp-root/etc
mount --bind /lib /tmp/64-bit-files-here/tmp-root/lib
mount --bind /sbin /tmp/64-bit-files-here/tmp-root/sbin
#mount other directories here
chroot /tmp/64-bit-files-here/tmp-root /MyShellScript.sh

When you do chroot, you’ll have access to run the 32-bit executables but modify your 64-bit file system. This specific example might only be useful if you’re doing imaging but the chroot / mount example should be useful for any kind of scripting.

Cron Quick Reference

On Linux you can schedule tasks using cron. To open up cron just run:
crontab -e

Something useful to paste into your crontab is this line:
#mh hd dm my dw command

Like most Linux configuration files, lines starting with # are comments. This comment shown above is helpful to show you the field order that a cron line is broken down into. Each cron is on it’s own line and it’s parameters are space separated. Here’s an explanation of the abbreviations found in the comment:
mh - minute of the hour (0-59)
hd - hour of the day (0-23)
dm - day of the month (1-31)
my - month of the year (0-12)
dw - day of the week (0-6, with 0 being Sunday)
command - command to run

For each value you can put an asterisk if you always want it to run. For example, if you put an asterisk for mh, the command will run every minute (see example 1).

You can also add a list by comma seperating values …
(see example 2).

Alternatively, you can use a range by using the hyphen …
(see example 3 and 4).

The last way you can provide a value is using a step value. For example, if you want to run a command every other day, you can basically provide the dm field with */2
(see example 5).

Here are a few examples that should help show how crontab entries work:
#mh hd dm my dw command

#example 1: executes once per minute (every day)
* * * * * echo "This is a test"

#example 2: executes every day at 10am, noon, and 2pm
0 10,12,14 * * * echo "This is a test"

#example 3: executes every day on every hour between at 10am and 2pm
0 10-14 * * * echo "This is a test"

#example 4: executes Monday thru Friday at 2pm
0 14 * * 1-5 echo "This is a test"

#example 5: executes every other day at 2pm
0 14 */2 * * echo "This is a test"

#example 6: executes every day at 2pm and logs
0 14 * * * echo "This is a test" >> /var/log/results.log 2>&1

Note that in example 6 above, I redirect standard output and standard error into a log file. This is a great way to grab the output from your cron.

This information was put together using these articles:
http://troy.jdmz.net/cron/
http://www.unixgeeks.org/security/newbie/unix/cron-1.html