2010-03-24

Windows UAC and bash's "Bad File Number" error

Today's problem: trying to run a freshly compiled setup_driver.exe from MSYS/MinGW produces the following:
sh: ./setup_driver.exe: Bad file number
The reason: Microsoft's implementation of UAC will try to give a free pass to any application that has "setup" in its name, and run it in elevated mode (after asking for confirmation).
This doesn't fare well with most shells running on Windows, as they don't know how to call the UAC elevation process, and you get the error above.

Rename your application to something that does not contain "setup" and your problem is gone.
Also applies to variations of "install" in your name.

Dynamically building headers with automake / autotools

Say part of your build process is meant to generate a custom include file, eg. resource.h, built from embedded resources and you want to ensure that this header is always built before the rest.

The trick then is to add
BUILT_SOURCES = resource.h
in your Makefile/Makefile.am as all files mentioned as BUILT_SOURCES will be built before any of the normal compilation rules run.

This is what we do in WDI (Windows Driver Installer) for libusb 1.0 with a Makefile.am that looks like:
BUILT_SOURCES = resource.h
lib_LTLIBRARIES = libusb-wdi.la

LIB_SRC = resource.h infs.h usbi.h installer.h installer_library.h installer_library.c

libusb_wdi_la_CFLAGS = $(VISIBILITY_CFLAGS) $(AM_CFLAGS) -L../../libusb/libusb/.libs -lusb-1.0 
libusb_wdi_la_SOURCES = $(LIB_SRC)

resource.h:
    ../embedder/embedder.exe resource.h

clean-local:
    -rm -rf resource.h

2010-03-18

Removing trailing whitespaces in Visual Studio

The following will remove trailing whitespaces whenever you save a file in Visual Studio 2008. Convenient!
  1. Go to Tools -> Macros -> Macro Explorer
  2. Double click "MyMacros -> Module1
  3. In the new Project Explorer Tree, double click on "EnvironmentEvents" above "Module1"
  4. After the #End Region for #Region "Automatically generated code, do not modify", add the following:
    Private saved As Boolean = False
        Private Sub DocumentEvents_DocumentSaved(ByVal document As EnvDTE.Document) _
                                                 Handles DocumentEvents.DocumentSaved
            If Not saved Then
                Try
                    ' Remove all the trailing whitespaces.
                    DTE.Find.FindReplace(vsFindAction.vsFindActionReplaceAll, _
                                         "{:Zs|\t}+$", _
                                         vsFindOptions.vsFindOptionsRegularExpression, _
                                         String.Empty, _
                                         vsFindTarget.vsFindTargetCurrentDocument, , , _
                                         vsFindResultsLocation.vsFindResultsNone)
    
                    saved = True
                    document.Save()
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Trim White Space exception")
                End Try
            Else
                saved = False
            End If
        End Sub

2010-03-08

A service installation section in this inf is invalid

If you've been banging your head against the wall on this error, and you are trying to install a 64 bit driver on Windows 7 x64 or Windows Vista x64, the problem might simply be due to the ServiceBinary property of your inf file referencing the 32 bit driver (.sys) rather than the 64 bit.

Make sure then that you have something like:
ServiceBinary = %12%\mydriver_x64.sys
and try again. Might work better this time around.