Friday, February 4, 2011

How to access a PCMCIA modem's serial number?

A Sprint cellular modem plugs into a laptop - often the PCMCIA slot. To connect, the user opens a program called the Connection Manager. This program has a menu option to display the serial number and phone number of the modem that's currently inserted.

Does that imply that the serial/phone number of the modem could be available to other programs running on the system? (Note: let's assume this is Windows XP.)

Specifically, could a company's VPN software be configured to pass along information about which modem is being used to connect?

Finally, is there existing VPN software that already does this, or would it have to be custom-programmed?

  • Sometimes you can get the modem's serial number using the AT command set. To see this in action, go to your control panel and open up Phone and Modem Options. Select the Modems tab, select the modem you're interested in, and choose Properties.

    In the modem window, select the Diagnostics tab, and press the Query Modem button.

    This opens the serial port and sends a series of AT commands to gather various settings and information. You can open the serial port in your program (or a terminal program), send the AT command, and get the same information back.

    You may need to check your specific modem's AT command set to find where the serial number is stored, or use a serial port spy program to see how Sprint's program does it.

    I'm not aware of any VPNs that use this information, and I can think of several ways to spoof it, since communications between the modem and the computer are not cryptographically secure.

    Nathan Long : Awesome! Spoofing here is not a big concern - the problem is that average users are swapping cards and not telling anybody. The company wants an automatic way to poll who has what card. I doubt anyone would know how to hack it.
    Nathan Long : (By the way, the "Query Modem" step you described did work. Going through "phone and modem options" made me give a default area code first, and since I will configure multiple cards on this machine I'm not sure if that's good. The query can also be found via Device Manager, righ
    From Adam Davis
  • Open hyperterminal or make a serial port connection programatically and use Hayes AT language to talk to it. Most software also has it listed in the device properties and/or diagnostics.

    AT+GSN

    press enter

  • I was able to get help with a serial PCMCIA card from www.usconverters.com, they have drivers and good tech support

    From Hans

Are C++ Reads and Writes of an int atomic

I have two threads, one updating an int and one reading it. This value is a statistic where the order of the read and write is irrelevant.

My question is, do I need to synchronize access to this multi-byte value anyway? Or, put another way, can part of the write be complete and get interrupted, and then the read happen.

For example, think of

value = ox0000FFFF increment value to 0x00010000

Is there a time where the value looks like 0x0001FFFF that I should be worried about? Certainly the larger the type, the more possible something like this is

I've always synchronized these types of accesses, but was curious what the community thought.

  • No, they aren't (or at least you can't assume they are). Having said that, there are some tricks to do this atomically, but they typically aren't portable (see Compare-and-swap).

  • Yes, you need to synchronize accesses. In C++0x it will be a data race, and undefined behaviour. With POSIX threads it's already undefined behaviour.

    In practice, you might get bad values if the data type is larger than the native word size. Also, another thread might never see the value written due to optimizations moving the read and/or write.

  • C++ does not currently specify any threading behaviour.

    On some compilers the int may not be aligned to an appropriately size boundary. Therefore two bus access may be necessary to read or write the value, and may not be atomic. Even if access is atomic, incrementing for instance will usually be two operations and there is no guarantee that threads will see updated values in a timely fashion.

    Adding a volatile modifier should make it atomic, but again this is not actually defined. For high performance, operations using atomic compare-and-swap and/or get-and-set should be considered as an alternatice to synchronisation.

    Jacek Ławrynowicz : "Adding a volatile modifier should make it atomic" - no way this can be true in c++. sorry.
  • You must synchronize, but on certain architectures there are efficient ways to do it.

    Best is to use subroutines (perhaps masked behind macros) so that you can conditionally replace implementations with platform-specific ones.

    The Linux kernel already has some of this code.

  • IF you're reading/writing 4-byte value AND it is DWORD-aligned in memory AND you're running on the I32 architecture, THEN reads and writes are atomic.

    From gabr
  • Boy, what a question. The answer to which is:

    Yes, no, hmmm, well, it depends

    It all comes down to the architecture of the system. On an IA32 a correctly aligned address will be an atomic operation. Unaligned writes might be atomic, it depends on the caching system in use. If the memory lies within a single L1 cache line then it is atomic, otherwise it's not. The width of the bus between the CPU and RAM can affect the atomic nature: a corectly aligned 16bit write on an 8086 was atomic whereas the same write on an 8088 wasn't because the 8088 only had an 8 bit bus whereas the 8086 had a 16 bit bus.

    Also, if you're using C/C++ don't forget to mark the shared value as volatile, otherwise the optimiser will think the variable is never updated in one of your threads.

    Skizz

    From Skizz
  • I agree with many and especially Jason. On windows, one would likely use InterlockedAdd and its friends.

    From kenny
  • At first one might think that reads and writes of the native machine size are atomic but there are a number of issues to deal with including cache coherency between processors/cores. Use atomic operations like Interlocked* on Windows and the equivalent on Linux. C++0x will have an "atomic" template to wrap these in a nice and cross-platform interface. For now if you are using a platform abstraction layer it may provide these functions. ACE does, see the class template ACE_Atomic_Op.

    From Adam Mitz
  • To echo what everyone said upstairs, the language pre-C++0x cannot guarantee anything about shared memory access from multiple threads. Any guarantees would be up to the compiler.

    MSN

  • Asside from the cache issue mentioned above...

    If you port the code to a processor with a smaller register size it will not be atomic anymore.

    IMO, threading issues are too thorny to risk it.

    From JeffV
  • On Windows, Interlocked*Exchange*Add is guaranteed to be atomic.

  • The only portable way is to use the sig_atomic_t type defined in signal.h header for your compiler. In most C and C++ implementations, that is an int. Then declare your variable as "volatile sig_atomic_t."

    Sam Miller : volatile doesn't do what you think it does http://stackoverflow.com/questions/2484980/why-is-volatile-not-considered-useful-in-multithreaded-c-or-c-programming
  • Lets take this example

    int x;
    x++;
    x=x+5;
    

    The first statement is assumed to be atomic because it translates to a single INC assembly directive that takes a single CPU cycle. However, the second assignment requires several operations so it's clearly not an atomic operation.

    Another e.g,

    x=5;
    

    Again, you have to disassemble the code to see what exactly happens here.

    tc. : But the compiler could optimize it into `x+=6`.
  • tc, I think the moment you use a constant ( like 6) , the instruction wouldn't be completed in one machine cycle. Try to see the instruction set of x+=6 as compared to x++

Automatically checking for a new version of my application

Trying to honor a feature request from our customers, I'd like that my application, when Internet is available, check on our website if a new version is available.

The problem is that I have no idea about what have to be done on the server side.

I can imagine that my application (developped in C++ using Qt) has to send a request (HTTP ?) to the server, but what is going to respond to this request ? In order to go through firewalls, I guess I'll have to use port 80 ? Is this correct ?

Or, for such a feature, do I have to ask our network admin to open a specific port number through which I'll communicate ?

  • The simplest way to make this happen is to fire an HTTP request using a library like libcurl and make it download an ini or xml file which contains the online version and where a new version would be available online.

    After parsing the xml file you can determine if a new version is needed and download the new version with libcurl and install it.

    David Dibben : Qt has networking modules so the HTTP request can be handled easily in Qt without needing to use an extra library.
  • Just put an (XML) file on your server with the version number of the latest version, and a URL to the download the new version from. Your application can then request the XML file, look if the version differs from its own, and take action accordingly.

    From wvdschel
  • I think that simple XML file on the server would be sufficient for version checking only purposes.

    You would need then only an ftp account on your server and build system that is able to send a file via ftp after it has built a new version. That build system could even put installation files/zip on your website directly!

    From Marcin Gil
  • On the server you could just have a simple file "latestversion.txt" which contains the version number (and maybe download URL) of the latest version. The client then just needs to read this file using a simple HTTP request (yes, to port 80) to retrieve http://your.web.site/latestversion.txt, which you can then parse to get the version number. This way you don't need any fancy server code --- you just need to add a simple file to your existing website.

  • If you want to keep it really basic, simply upload a version.txt to a webserver, that contains an integer version number. Download that check against the latest version.txt you downloaded and then just download the msi or setup package and run it.

    More advanced versions would be to use rss, xml or similar. It would be best to use a third-party library to parse the rss and you could include information that is displayed to your user about changes if you wish to do so.

    Basically you just need simple download functionality.

    Both these solutions will only require you to access port 80 outgoing from the client side. This should normally not require any changes to firewalls or networking (on the client side) and you simply need to have a internet facing web server (web hosting, colocation or your own server - all would work here).

    There are a couple of commercial auto-update solutions available. I'll leave the recommendations for those to others answerers, because I only have experience on the .net side with Click-Once and Updater Application Block (the latter is not continued any more).

  • I would absolutely recommend to just do a plain HTTP request to your website. Everything else is bound to fail.

    I'd make a HTTP GET request to a certain page on your site containing the version of the local application.

    like

    http://www.example.com/update?version=1.2.4
    

    Then you can return what ever you want, probably also the download-URL of the installer of the new version.

    Why not just put a static file with the latest version to the server and let the client decide? Because you may want (or need) to have control over the process. Maybe 1.2 won't be compatible with the server in the future, so you want the server to force the update to 1.3, but the update from 1.2.4 to 1.2.6 could be uncritical, so you might want to present the client with an optional update.

    Or you want to have a breakdown over the installed base.

    Or whatever. Usually, I've learned it's best to keep as much intelligence on the server, because the server is what you have ultimate control over.

    Speaking here with a bit of experience in the field, here's a small preview of what can (and will - trust me) go wrong:

    • Your Application will be prevented from making HTTP-Requests by the various Personal Firewall applications out there.
    • A considerable percentage of users won't have the needed permissions to actually get the update process going.
    • Even if your users have allowed the old version past their personal firewall, said tool will bi*ch around because the .EXE has changed and will recommend the user not to allow the new exe to connect (users usually comply with the wishes of their security tool here).
    • In managed environments, you'll be shot and hanged (not necessarily in that order) for loading executable content from the web and then actually executing it.

    So to keep the damage as low as possible,

    • fail silently when you can't connect to the update server
    • before updating, make sure that you have write-permission to the install directory and warn the user if you do not, or just don't update at all.
    • Provide a way for administrators to turn the auto-update off.

    It's no fun to do what you are about to do - especially when you deal with non technically inclined users as I had to numerous times.

    osti : Very complete answer; but I'd like to add: if possible, use HTTPS/SSL. Although it might not be necessary here: There are some tools around sniffing for such ``hey, is there an update?''-questions and simply answering ``yes, here it is, please take my code and install/execute it''...
    pilif : even better than using SSL is to actually sign the update binary and check its signature. SSL only proves that the url the update was loaded from is what the (any) certificate was issued for. To be on the totally save side, I'll deploy the to-be-updated app with my public key, sign the updates with my private key and then check the update itself.
    From pilif
  • Pilif answer was good, and I have lots of experience with this too, but I'd like to add something more:

    Remember that if you start yourapp.exe, then the "updater" will try to overwrite yourapp.exe with the newest version. Depending upon your operating system and programming environment (you've mentioned C++/QT, I have no experience with those), you will not be able to overwrite yourapp.exe because it will be in use.

    What I have done is create a launcher. I have a MyAppLauncher.exe that uses a config file (xml, very simple) to launch the "real exe". Should a new version exist, the Launcher can update the "real exe" because it's not in use, and then relaunch the new version.

    Just keep that in mind and you'll be safe.

  • Martin,

    you are absolutely right of course. But I would deliver the launcher with the installer. Or just download the installer, launch it and quit myself as soon as possible. The reason is bugs in the launcher. You would never, ever, want to be dependent on a component you cannot update (or forget to include in the initial drop).

    So the payload I distribute with the updating process of my application is just the standard installer, but devoid of any significant UI. Once the client has checked that the installer has a chance of running successfully and once it has downloaded the updater, it runs that and quits itself.

    The updater than runs, installs its payload into the original installation directory and restarts the (hopefully updated) application.

    Still: The process is hairy and you better think twice before implementing an Auto Update functionality on the Windows Platform when your application has a wide focus of usage.

    Martín Marconcini : Agreed! we don't update through Internet. Our "launcher" only fires a process, the update code is in the app. In either case, the user never does this automatically, we do it through VNC because it is a Medical DB and we don't want to risk the DB in case there are DB scripts to run. Good
    From pilif
  • @pilif : thanks for your detailed answer. There is still something which is unclear for me :

    like

    http://www.example.com/update?version=1.2.4

    Then you can return what ever you want, probably also the download-URL of the installer of the new version.

    How do I return something ? Will it be a php or asp page (I know nothing about PHP nor ASP, I have to confess) ? How can I decode the ?version=1.2.4 part in order to return something accordingly ?

    From Jérôme
  • I would agree with @Martin and @Pilif's answer, but add;

    Consider allowing your end-users to decide if they want to actually install the update there and then, or delay the installation of the update until they've finished using the program.

    I don't know the purpose/function of your app but many applications are launched when the user needs to do something specific there and then - nothing more annoying than launching an app and then being told it's found a new version, and you having to wait for it to download, shut down the app and relaunch itself. If your program has other resources that might be updated (reference files, databases etc) the problem gets worse.

    We had an EPOS system running in about 400 shops, and initially we thought it would be great to have the program spot updates and download them (using a file containing a version number very similar to the suggestions you have above)... great idea. Until all of the shops started up their systems at around the same time (8:45-8:50am), and our server was hit serving a 20+Mb download to 400 remote servers, which would then update the local software and cause a restart. Chaos - with nobody able to trade for about 10 minutes.

    Needless to say that this caused us to subsequently turn off the 'check for updates' feature and redesign it to allow the shops to 'delay' the update until later in the day. :-)

    EDIT: And if anyone from ADOBE is reading - for god's sake why does the damn acrobat reader insist on trying to download updates and crap when I just want to fire-it-up to read a document? Isn't it slow enough at starting, and bloated enough, as it is, without wasting a further 20-30 seconds of my life looking for updates every time I want to read a PDF?
    DONT THEY USE THEIR OWN SOFTWARE??!!! :-)

    Martín Marconcini : LOL, well we don't autoupdate. The user never autoupdates. We do :) Our user's DBs contain very important medical data, what we do is we update the server (using an in-house wizard) that backups the DB and executes scripts and then if everything is "ok", it "publishes" the updat
    From robsoft
  • if you keep your files in the update directory on example.com, this PHP script should download them for you given the request previously mentioned. (your update would be yourprogram.1.2.4.exe

    $version = $_GET['version'];    
    $filename = "yourprogram" . $version . ".exe";
    $filesize = filesize($filename);
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: post-check=0, pre-check=0");
    header("Content-type: application-download");
    header('Content-Length: ' . $filesize);
    header('Content-Disposition: attachment; filename="' . basename($filename).'"');
    header("Content-Transfer-Encoding: binary");
    

    This makes your web browser think it's downloading an application.

  • in php, the thing is easy:

    <?php
        if (version_compare($_GET['version'], "1.4.0") < 0){
            echo "http://www.example.com/update.exe";
        }else{
            echo "no update";
        }
    ?>
    

    if course you could extend this so the currently available version isn't hard-coded inside the script, but this is just about illustrating the point.

    In your application you would have this pseudo code:

    result = makeHTTPRequest("http://www.example.com/update?version=" + getExeVersion());
    if result != "no update" then
        updater = downloadUpdater(result);
        ShellExecute(updater);
        ExitApplication;
    end;
    

    Feel free to extend the "protocol" by specifying something the PHP script could return to tell the client whether it's an important, mandatory update or not.

    Or you can add some text to display to the user - maybe containing some information about what's changed.

    Your possibilities are quite limitless.

    From pilif
  • My Qt app just uses QHttp to read tiny XML file off my website that contains the latest version number. If this is greater than the current version number it gives the option to go to the download page. Very simple. Works fine.

    From Andy Brice

Using an external "windows"-keyboard under Mac OS X

I use a MacBook, but I've got a usual keyboard attached to it.

The problem is that the keys don't exactly map 1-to-1. One thing is the APPLE and ALT keys. They map to WIN and ALT, but they are usually physically inverted, so if you want to use them with the same layout you have to invert them in the OS. The Function keys work differently too. Fx on the external = Fn + Fx on the MacBook keyboard. And then there are all the insert, delete, keys.

So, the question is, how do you come around this? Now I remap all the things I want at the System Preferences panel, but when I unplug the external keyboard it's all messed up. Is there a way to remap keys only for the external one? Some model of keyboard can store it's own mappings without needing the OS? Am I the only one who is bothered by this?

(I would like to avoid buying an external mac keyboard, because I wanted to try one of the ergonomic models, and as far as I know, there are no mac ergonomic models)

Update: Thanks for the responses, I fixed this.

To set the control keys for different keyboards, you have to go to System Preferences/Modifier Keys, then the drop down menu Select Keyboard allows you to choose one particular keyboard and set these keys. Works after unpluging/pluging it seems

The suggestion from @Matthew Schinckel seems to work for the rest of the issues (function keys, ...). I didn't try it yet, as the commands keys were my biggest gripe.

  • You could investigate DoubleCommand, it may do what you need.

    There's an experimental version that allows for different properties for different keyboards.

  • You create create your own custom keyboard mapping, which could then be used with the keyboard language menu. So when you plug in your keyboard, just switch to your custom layout. OS X has supported this since 10.2, and Apple has documentation on how to produce your own custom maps.

    http://developer.apple.com/technotes/tn2002/tn2056.html

    It's not something I've tried myself, just read about it once or twice. Looks like it could potentially do the job. I'd just duplicate a mapping that is as close as you can get to what you want, and then customise it from there.

    From Chops
  • In OS X 10.5 they allow you to have different keyboard setups for different keyboards. This works most of the time. I've had issues with very old keyboards that are plugged in via a PS2 to USB but otherwise it works fine.

    From emeryc
  • The best method I have is to download the Logitech Control Center for OSX from Logitech. Search throw the Installer package for the LCCKCHR.rsrc. Drop this file into either ~/Library/Keyboard Layouts or /Library/Keyboard Layouts. Logout and log back in and you'll notice a few more options in the International System Preferences under Inputs. Check the keyboard layouts you would like.

    Although this keyboard layout is for Logitech keyboards it works for most keybaords (especially international users)

    From Joe Chin

How to return a page of results from SQL?

Many applications have grids that display data from a database table one page at a time. Many of them also let the user pick the number of records per page, sort by any column, and navigate back and forth through the results.

What's a good algorithm to implement this pattern without bringing the entire table to the client and then filtering the data on the client. How do you bring just the records you want to display to the user?

Does LINQ simplify the solution?

  • On MS SQL Server 2005 and above, ROW_NUMBER() seems to work:

    http://blogs.x2line.com/al/archive/2005/11/18/1323.aspx

    DECLARE @PageNum AS INT;
    DECLARE @PageSize AS INT;
    SET @PageNum = 2;
    SET @PageSize = 10;
    
    WITH OrdersRN AS
    (
        SELECT ROW_NUMBER() OVER(ORDER BY OrderDate, OrderID) AS RowNum
              ,OrderID
              ,OrderDate
              ,CustomerID
              ,EmployeeID
          FROM dbo.Orders
    )
    
    SELECT * 
      FROM OrdersRN
     WHERE RowNum BETWEEN (@PageNum - 1) * @PageSize + 1 
                      AND @PageNum * @PageSize
     ORDER BY OrderDate
             ,OrderID;
    
  • Actually, LINQ has Skip and Take methods which can be combined to choose which records are fetched.

    Check those out.

    For DB: http://www.singingeels.com/Articles/Pagination_In_SQL_Server_2005.aspx

    From Vaibhav
  • Oracle Solution:

    select * from (
        select a.*, rownum rnum from (
            YOUR_QUERY_GOES_HERE -- including the order by
        ) a
        where rownum <= MAX_ROW
     ) where rnum >= MIN_ROW
    
  • I'd recommend either using LINQ, or try to copy what it does. I've got an app where I use the LINQ Take and Skip methods to retrieve paged data. The code looks something like this:

    MyDataContext db = new MyDataContext();
    var results = db.Products
        .Skip((pageNumber - 1) * pageSize)
        .Take(pageSize);
    

    Running SQL Server Profiler reveals that LINQ is converting this query into SQL similar to:

    SELECT [ProductId], [Name], [Cost], and so on...
    FROM (
        SELECT [ProductId], [Name], [Cost], [ROW_NUMBER]
        FROM (
           SELECT ROW_NUMBER() OVER (ORDER BY [Name]) AS [ROW_NUMBER], 
               [ProductId], [Name], [Cost]
           FROM [Products]
        )
        WHERE [ROW_NUMBER] BETWEEN 10 AND 20
    )
    ORDER BY [ROW_NUMBER]
    

    In plain English:
    1. Filter your rows and use the ROW_NUMBER function to add row numbers in the order you want.
    2. Filter (1) to return only the row numbers you want on your page.
    3. Sort (2) by the row number, which is the same as the order you wanted (in this case, by Name).

  • There is another identical post: http://stackoverflow.com/questions/2840/paging-sql-server-2005-results#9870

    Is there a way to merge posts in stackoverflow?

  • There are a few solutions which I use with MS SQL 2005.

    One of them is ROWNUMBER(). But, personally, I don't like ROWNUMBER() because it doesn't work for big results (DB which I work on is really big -- over 1TB data running thousands of queries in second -- you know -- big social networking site).

    Here are my favourite solution.

    I will use kind of pseudo code of T-SQL.

    Let's find 2nd page of users sorted by forename, surname, where each page has 10 records.

    @page = 2 -- input parameter
    @size = 10 -- can be optional input parameter
    
    if @page < 1 then begin
        @page = 1 -- check page number
    end
    @start = (@page-1) * @size + 1 -- @page starts at record no @start
    
    -- find the beginning of page @page
    SELECT TOP (@start)
        @forename = forename,
        @surname = surname
        @id = id
    FROM
        users
    ORDER BY
        forename,
        surname,
        id -- to keep correct order in case of have two John Smith.
    
    -- select @size records starting from @start
    SELECT TOP (@size)
        id,
        forename,
        surname
    FROM
        users
    WHERE
        (forename = @forename and surname = @surname and id >= @id) -- the same name and surname, but bigger id
        OR (forename = @forename and surname > @surname) -- the same name, but bigger surname, id doesn't matter
        OR (forename > @forename) -- bigger forename, the rest doesn't matter
    ORDER BY
        forename,
        surname,
        id
    
  • LINQ combined with lambda expressions and anonymous classes in .Net 3.5 hugely simplifies this sort of thing.

    Querying the database:

    var customers = from c in db.customers
                    join p in db.purchases on c.CustomerID equals p.CustomerID
                    where p.purchases > 5
                    select c;
    

    Number of records per page:

    customers = customers.Skip(pageNum * pageSize).Take(pageSize);
    

    Sorting by any column:

    customers = customers.OrderBy(c => c.LastName);
    

    Getting only selected fields from server:

    var customers = from c in db.customers
                    join p in db.purchases on c.CustomerID equals p.CustomerID
                    where p.purchases > 5
                    select new
                    {
                        CustomerID = c.CustomerID,
                        FirstName = c.FirstName,
                        LastName = c.LastName
                    };
    

    This creates a statically-typed anonymous class in which you can access its properties:

    var firstCustomer = customer.First();
    int id = firstCustomer.CustomerID;
    

    Results from queries are lazy-loaded by default, so you aren't talking to the database until you actually need the data. LINQ in .Net also greatly simplifies updates by keeping a datacontext of any changes you have made, and only updating the fields which you change.

    ScSub : The first statement seems to pull back everything from the DB and then the second statement gets a subset. What if you just want a subset in the first place? I have 90,000 rows and just want page 4 of 10 rows.
    Adam Lassek : @ScSub LINQ expressions are lazy-loaded, so the first call doesn't actually do anything at first. You could call `customers = customers.Skip(30).Take(10)` and it would only pull back what you want.
  • There is a discussion about this Here

    The technique gets page number 100,000 from a 150,000 line database in 78ms

    Using optimizer knowledge and SET ROWCOUNT, the first EmployeeID in the page that is requested is stored in a local variable for a starting point. Next, SET ROWCOUNT to the maximum number of records that is requested in @maximumRows. This allows paging the result set in a much more efficient manner. Using this method also takes advantage of pre-existing indexes on the table as it goes directly to the base table and not to a locally created table.

    I am afraid I am not able to judge if it is better than the current accepted answer.

    From Aidan

What is the best way to send large batches of emails in ASP.NET?

I'm currently looping through a datareader and calling the System.Net.Mail.SmtpClient's Send() method. The problem with this is that it's slow. Each email takes about 5-10 seconds to send (it's possible this is just an issue with my host). I had to override the executionTimeout default in my web.config file (it defaults to 90 seconds) like this:

 <httpRuntime executionTimeout="3000" />

One caveat: I'm on a shared host, so I don't think it is possible for me to send using the PickupDirectoryFromIis option (at least, it gave me errors when I turned it on).

Display blanks instead of 0 or 0.0 in a BIRT report

When using an aggregate control in some reports you would prefer to see a blank field instead of 0. There does not appear to be a way to do this automatically. Does anyone have a way that this can be done. Note, you want to maintain the '0' value for the field in cases when you export, but you want to show a blank when rendering to PDF or HTML.

  • There are a number of ways to solve this. The two primary are to use either visibility rules or highlights to create conditional formatting. The visibility is particularly attractive since it is easy to only apply the format rules to particular types of output (e.g. HTML).

    For this particular case, there are two problems with these approaches. First, I want a general solutions where I don't have to specify the text color. In other words, when the condition is true (value of 0) then I want my text color to match the background color. In that way if someone changes the backgroundColor for the control, the code still works.

    The other issue is that in this case I am using dynamic column binding which does not support value lookup.

    The solution that I created was to add a JavaScript function called hideMe as shown below.

    function hideText (dataControl){
        if (dataControl.getValue() == 0) {
         var color = dataControl.getStyle().getBackgroundColor();
         var parentItem = dataControl.getParent();
         do {
          if (color == null && parentItem != null) {
           color = parentItem.getStyle().getBackgroundColor();
           parentItem = parentItem.getParent();
          } else {
           break;
          }
    
         } while (color == null);
         dataControl.getStyle().color = color;
        }
    }
    

    Once this function has been added to the report (in my case an included javascript file) I just call it from the OnCreate method of the control.

    hideText(this);
    

    This can also be done using Java Event Handlers but this method seems to be easier.

  • Just an FYI, after working with this for a while longer, I have found that it is just easier to use Visibility rules. The one big advantage is that you can easily configure different visibility for different output formats. So for PDF it may be best to use blanks, but for Excel you may want the 0 values.

    Scott Rosenbaum : Yes: I changed my mind.

Right Align text in SQL Server

We all know T-SQL's string manipulation capabilities sometimes leaves much to be desired...

I have a numeric field that needs to be output in T-SQL as a right-aligned text column. Example:

Value
----------
   143.55
  3532.13
     1.75

How would you go about that? A good solution ought to be clear and compact, but remember there is such a thing as "too clever".

I agree this is the wrong place to do this, but sometimes we're stuck by forces outside our control.

Thank you.

  • The STR function has an optional length argument as well as a number-of-decimals one.

    SELECT STR(123.45, 6, 1)
    
    ------
     123.5
    
    (1 row(s) affected)
    
    Euro Micelli : Bravo! How did I miss this? Do note that STR() takes a float, not a decimal. SQLServer Help is very explicit about it ("... an expression of approximate numeric (float) data type"): if your decimals have enough digits, rounding will cause you trouble. This is still a great answer for most
    From d91-jal
  • If you MUST do this in SQL you can use the folowing code (This code assumes that you have no numerics that are bigger than 40 chars):

    SELECT REPLICATE(' ', 40 - LEN(CAST(numColumn as varchar(40)))) + 
    CAST(numColumn AS varchar(40)) FROM YourTable
    
    From Espo
  • The easiest way is to pad left:

    CREATE FUNCTION PadLeft(@PadString nvarchar(100), @PadLength int)
    RETURNS nvarchar(200)
    AS
    begin
    return  replicate(' ',@padlength-len(@PadString)) + @PadString
    end
    go
    print dbo.PadLeft('123.456', 20)
    print dbo.PadLeft('1.23', 20)
    
    From Josef

Race Condition Analysers for .NET

I've seen there are some race condition analysis tools for C++, C and Java. Anyone know of any static analysis tools that do the same for .NET?

  • I haven't ever used this tool, but it looks like TypeMock has a tool called Racer that can handle this. Roy Osherove blogged about it here. Another post with a better preview is here.

Database changes versioning

What are the recommended strategies to do versioning of database changes to tables, views, functions, stored procedures, etc.?

  • Some insight might be gained here in this similar question.

    From Craig
  • Export your schema as a sql script and maintain that script in a version control system.

    From Ed Guiness
  • I am not sure what is the best approach, but I always keep an updated SQL script into my SVN repository, so, any version of the app matches with the right SQL structure.

  • Keep creation scripts for all of your database objects under source control. Whenever you change a table, trigger, etc., check out that file, make the necessary change, execute the script, and check it back in.

  • I like Rails' method.

    It preserves in an executable script (with a version number in the file name) the sql (*) sentences needed to both apply and revert changes to the database. It also adds a table in the database to know which one's the current version.

    Result: You can actually move between versions automatically by having some extra scripts that go and call the update or downgrade parts of each script.

    (*) Note: Rails doesn't actually store sql, but ActiveRecord code, but that's not relevant.

    Example: That's version 10 of the db schema. Calling the up() method will move the db to version 10, calling down() will leave the db in version 9. In this case, by creating a table and dropping it respectively. You can use this same method in any other database/environment. At most you'd have to hack up a few supporting scripts which'll do this for you as rails does.

    $ more 010_programs_users.rb
    class ProgramsUsers < ActiveRecord::Migration
      def self.up
        create_table :programs_users , :id => false do |t|
          t.integer :user_id
          t.integer :program_id
        end
      end
    
      def self.down
        drop_table :programs_users
      end
    end
    

    This does not eliminate the need of having these files under source control, of course.

  • As Jeff Atwood posted about database control, on his blog.

    Scott Allen just wrote a brilliant five part series on the philosophy and practice of database version control:

    1. Three rules for database work
    2. The Baseline
    3. Change Scripts
    4. Views, Stored Procedures and the Like
    5. Branching and Merging
    Joe Philllips : If you'd also like an explanation of the versioning, visit http://commons.apache.org/releases/versioning.html
    From stukelly

Opening a file stored in a database in .NET

I'm storing a Word document in a SQL Server 2005 database in a varbinary(max) column. Is there a way to open this document from a VB.NET Windows Forms application without serialising to a file first (i.e. directly from the byte array I've read from the database)?

  • Not really. You need to treat it like an e-mail attachment, where the file is generally copied to a temp folder that is cleaned out periodically.

  • Depends on what's reading it. If it's Word, you'll probably have to serialize to a file, but if it's a function or library that can take an IO.Stream then you could wrap a new MemoryStream around the byte array and pass that.

Is a Flex debugger included in the sdk?

I have been writing Flex applications for a few months now and luckily have not needed a full debugger as of yet, so far I have just used a few Alert boxes...

Is there an available debugger that is included in the free Flex SDK? I am not using FlexBuilder (I have been using Emacs and compiling with ant).

If not, how do you debug Flex applications without FlexBuilder? (note: I have no intentions of using flexbuilder)

  • I had the same problem when programming with ActionScript and having to test it on a browser. Try this. It involves using Firefox (which I believe you do) and FireBug to receive the debug messages.

    Theo : A debugger is much more than something that shows debug messages. It lets you break the running application and step through the code line by line watching the values of variables change as each statement executes.
    From changelog
  • A debugger called fdb is included in the Flex SDK. Here's some documentation on how to use it:

    mmattax : Just what I was looking for!
    From hasseg
  • yes FDB is there , you can also see this Flex Runtime Tracer class , for runtime tracing without flash player debug version.

    http://askmeflash.com/applications/9/runtime-flex-tracer-and-debugger

    Ain : Re the link mentioned: the ~ key doesn't trigger anything on Mac OS (10.6.4). A way around would be to use a different and a more sensible key for it...
    From Robert

How do I script a password change for a SQL server login?

Just what the title says, I need to change the password for an existing sql server login and I want to do it via sql script.

ant build.xml windows white space in path

I'm using Windows and I'm trying to get ANT to work.

When I do an ant build from the command line, I get: C:\dev\Projects\springapp\${%ANT_HOME%}\lib not found.

I look into the build.xml file and I find: appserver.home=${user.home}/apache-tomcat-6.0.14 (which I just copied and pasted straight from a tutorial)

I changed it to: appserver.home="C:\Program Files\Apache Software Foundation\Tomcat 6.0" but now I get: C:\dev\Projects\springapp\"C:Program FilesApache Software FoundationTomcat 6.0"\lib not found.

It seems like the white space in Program Files and Tomcat 6.0 are causing the build to fail. How do you deal with these in xml files without re-creating the directory with a path with no white space?

  • Change it to

    appserver.home="C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0"
    
    John Meagher : Or use '/' instead of '\\'. It works the same in Ant on Windows and is a little easier to read.
  • In addition to escaping the windows directory separator also make sure that all paths that you type in should be with correct capitalisation, Windows is not case sensitive but case presrving, while Ant is case sensitive.

    From Daemin
  • It looks like you have your properties setup incorrectly.

    I'm guessing your basedir property is pointing at C:\dev\Projects\springapp and your properties are using value like:

    <property name="property.1" value="directory" />
    

    instead of

    <property name="property.1" location="directory" />
    

    Using the location property then resolves links as relative to your basedir if the location is a relative path and to the absolute path if you enter one of those. If you could post parts of your Ant file specifically how you define appserver.home and how you use it in the task that's throwing the error I could be more specific.

  • Variant with "/" instead "\" works on my system. Just need to delete " symbols before and after path structure.

    From Eugene

Travelling salesman problem..

Heres another famous problem we used to solve in college :)

Travelling salesman problem - Problem Statement:

 Given a number of cities and the costs of travelling from any city to any
 other city, what is the least-cost round-trip route that visits each city
 exactly once and then returns to the starting city?

How would you solve this in your favorite programming language?

  • IIRC, it's not the problem isn't solvable. It's that the complete solution has a largish big O value. But if you further define the problem such that you don't have to arrive at the optimum solution but can stop once you find a solution that's close enough, it's no big deal any more.

  • You used to solve the TSP in college?

    EDIT: Since you mention you went to school in India I'll add that we only study the TSP here in the US. We've made some progress, as you point out, but we haven't quite solved it yet. :)

  • If you solved TSP in college, you'd be living the life in some sunny island right now. No, scratch that - you'd own some sunny island. TSP is NP-complete, which means we can only approach the optimal result in acceptable time for bigger problems.

    From wvdschel
  • Check out the pthread (POSIX thread) library.

    From Jay Conrod
  • Where is TSP is an interview question? :)

    From Jimmy
  • The challenge isn't solving it, but it's solving it in less than exponential time. If that was what you used to do that college then publish your results and prepare for a nobel prize :-)

    The easy solution is to find all the possible routes and then pick the shortest one. I've seen all kinds of other solutions, from neural networks to genetic algorithms. But those just get a good solution, not the best.

    From Mendelt
  • Use DNA. These guys used the fact that DNA can be replicated exponentially to solve an NP-complete problem in polynomial time. For a sufficiently large number of cities, this technique would be faster than any approach on a deterministic computer.

    From Jay Conrod
  • I agree with Bill here. In the wikipedia article you quoted is the answer to your Question Or so sum it up: If you have to solve the problem for any size of map, any layout of map - Dont bother. You will be dead before the calculation is done.

    For specific sizes of maps and/or specific layouts it may be solvable.

    But I'd like to see the collage where you "solve" TSP.

    From Tnilsson
  • I agree that this is a bit of a silly question, there is no good programmatic implementation of a solution that would be short enough to throw up here.

    That being said, I HAVE seen some pretty neat good-enough solutions using neural networks and genetic algorithms, here's an example.

    Notwithstanding, if you get asked to solve TSP in an interview and you are not Alan Turing or Van Neuremburg resurrected you should probably run run run.

  • Well, i agree to the point that we didnt really SOLVE

    But comeon guys, have you not done this exercise in college?

    FYI: I studied in india, and this will be one of the exercises in CS major in any college.

    From Prakash
  • There is no good programmatic solutions?

    http://www.adaptivebox.net/CILib/code/tspcodes_link.html

    From Prakash
  • What sort of answer are you looking for in an interview when you ask this question? From the perspective of an interviewee my first response would be to ask what sort of solution are you looking for, what size the input set would be, and what type of running time are you looking for?

    If the response was that you wanted the running time to be to be O(n^2) with no restriction on the input set size then as an interviewee I would likely wonder if it is a trick question or not.

    However, if we are talking about a restricted set size, for example no more than 10 cities, then the simplest one to demonstrate during an interview would be to use a combinadic algorithm to generate the possibilities and then run through them to find the one that is the smallest. It's not going to win any awards for speed. But if the size of the input set is limited then even an inefficient algorithm is going to seem "fast" due to the speed at which modern computer operate. Another justification for this is that if the input set is limited then there is no point in spending a significant amount of time to research a faster solution when the problem is known to be NP-Complete.

    From Rob
  • Have a look at this pdf. It seems intersting.

  • Try this.

  • have a look.Travelling Salesman Problem