Blog

Fun with Sockets

Actually, not fun at all. This problem has been driving me nuts for hours.

There are some things I like about .net socket programming, but one thing that always causes horrible problems is shutting sockets down when I’ve finished with them. One way or another, I always end up having to trap some exceptions. Continue reading “Fun with Sockets”

Incorporating MS Identity database into an application database

I’ve just started writing my first serious .NET MVC application. It’s intended to be a slightly cut-down web version of Calibre, supporting only epub format (at least, initially). It has a SQL Server database where everything is stored, and will allow the user to upload epubs, then extract metadata from them, and put it all into database records organised by user. All properly normalised, of course, so that if, for example, two users upload the same epub file it will only store one copy. Continue reading “Incorporating MS Identity database into an application database”

The VS IDE, the FTD2XX dll, the mouse, the keyboard, and a dent in a brick wall

I’m just starting a project that involves talking to a board with an FT245R USB interface. I’ve got an XP VM set up with VS 2010 (some target machines will be running XP), the FTD2XX dll, the C# interface code published by FTDI, and some working VB6 code to get started with.

All seemed straightforward-ish. The first thing I had to do was rewrite the C# interface code to take out the message boxes it popped up when there were errors, and change it so that it throws exceptions instead of returning statuses that have to be checked for every call. It uses a somewhat bizarre and long winded way of referencing the functions in the dll, getting function pointers and marshalling them to delegates as they are used (rather than just using extern declarations), but it works so I left it.

Next thing was to create a Windows Forms app, and open the USB device using its description. Worked straight away. Excellent. Put some buttons on the form and run it again. Didn’t work – failed to open the device. That happened repeatedly, so I tried running the VB program. That worked, then running the app again worked. Then it didn’t. Continue reading “The VS IDE, the FTD2XX dll, the mouse, the keyboard, and a dent in a brick wall”

When is a char not a char? When it’s an int.

Just discovered a curious quirk in C#’s handling of the char type. Most of the time you can just think of it as being a character type, like a single character within a string, but actually it isn’t. It has an implicit coversion to int, but not to string, so

Console.WriteLine('A' + 'B');

would produce 131. Continue reading “When is a char not a char? When it’s an int.”

FtpWebRequest bug

I’ve just run into what appears to be a bug in .net’s FtpWebRequest class. I wrote some rather inefficient code that requested the same directory listing from the server several times, and found that it hung the second or third time it made the request. After some googling, I found an explanation. It seems that it caches some of the objects it creates and retrieves them from a pool, which is fine, but if it finds that the request is identical to the existing one it doesn’t reopen the response socket, so when you try to get the response stream it hangs. It’s a simple fix in my program, just save the result of the list request, but it would make it very difficult to write something that works by polling the contents of an ftp folder.

Ejecting recalcitrant USB disks

I’ve got a couple of USB SATA caddies that I often have problems with when I try to remove them safely to switch them off. The disks in them are seen by the system as fixed drives, and the System process (pid 4) often opens some system files (in the $extend folder) on them that prevent the USB device being removed. Because they are seen as fixed drives there’s no Eject option in Explorer.

I’ve finally discovered how to eject them safely. Open Disk Management (either through Control Panel, or just running diskmgmt.msc) and set the disks to be offline by right clicking on the disk name in the lower panel. Then they can be ejected. The only problem is that they will remain offline, so you need to make them online again before they can be used next time.

Setting up a simple Windows 7 VPN

Although I work mostly from home, I rent a hot desk at a local business centre, and take my laptop in there to work on a couple of times a week just for a change of scene and because I get a large discount in their cafe. I often need to connect to my home network while I’m there, and I’ve been doing it by having a remote desktop port open on the router. This is far from ideal, and so I decided to set up a VPN. Both the home PC and the laptop are running Windows 7 Ultimate, which has VPN built in, so it should have been a 10 minute job, but it wasn’t. It just took me all morning to get it working. Continue reading “Setting up a simple Windows 7 VPN”