Jan 01
My Lenovo X200 notebook comes with an integrated webcam and I’m using it pretty often for Skype calls etc. But some time ago it stopped working. After reinstalling drivers, taking to the Lenovo support hotline, installing exactly the driver I was told to it was still broken. Yesterday a Lenovo technician cam to replace the camera (I’m so glad to have on-site support and do not need to send the notebook in), but in the end the new webcam behaves exactly as the first one – just not working.
So I started to debug the problem by myself and figured the following out. Maybe this is just an issue on my machine with my other software installed but maybe this is helpful for someone else.
The driver from the Lenovo webpage is build by the camera vendor Ricoh. That driver package just does not work on my Windows 7 64bit machine. The out-of-the-box Windows 7 driver works just fine!
Find the Camera device in Device Manager

On the properties sheet choose Update driver

Then “Browse my computer for driver software” and “Let me pick from a list of device drivers on my computer”


On my machine it only shows up one compatible “USB Video Device” – just use that driver:

If Windows does not find this single one you can select it from the larger list by unchecking “Show compatible hardware” and choosing “Microsoft” / “USB Video Device”:

For some reason it does not always work the moment I installed the new driver and I had to reboot the machine. After installing the driver the device in device manager is called “USB Video Device” but after the reboot it is for some reason renamed to “Integrated Camera” but still using the Windows driver (that is the important part, you can check it in the properties sheet from within device manager). From now on it was working fine.
After some time Windows suddenly replaced this driver with the Ricoh one and the camera was broken again. To prevent Windows from doing so I uninstalled the driver package from “Programs and Features” in Control Panel.
Dec 27
I’m using a Lenovo laptop and I’m a big fan of the Trackpoint mouse replacement. That is this small red thing in the middle of the keyboard some notebooks are equipped with instead of a touchpad. In fact my notebook does not even has a touchpad.
You can use the Trackpoint for scrolling like a mouse wheel when pressing the middle button and moving the Trackpoint up or down. Everybody using it will know what I’m talking about, anybody else does not need to read to the end.
But in some applications the scrolling does not work at all what is very annoying. I had the most trouble with Thunderbird and iTunes – just no scrolling.
And here is a fix for that issue. There is a file (C:\Program Files\Lenovo\ TrackPoint\tp4table.dat) on your machine when you have installed the Trackpoint software from Lenovo. Just edit that file and add some additional rows for the application with broken scrolling.
; Apple ITunes
*,*,itunes.exe,*,*,*,WheelStd,0,9
; Mozilla Thunderbird
*,*,thunderbird.exe,*,*,*,WheelStd,0,9
|
Remember to open the file with an editor as Administrator otherwise it would not work. I had to reboot my machine after that change. I just tested this change on a Windows 7 64 bit machine but it should work on 32 bit or other versions of Windows as well. Maybe the same fix applies also to non-Lenovo notebooks but I cannot tell. If someone out there tested it, please comment.
More on this topic and templates for some more applications (Picasa, Safar, Windows Live Mail, Windows Live Writer) can be found at http://forums.lenovo.com/t5/X-Series-ThinkPad-Laptops/Fixing-Trackpoint-Scroll-for-ITunes-Picasa-Safari-Windows-Live/m-p/124378
Dec 22
When building applications that mix WPF and Winforms windows (e.g. because you are using a Winforms dialog within a WPF application) you will sometimes need a System.Windows.Forms.IWin32Window interface from an WPF System.Windows.Window. This will be useful when using the WinForms OpenFileDialog and wanting to provide a parent window to the ShowDialog() method that only accepts IWin32Window as parent.
The IWin32Window interface declares only one read-only property named Handle that must be provided by the WPF Window.
Implementing the Interface
It is very simple to just add this interface to an existing WPF Window and implementing the single property:
public partial class Window1 : Window, IWin32Window
{
public IntPtr Handle
{
get
{
var interopHelper = new WindowInteropHelper(this);
return interopHelper.Handle;
}
}
}
Using a wrapper class
It does not look as a very good solution to add such an interface to each and every Window in you whole application only to be able to be the parent of a dialog. So instead of implementing the interface directly you could build a wrapper class that implements this interface and use this wrapper when opening a dialog:
public class Wpf32Window : IWin32Window
{
public IntPtr Handle { get; private set; }
public Wpf32Window(Window wpfWindow)
{
Handle = new WindowInteropHelper(wpfWindow).Handle;
}
}
Opening the dialog will now look like:
d.ShowDialog( new Wpf32Window(this) );
The grand solution to this problem
I like the wrapper solution more than implementing an interface to each and every window but I do not like the changed ShowDialog() call. It just does not look natural any more. To solve this I created an extension Method to be used with the wrapper class:
public static class CommonDialogExtensions
{
public static DialogResult ShowDialog
(this CommonDialog dialog,
Window parent)
{
return dialog.ShowDialog(new Wpf32Window(parent));
}
}
Now the call to open a WinForms dialog with an WPF parent looks as it should look like:
d.ShowDialog(this);
Dec 22
In fact one OpenFileDialog is enough but the .net framework provides two of them:
- System.Windows.Forms.OpenFileDialog in System.Windows.Forms.dll
- Microsoft.Win32.OpenFileDialog in PresentationFramework.dll (that is WPF)
With such a setup one would use the first for WinForms applications and the second for WPF apps. In fact if you create a WPF application and just type in OpenFileDialog you will get number 2.
But the 2nd one (even if it is part of the newer part of the framework) is an old dialog where the 1st will use the new and shiny Windows 7 dialog on Win7. Just look at the following screenshots.
Only the first class uses the Windows 7 dialog and comes with proper support for Libraries etc. Even if the second shows the Libraries in its left part it does not fully support this new Windows 7 feature.
I have no idea why the WPF class uses the old windows API but it does. For best compatibility with all Windows versions (including 7) you should always use the Winforms Dialog even when building WPF apps! It does not hurt to reference the System.Windows.Forms.dll (it is installed an every machine running the .net framework) and you can use that dialog from WPF without any trouble.
You should only know about one little disadvantage when using the WinForms dialog. It only accepts WinForms as a parent window no WPF windows. To overcome this limitation please read my other article about the IWin32Window interface and how to implement / provide it in WPF at http://www.ticklishtechs.net/2009/12/22/how-to-get-a-iwin32window-from-a-wpf-window/
Aug 02
Disclaimer: The following is no official advice or anything. I just did it and it worked ON MY MACHINE. Fell free to try but have a backup of your data.
The Windows 7 RTM bits will not allow an upgrade from an earlier version of Windows 7 (e.g. RC or beta). I belief that Microsoft did this to ensure that your new operating system works just fine and no beta and release bits get mixed up.
For the upgrade from the beta version to the release candidate quickly some patch occurred which allowed an upgrade. I used the same one and it works fine on my machine to upgrade Windows 7 RC to RTM.
What’s to do?
You need to change one file on the installation DVD. On way to do this is to copy all files to your computer, change the file and burn a new DVD. Since the upgrade will be run from within the already installed Windows 7 you do not need to boot from this DVD. I just copied all files to a USB drive and run the setup.exe from that drive. I belief you will be able to install Windows also from a directory on you local hard disk containing all the files from the DVD.
In the source folder you will find a text file named cversion.ini. The original file looks like:
[HostBuild]
MinClient=7233.0
MinServer=7100.0
|
Just change the MinClient number to the build version of you current Windows version. For the Windows 7 RC it will be 7100.
The upgrade should work fine now. But if your new Windows 7 starts acting strange don’t blame Microsoft or me. Always remember you installed it with an unsupported patch.
Apr 28
Disclaimer: I’m absolutely not the person who can tell a lot about Java. I learned it a few years ago for university, but used it most of the time only for small example stuff. Never as deep as I’m working with .net. But maybe my newest discoveries are useful for more programmers with .net background.
I started using Java 1.4 and until last week I never took a look at Java 5 or 6. The only thing I was aware of were generics in Java.
iterators in Java
I was always annoyed in the missing of a foreach loop in Java. There is a concept of iterators in Java comparable to the .net Enumerators. But to loop over a list you will find in millions of Java programs the following code:
Iterator<Integer> i = list.iterator();
while(i.hasNext()) {
Integer current = i.next();
// do something with the current element
}
Sometimes build into a regular for loop, but I don’t think that is more readable or convenient:
for(Iterator<Integer> i = list.iterator(); i.hasNext(); ) {
Integer current = i.next();
// just do something
}
By the way: Can anyone tell me why the method is called iterator() and not getIterator() as used everywhere else in Java?
foreach in Java
With Java 5 (or 1.5 – call it as you like) a foreach loop was introduced. This is about five years ago, so I’m very fast in getting news from the Java world.
for(Integer i : list) {
// just use i as the current element
}
I like it. It’s working for everything that implements java.lang.Iterable and for regular arrays.
Apr 10
Imaging a small C# winform app with just one ListBox and the following code:
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
Thread t = new Thread(() => AddToListbox(i));
t.Start();
}
}
private void AddToListbox(int i)
{
if (this.InvokeRequired)
this.Invoke(new Action<int>(AddToListbox), i);
else
this.listBox1.Items.Add(i);
}
A simple loop iterating over the numbers from 0 to 4 and adding these values asynchronous to a ListBox. What do you expect? I expected the numbers from 0 to 4 shown in the ListBox but in any random order since I do not control the threads in any way.

I didn’t expect any number to appear multiple times and I’m totally surprised to see the number 5!
But ReSharper gave me a hint that I often saw but never understood:

So what’s going on?
I use the syntax of an Lambda expression instead of a regular function call (e.g. using the ThreadStart class and a delegate). This Lambda expression is not evaluated until the thread uses it. And by this time, the loop can be in its next iteration. If the loop is already finished i will be 5.
That is exactly what R# tries to tell me: “Hey, you are accessing here a variable but change it later on. Maybe that is not a good idea.”. – It isn’t.
The solution
Just make a copy of i before passing it into the expression. This copy must be a private copy that will not be changed later. The easiest way to do so is declaring a variable inside the body of the loop. In every iteration of the loop a new integer will be created on the stack and the Lambda expression will access this one.
for (int i = 0; i < 5; i++)
{
int copy = i;
Thread t = new Thread(() => AddToListbox(copy));
t.Start();
}
Jan 21
Ok, what does this expression do or mean? First of all: never write something like this in your code. Nobody understands the meaning with a single look and that will lead to confusion and maybe to a bug in your software someday.
Now let’s take a deeper look. First you may think about the difference between x++ and ++x. If you write it as a single statement both expressions are equal, but if you use it as a part of a complex expression the first will evaluated to the value of x before increasing it; the second one will evaluate to the new value of x. So y = x++; leads to a different result for y as y = ++x;.
x+=a simply is a ‘shortcut’ for x=x+a.
Now let’s do it step by step. For example x is 5. Then first x will be increased by one to 6 but the old value will go into the formula that remains as x=x+5. Since x was increased before the result will be 11.
If you think that is all right, than please take a break and test it with your favorite compiler. If you are a C or C++ guy you will in fact receive 11 as an answer and everything is fine. But if you are a C# or java guy x will be 10. Why?
.Net as well as the Java Runtime are stack machines. The expression will be put on the stack step by step before evaluating the whole thing. At that time x is 5. x will be changed to 6 by the x++ part, but that only happens in the main memory. The old value (5) is still on the stack. After executing the whole expression the changed x will be overwritten by 10 (5+5).
And once again: NEVER write code like this!
Jan 13
I found this somewhere and I think it’s worth to write a short (only a very short) entry.
throw null;
If you use this expression in your C# code it will throw a NullReferenceException. That is because the throw-statement needs an object of type Exception as its single parameter. But this very object is null in my example.
I like this way to produce some exception while testing code.
Jan 05
It was very silent here in the past month. That was mainly because I was working on my diploma thesis until the end of September. After that I did my final exam and started working in Bonn. Now it is December and almost 2009. I’m sitting in a plane to Australia for holiday and have much time to write articles. I will try to keep this blog alive and will write articles more frequently.
Today I will introduce Flow. With Flow you can model the behavior of nodes in a wireless sensor network (WSN) in a data driven and event driven manor. I developed Flow as part of my diploma thesis.
Flow is build as a Visual Studio Addon (a VS Package) on top of the DSL-Tools containing three different domain specific languages to describe different parts of the software running on a sensor node. These different DSLs are working together and using most of the techniques I described earlier in this blog. E.g. my library JaDAL was initially build to support this diploma thesis.
You will find screenshots, more explanations and Flow itself on http://flow.irgendwie.net. While the webpage and the software are available in English the thesis itself is a pdf-document and can only be downloaded in a German version.
The software is fully working and if no wireless sensor network is available you can use a node simulator to evaluate Flow. The code is released under the new BSD license and also available for download.
Screenshots:
Dataflows modeled with Flow looks like that:

The simulated sensor nodes are represented as Windows forms programmed by such dataflows:

For more information just visit http://flow.irgendwie.net.
Recent Comments