Zach Burlingame
Programming, Computers, and Other Notes on Technology

Getting the Load Count for a DLL

August 17th, 2011

Recently I was trying to unload a DLL from a running process so that I could delete it from the disk but it just wouldn’t delete. Looking at the Modules pane in Visual Studio, I could see that the DLL was still loaded. I doubled and tripled check all of my calls to LoadLibrary for a corresponding call to FreeLibrary, and everything checked out. I needed to figure out what was loading it and where. One of the things that I wanted to know was, “What is the current load count for the DLL?”

Windows maintains a load count for each module on a per-process basis. When the load count reaches zero, the module will be unloaded. The problem is that this load count is not accessible through documented API calls. To get it, you need to use some undocumented structures and API calls from ntdll.dll. Fortunately, like so many other issues you run into, someone else has already run into it and Google knows where they are at. In this case there is a great article here (unfortunately I couldn’t figure out who specifically was the contributing author for that article so that I could give them due props).

The securityxploded article did such a great job of explaining it that I won’t bother to duplicate it here. I based my implementation on theirs however I updated it to support both MBCS and Unicode via the TCHAR macros. It’s available as part of my ModuleUtilities library.

How to Get the Exit Code of a Windows Command Line Application

July 29th, 2011

Since I regularly forget the exact name and end up having to look this up, I’m documenting it here.

If you run an application from the command-line and want to get it’s exit status code, you use:
[bat gutter=”false”]
echo %ERRORLEVEL%
[/bat]

Beware however, if there is an environment variable called errorlevel, because it will override the exit code.

See this SO question for more information.

HOWTO: Enable Wireless Networking on Boot in Ubuntu Linux without NetworkManager

July 25th, 2011

Building on my previous post, this is how to enable wireless networking on boot without NetworkManager.

I’m using WPA in this example, but the setup is similar for WEP and WPA2 using wpa_supplicant.

Remove NetworkManager (Optional)

sudo apt-get remove network-manager

Setup WPA Supplicant

To convert the WPA passphrase into the appropriate form (which is salted with the SSID), you need to use wpa_passphrase. For example:

wpa_passphrase my_ssid my_secret_password

Generates:

network={
ssid=”my_ssid”
#psk=”my_secret_password”
psk=6bea99c21cff6002adc637d93a47fba760ec5e6326cb41784c597b6691ed700d
}

Using this information, you need to setup /etc/wpa_supplicant.conf like so:

ap_scan=1
network={
ssid=”my_ssid”
#psk=”my_secret_password”
psk=6bea99c21cff6002adc637d93a47fba760ec5e6326cb41784c597b6691ed700d
}

Enable Wireless Interface

Put an entry in /etc/network/interfaces for wlan0 (or wlan1, or whatever your wireless interface is).

NOTE: I’ve put the DHCP option here for completeness, but I ran into problems with a Belkin USB F5D9050 wireless adapter not getting an IP successfully, even after it associated with the AP. I’m not sure if this was a problem with the device, the linux driver, or the AP. I ended up adding a DHCP reservation on the AP and then using a static IP configuration on the server.

Option 1: DHCP

auto wlan0
iface wlan0 inet dhcp

Option 2: Static IP

auto wlan0
iface wlan0 inet static
address 192.168.0.20
gateway 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
wpa-driver wext
wpa-conf /etc/wpa_supplicant.conf

Debugging

If you are having issues getting this to work, one debugging trick is to start up wpa_supplicant directly in the foreground and checking the output of dmesg and /var/log/syslog for additional details.

sudo wpa_supplicant -Dwext -iwlan0 -c/etc/wpa_supplicant.conf -dd

HOWTO: Enable Wired Networking on Boot in Ubuntu Linux without NetworkManager

July 21st, 2011

A lot of Linux distros are going to applet-based management of their network connections in their desktop flavors. For example, Ubuntu Linux Desktop Edition has been using the Gnome applet NetworkManager since at least 9.10 Karmic Koala. While it works great most of the time, I’ve run into issues with it several times.

UPDATE:I believe this issue may have gone away with recent versions of NetworkManager.
The first was that (at least with 9.10) while NetworkManager was running from boot, it didn’t start receiving commands to connect until the user initiated their Gnome session by logging in. If you wanted to run an SSH server on the machine, you wouldn’t be able to connect to it until a local user logged in.

The second issue is that I often times end up using the Desktop Edition in a server-like capacity and turn gdm/X off entirely. The Desktop Edition has a shorter-lead time for package updates (which can be both a blessing and a curse). In my experience it’s also easier to find help/info on it versus the Server Edition. I recently setup a machine to act as a server for my dad, connecting to his weather station’s base station and uploading the results online. I ended up using the Desktop Edition of 11.04 because the server version didn’t have support out-of-the-box for some of his hardware.

Anyways, while I found it maddening to find a solution to initially, like many things Linux, once you know the magic incantation to recite, it’s cake.

Remove NetworkManager

This is optional and many of you may want or need to keep it around. For me, in the cases where I need to use this at all, I find it easier just to completely remove NetworkManager from the picture.

sudo apt-get remove network-manager

Enable Wired Interface

Put an entry in /etc/network/interfaces for eth0 (or eth1, or whatever your wired interface is).

Option 1: DHCP

auto eth0
iface eth0 inet dhcp

Option 2: Static IP

auto eth0
iface eth0 inet static
address 192.168.0.10
gateway 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255

Now your network interface should come up on boot, without NetworkManager!

HOWTO: Disable IPv6 in Ubuntu Linux

July 21st, 2011

Although we are edging closer to wide-spread IPv6 adoption with milestones such as World IPv6 Day, we aren’t quite there yet. Since I don’t use IPv6 on my LAN, I prefer to disable it. These instructions were written with Ubuntu 11.04, but it should work for 9.x,10.x, and probably many other distros as well.

Check if IPv6 is enabled

cat /proc/sys/net/ipv6/conf/all/disable_ipv6

0 means IPv6 is Enabled while 1 indicates that IPv6 is Disabled

Disable IPv6

Add the following to /etc/sysctl.conf

net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

Reboot!

Automotive Systems to Mitigate Heat-Related Deaths or HAL9000 Rescues Timmy

July 6th, 2011

Recently a child in our community died when they were left in the car for several hours when the outside temperature was in excess of 80F. This tragic accident is not the first time I’ve heard of children being left in cars during temperature extremes. Unfortunately in the fast paced world we live in with hectic schedules and constant distraction, it probably won’t be the last either. There are numerous aides that parents can employ to help remind them that their young children are in the car. Establishing a routine, placing your bag or phone in front of the child’s seat, and setting reminders on your phone are just a few of the ideas that regularly come up. While technology alone cannot solve the problems of human error and distraction, this is an area that I feel it could provide significant impact.

Today’s cars are outfitted with myriad sensors and automated controls. Power windows, power door locks, alarm systems, weight sensors for airbag activation, door ajar sensors, temperature sensors for automatic climate control, and even motion or radar sensors for alarm systems in some convertibles. The equipment is present to provide several different types of solutions to the problem of children or pets being left in cars during temperature extremes.

Type I – Early Warning/Reminder Systems

The best solution is to prevent the child, pet, elderly, or disabled individual from being left in the car in the first place. Several concepts that could be applied here:

  • Unique alarm, tones, or spoken word (such as from the navigation system) reminding the driver that a rear occupant has been detected when they remove the key from the ignition.
  • Unique alarm or tones when locking the doors and/or arming the security system on a car when a rear occupant is detected. Many cars will sound a continuous tone when the owner attempts to lock the doors with the key fob if a door is ajar or the lights are left on. In much the same way, a tone could be sounded if a rear occupant is still in the car.

Type II – Notification Systems

The next class of solution is to notify others of the potential danger after certain conditions are met. For instance, trigger conditions could include such things as timers, internal temperature sensor data, rear occupant weight sensors, and/or motion sensors. When a potentially dangerous situation is detected, the car could alert others in one of the following ways:

  • Sounding a unique alarm or tone. It is imperative that this tone be easily distinguishable from the normal alarm sounds that so many of us have become immune to due to false alarms. Ideally it would be a pattern that would be agreed upon by all car manufacturers such that it would become the ‘universal signal’ for an at risk occupant locked in a car.
  • In cars equipped with remote monitoring and communication systems, such as GM’s OnStar, notifications could be sent to the owner’s email, smart phone, or even local authorities including GPS coordinates and internal temperature readings.

Type III – Risk Mitigation Systems

The final class of solution would seek to reduce the threat to the at-risk occupant. Once trigger conditions were meet, the car could respond in one or more of the following ways:

  • Opening all the windows partially
  • Turning on the car’s HVAC fan

These are just some ideas I thought of in a single sitting after hearing of this tragic story on the news. Certainly the engineers in the automotive industry who can design automatic transmissions, hybrid and plug-in power trains, integrated navigation and infotainment systems, and 100HP/L+ engines can greatly improve on this. I realize that there are significant financial, legal, and technical hurdles that must be overcome to take an idea like this into production. However, I think effective solutions can be implementing using one or more of the classes of systems above in a cost effective manner. Car manufacturers could partner with one another to offset design, component, and integration costs. Many of the necessary components are already present in modern cars thus reducing incremental costs. Finally, much like many other new and innovative systems, they could be targeted to specific vehicle classes or models (minivans, SUVs) initially and offered on higher margin trim levels and/or as additional cost options. As the systems mature and the costs come down, they could then be trickled down to other vehicles.

Keeping Applications Up-to-Date on Windows Systems

July 4th, 2011

With so many applications installed on your machine and with many of us having multiple machines, keeping them up-to-date is a real PITA and chews up a lot of time. Microsoft has helped us out a lot with automatic updates to Windows, .NET, Office, Microsoft Security Essentials, SQL Server, and several other applications. Other applications like the JRE and Adobe Acrobat Reader have added automatic application updating as well, but there are still numerous applications on most machines (a quick look at one of my machines shows over 139 applications!) that require at least manual update installations, if not manual update checks as well.

Enter Secunia’s Personal Software Inspector (PSI). Free for personal use, this application detects and installs missing security patches for hundreds of different Windows applications. For many applications it can offer to install the updates automatically and when it can’t, it can link you directly to where you need to go to get the update. If there is an application that isn’t currently supported by PSI, it’s very easy to submit a request to Secunia to have it added by clicking the “Are you missing a program?” link on the “Scan Results” page.

PSI gives you a nice dashboard with some historical “Secunia Score” tracking.

Secunia Dashboard

Secunia PSI Dashboard

The meat of the application is the “Scan Results” page which shows you a list of applications you have installed that it can monitor, the current version you have installed, whether it’s up-to-date or not, and where the application is installed.

Secunia Scan Results

Secunia PSI Scan Results

Occasionally I have to go in and manually remove an old instance of an application (old JDK version, Google Chrome instances, etc.) to get the patch level to 100%, even though I’m only using the latest version. I’ve been running this on several of my machines for close to a year now, and overall I’ve found it to be a real time saver.

Windows Updates for Offline Machines or Slow Connections

July 1st, 2011

I needed to upgrade someones computer to Windows 7 and they had a very slow internet connection. To save time, I wanted to download all of the updates ahead of time so I wouldn’t have to wait an eternity for them when I was on-site. I initially considered setting up a WSUS server inside a VM but stumbled across another solution in the process: WSUS Offline Update.

I simply downloaded and extracted the zip file to an external hard drive, ran UpdateGenerator.exe, selected the products I wanted and then let it eat overnight to download all the packages. I then took the external hard drive with me, attached it to the machine after I installed the OS, ran the UpdaterInstaller.exe (located in the client directory) and in very short order had (almost) all the Microsoft Windows, .NET, VC++ Redistributable, and Office updates installed. On a Windows 7 Professional x86 machine, Windows Update still found about 18 packages totaling 38MB that needed to be downloaded after WSUS Offline Update had done its thing. Not perfect but sure beats downloading a gig+ over a slow connection.

I’ve also used the app to update new VMs when I create them, as it’s still faster getting them off the disk than the internet, even with a fast connection.

Ubuntu 11.04 Natty Narwhal Upgrade – Grub Prompt on First Reboot

June 29th, 2011

I just updated one of my VMs from Ubuntu 10.10 to 11.04 Natty Narwhal using the Update Manager. All seemed to go well during the upgrade process. When it rebooted for the first time however, I was left with a grub prompt rather than a booting system. Grrrrrr.

NOTE: The following assumes the default disk layout. If you installed to a different disk or partition, you’ll have to adjust the steps below accordingly.

The fix is to manually boot the system at the grub prompt by typing

set root=(hd0,1)
linux /boot/vmlinux-2.6.38-8-generic root=/dev/sda1 ro
initrd /boot/initrd.img-2.6.38-8-generic
boot

Then once you are successfully booted, re-install grub like this:

sudo grub-install /dev/sda
sudo update-grub

Thanks to Rob Convery for the tip!

C Assert and Wrapping Macros in Do/While Loops

June 24th, 2011

I was planning on writing a couple of entries on C macros, including assert macros and why you should wrap your macros in do/while loops. While doing a bit of research I came across a couple of excellent blog posts by Charles Nicholson on these very topics. Rather than try and out do him, I’ll just refer you to him!

Building a Better Assert Macro
http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/

Why You Should Be Wrapping Your Macros in Do/While Loops
http://cnicholson.net/2009/03/stupid-c-tricks-dowhile0-and-c4127/