Is Windows 7 Really Checking For Drivers Online?

Windows 7 has an amazingly thorough collection of drivers available.  While some are built right into the OS, most need to be downloaded from Windows Update.  Depending on how you install Windows 7, this might not happen.  Particularly, if you don’t go along with all of Microsoft’s recommendations during setup (like turning on Windows Update and activating Windows right away), this can be a problem.

Often, it will appear as though Windows is checking online, but after a couple seconds, it will report that it found no drivers.  You might want to make sure this is true; it could easily be that it just didn’t find any drivers it’s allowed to download due to your current settings.

Here’s how to fix it.

First, activate Windows.  This never hurts.  Keys are free right now, so there’s no reason not to.  In theory, you shouldn’t need to do this, but I’ve encountered this issue a few times where activating Windows seemed to fix it.

Second, make sure Windows Update is turned on.  You don’t need to set it to automatically install everything.  I have it set to automatically download updates but let me choose what and when to install.

Third, make sure you’ve configured your system to allow driver downloads.  Go to System under Control Panel (the fastest way is WinKey + Pause/Break).  Then, on the left side of that window, you’ll see a link for ‘Advanced system settings’.  Click it and then select the Hardware Tab:

image

Click ‘Device Installation Settings’.  You’ll see a window like this:

image

If you have the same settings selected as shown above, you won’t get driver updates, even if you specifically choose to check online.  You can pick alternative settings if you want, but it really just makes sense to choose ‘Yes’.

Once you’ve done all this, make sure that Windows Update isn’t busy.  Finish installing any updates it’s working on.  Then, just to be safe, restart your computer (this shouldn’t be required, but certainly won’t hurt).  That should do it.

In theory, the second you save your changes in the Device Installation Settings window, Windows should automatically scan for drivers for any hardware that needs it.  Sometimes it even works.  If not, there are two ways of forcing Windows to check again.  The easiest, and probably the safest, is to just do another check for updates through Windows Update.  Any driver updates will be shown there, and you can install them just like any other update.

If you’d rather have a bit more control, go to Device Manager (click Start, right-click on Computer, click Manage, and then open Device Manager in the Computer Management window that appears):

image

Right-click the device in question and choose Update Driver Software:

image

Then, just click ‘Search Automatically’.  Windows should take care of the rest.  If you happen to have a driver – or you think you might, and want to see if it works – click ‘Browse’ and point it to the folder with your driver.  You can tell it to search subfolders, so don’t worry to much about picking the exact folder within a complicated driver package.  Generally speaking, Windows will install the driver if it finds one that works, and if it doesn’t, it won’t work anyway.  In theory, you can specify exactly which driver you want to use, but under Windows Vista and Windows 7, I’ve never come across a situation where this works.

If you ever have a choice, trust the Windows driver installation system over the vender’s setup program; they often do some pretty weird shit and sometimes install programs you really don’t want.

If your driver isn’t found online, and you don’t have a driver package provided by the manufacturer, you’ve probably come across some hardware that is going to take serious work to get running with Windows 7, if it’s even possible.  In this case, time to hit Google (or Bing?).  And good luck.

Choosing a Data Store

Regardless of what you’re doing, you’ll probably have to save some data sooner or later.  When you do, you’ll be faced with an incredible array of data technologies, all of which are clamoring for you to use them.  The data store you choose will define your entire project.  Choosing wrong dooms you to failure.  But until you’ve had a hell of a lot of experience – and made some horrible mistakes – it’s pretty tough to know what to pick.  Here’s some help.

The first thing you’ll need to decide is whether your data belongs in a file or on a server.  This is where a lot of people go wrong.

Files are great when the data is specific to one person and one place.  The data is saved in one file, an easy bundle to manage and understand.  Your data will be easy to backup, easy to send to other people, easy to copy and archive and transfer.  Everyone understands files.  If your program has a File Open command, you probably want to go with a file-based store.

But files have weaknesses.  They’re fragile – lose the file, and you’ve lost everything.  This is a bigger problem than you might expect: if your hard drive hiccups or if someone bumps your network cable or if your power goes out at the wrong time, you might lose everything.  They’re very difficult to share.  Security can be an issue – you’re pretty much at the mercy of the underlying OS.  They often don’t perform very well when there are many read or write operations on the underlying file.  And perhaps most importantly: it’s just a bunch of bytes.  It can’t do anything but sit there.

Servers, on the other hand, can do all kinds of amazing stuff.  In fact, it’s just about possible to write entire applications using nothing but a database server these days.  They’re great at coordinating data between many different people and places, usually have lots of flexibility when it comes to security, and often offer much more extensive feature sets than file-based storage.

But, they have their weaknesses, too.  Obviously, you need a server machine.  Maybe that’s just the machine your program runs on anyway, but even so, you’ll probably need to install some supporting software there.  They’re much more complicated to manage.  Since the data is exposed as a service, you can’t nicely bundle it up and e-mail it to someone or burn it to a CD.  And there’s often lots of overhead associated with the server.

Generally speaking, if you’re writing a program that just one person uses (at a time), and you’re working with easy-to-isolate chunks of data, use a file.  If you envision your program opening documents of some type, you’ll want to use files.  If you’re building a stand-alone program that will be installed on many computers, you probably want to use files.

If you need a bit more power, reliability, performance, security, or interoperability than simple files provide, then move up to a server.

Of course, most complex solutions support – or require – more than one type of data store.  Simplicity is good, but don’t feel that you have to keep everything in one place if it doesn’t really make sense.

I’ll be going into much more depth on this topic later, but for now, take this away:

Need a server?  Use Microsoft SQL Server.  This comes in so many flavours, something is bound to work for you.  From the full-blown Enterprise edition (or the virtually identical – to a programmer – Express edition available for free), to the file-based Compact Edition needing only a small DLL to run, it’s a good bet.

Need a file?  Perhaps SQL Server is overkill.  If so, use XML.  XML is very flexible, with a great tool set.  You can even go between a DataSet and XML with one line of code.  Another safe bet.  Be good: save it in an appropriate location under %AppData% or %UserProfile%.

Saving simple user settings?  Particularly the type that don’t really need to be saved or backed up, but merely offer convenience?  Use the registry.  But use it properly: save in the right place, and don’t save any serious amount of data.  If you feel you have too much data for the registry – or you just don’t like it – then save an XML file in %AppData%.  But still: don’t save anything here that the user might want to backup.

Of course, this advice is only valid most of the time, and as a developer you should know that a solution that works most of the time is worse than no solution at all.  Nevertheless, if you’re making a different decision, it wouldn’t hurt to take a moment and think about it again.

Windows® Internals, Fifth Edition

For those of you who aren’t familiar with him, one of Microsoft’s foremost leaders and a leading writer, Mark Russinovich, has written another book along with David Solomon and Alex Ionescu.

Mark has always been one of my personal favourites in the Windows world; he is a mentor to me and presumably to millions of other IT professionals out there. He is a technical fellow at Microsoft, author of the indispensable widely-used Sysinternals tools, and more notably the discoverer of the “Evil” Sony rootkit that permanently turned me away from Sony products. His expertise in Windows is unparalleled.

The book promises to give both developers and system administrators an in-depth understanding of the Windows kernel and will foster effective troubleshooting and smooth operation of any Windows platform. The book will focus on Windows Server 2008 and will hence be applicable to Vista, Windows 7 and any other release that is built on the NT 6.0 kernel.

Windows® Internals, Fifth Edition

Windows Internals: Covering Windows Server 2008 and Windows Vista

In addition to this, there are hands-on experiments throughout the book that will allow the reader to witness some of the theoretical paradigms presented first hand.

The book is set to release June 17, 2009 and I am going to be one of the first to snap up a copy. Suggested retail price is $69.99 and of course the knowledge gained – priceless.

Understanding Finally Blocks

Whether you use C# or VB, you’ll no doubt be familiar with the Try...Catch block:

Try
'Do stuff
Catch ex As Exception
'Handle exception
Finally
'Clean up
End Try

The basic idea is simple: any code you right that might go wrong should be inside a Try block.  You add a Catch block to handle the exception, and you add a Finally block to run any code you want to execute once everything is finished, regardless of how things went.  Typically, this is code to clean up after yourself.  Of course, you don’t need both the Catch or Finally blocks; you could use just one or the other.  Let’s look at at really simple example:

'Get a temporary file path
Dim sFile As String = IO.Path.GetTempFileName()

Try

'Create temp file
IO.File.WriteAllText(sFile, Now)

'TODO: Stuff
Throw New NotImplementedException()

'Delete temp file
IO.File.Delete(sFile)

Catch ex As Exception

'Write this error to a log file
IO.File.WriteAllText("errors.log", ex.ToString)

End Try

Hopefully, you all see the big problem here: since an exception is thrown, code will jump to the Try block and then carry on running the program.  Our temporary file will never be deleted.  That’s bad.  Instead, you should have used the Finally block, like this:

'Get a temporary file path
Dim sFile As String = IO.Path.GetTempFileName()

Try

'Create temp file
IO.File.WriteAllText(sFile, Now)

'TODO: Stuff
Throw New NotImplementedException()

Catch ex As Exception

'Write this error to a log file
IO.File.WriteAllText("errors.log", ex.ToString)

Finally

'Delete temp file
IO.File.Delete(sFile)

End Try

Let’s examine the Finally block a bit closer.  What exactly is the point of it?  If you read the manual, it basically says that code in the finally block executes after the code in both the Try and Catch runs, regardless of whether an error happened or not. Okay, so it’s pretty clear that you shouldn’t put your clean-up code in the Try block, because if an exception is run, execution jumps to the Catch block and then exits, so your clean-up code will never run.  But here’s the question many people don’t get: why not just put the clean up code after the whole block?  Won’t it basically do the same thing?  Well, most of the time, yes.  In fact, sometimes that’s exactly where you should put your next bit of code.  But sometimes, there’s a difference.

For example, what if we ran this program from a CD, or from another location where we have only read access?  Well, our exception handling code is going to break since it won’t have permission to write to “errors.log”.  If you put the File.Delete call after the whole Try block, it will never run.  Because it’s in the Finally block, though, it will run no matter what.

Of course, there’s another potential problem here, too: what if an exception is thrown when trying to write to the temp file (due to a disk error, perhaps)?  Well, in that case, your File.Delete call is going to break.  So what happens when an exception is thrown in a Finally block?  Actually, the same thing as any other exception: it will filter up to the next Try block, wherever that may be.  The way to deal with this would be to wrap your .Delete call in its own Try block – yes, you can embed Try blocks inside other Try blocks, even in the Finally portion.

There’s one other concept you might want to understand, and that is the Try...Finally block.  Why might you not want to have a Catch block?  Well, what if you have code higher up ready to handle any exceptions that might occur, but you still want to run some clean up code?  In this case, you’d put your clean-up code in the Finally block.  That code will run, but the code after the Try block won’t.  It’s kind of like having a chance to fix any problems your exception may have left without having to actually handle the exception at the time.

The Try block is a very powerful tool, but it’s important to understand exactly how it works and use it properly.  Since it only comes into play when things go wrong, and you never really know how or when things will go wrong, it’s something you MUST get right.

Fixing Visual Studio’s Edit and Continue under x64

One of the best features about developing under Visual Studio and .NET is Edit and Continue.  Many of you might not have heard this term before, but I’m sure you use it on a regular basis; it simply refers to the ability to edit your code while debugging.  This is actually a pretty amazing thing… Visual Studio rebuilds your application and then resumes execution right where you left off, but using the new code.  It’s not too different from having a mechanic rebuild your car’s engine while you’re driving along the freeway.

But, you’ll find it doesn’t work under 64-bit versions of Windows.  You’ll get this error message:

Changes to 64-bit applications are not allowed.

As it turns out, there’s a very easy fix for this: turn your application into a 32-bit one.  This is pretty unlikely to affect the way your application behaves, and it’s a very simple setting to change.  You’ll find it under Project Properties.  In VB, go to the Compile tab, click Advanced Compile Options, and look for the Target CPU setting.  Under C#, go to the Build tab and look for the Platform Target option.  Once you’ve found it, change it from “Any CPU” to “x86”.  That’s it!

Of course, you might want to consider changing this back when you’re ready to deploy your application.  While 32-bit applications will still run on 64-bit Windows, your customers will be much happier with a native 64-bit exe.  Leaving it as “Any CPU” lets the framework handle the details and make things work properly regardless of the platform.


Copyright © 2010 Paul Guenette and Matthew Sleno.