Wednesday, February 28, 2007

UAC in the Oven

I just noticed that Aleksander Oven, a honorable member of Delphi-SI community is blogging.

Just now he finished a great three-part series on Vista UAC and Delphi. Recommended!

Tuesday, February 27, 2007

Building in Delphi 2007

[I'm blogging about Spacely - the beta version of Delphi 2007 for Win32 - with special permission given from CodeGear. Thanks, guys!]


Today I'll focus on a feature that I'm already exploiting a lot - build configurations.

Delphi 2007 for Win32 will use Microsoft's MSBuild engine for project building. In addition to faster IDE compile times, this will bring us great new improvement - build configurations. In short, that will allow you to give a name to a group of of compile settings and then recall them at will. Standard installation already defines Debug and Release configurations, but you are free to add many other.

In IDE, build configurations show as a new drop-down combo, visible on some Project Options pages - namely on Compiler, Compiler Message, Linker, and Directories/Conditionals.

Build configuration - click for larger picture

Another feature of the new build engine are pre- and post-build events. You can attach a list of commands (calls to external programs) that will be executed before and after building. Quite powerful, as you can integrate external steps required for making a release here.

Pre- and post-build events - click for larger picture

That's all for today. Stay tuned as I have more to show ...

Monday, February 26, 2007

Four Ways to Detect Vista

Pop quiz. How do you detect Windows version? Simple, call the GetVersionEx API.

And now for a bonus question. How do you detect Windows version without a doubt? Well, that's not so simple. Do you disagree? Read on.

Compatibility mode

For some time (I think this functionality appeared in Windows XP), you can execute applications under a compatibility layer. Right-click your exe, select Properties and click on the Compatibility tab. Click on the Run this program in compatibility mode for. Now you can select from the list of many operating system versions - from Windows 95 onwards.

When you do this, plenty of things happen. Firstly, Windows starts faking OS version to your program. Secondly, some API functions work more like they did in the selected OS version.

Why would you do this? Maybe the program in question is not well-behaved and needs this kind of help from the OS. Or maybe it just tests for supported OS version too aggressively and you want to circumvent this test.

In all cases, result is the same - what you get from GetVersionEx is not the true answer. So, I have to repeat my bonus question - how could you detect the true OS version?

Four ways to detect OS

I run into that problem few weeks ago. We have an application that is not yet Vista-ready, mostly because of some DirectX incompatibilities. We don't want negative user experience and therefore we don't allow the app to run on Vista at all. Still, some users may be smart enough to enable compatibility mode for this application, which would make OS version test useless (sadly, compatibility mode doesn't help this application to work correctly on Vista).

I found no good solution but luckily my fellow Slovenian Delphi users did. Even more, they found three alternative solutions.

One is to check for system file that was not present in previous OS versions. For example, one can test for presence of %WINDIR%\System32\ndfetw.dll.

One is to check Notepad.exe version. Under Vista, Notepad has version 6.something while it was 5.something in XP.

And the last one (and the best, in my opinion) is to check if specific API is exported. For example, Vista exports GetLocaleInfoEx from kernel32.dll while previous Windowses didn't.

That's it - now you have one way to get simulated OS version (actually, there are two - you can check the HLKM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion key) and three to get true OS version.

The last approach is so flexible that we (Miha-R from the Delphi-SI forum, who did most of the work, and me) turned it into a function that can detect all 32-bit OS versions simply by checking for exported API functions. After some work, we created a list of OS-version-determining APIs, all exported from kernel32.dll.

  Vista Server 2003 XP SP1 XP 2000 ME NT 4 98 95 OSR 2 NT 3 95
GetLocaleInfoEx  x                    
GetLargePageMinimum  x  x                  
GetDLLDirectory  x  x  x                
GetNativeSystemInfo  x  x  x  x              
ReplaceFile  x  x  x  x  x            
OpenThread  x  x  x  x  x  x          
GetThreadPriorityBoost  x  x  x  x  x    x        
IsDebuggerPresent  x  x  x  x  x  x  x  x      
GetDiskFreeSpaceEx  x  x  x  x  x  x  x  x  x    
ConnectNamedPipe  x  x  x  x  x          x  
Beep  x  x  x  x  x  x    x    x  x

Writing the function - DSiGetTrueWindowsVersion - was quite simple; just proceed from newest OS to oldest and check for exported APIs. You can get this function (and many more) by downloading the freeware DSiWin32 unit (another effort of Slovenian Delphi community).

Checking for compatibility layer

It would be also interesting to detect whether the application is running under the compatibility layer. There is a registry key that contains this info - Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers, key name is full path of the executable - but the information itself is not reliable.

First, you have to check both HKLM and HKCU branches, as the compatibility may be set by current user and enforced by the system administrator. Second, on 64-bit Windows, you must check the (true) HKLM64 branch, not the (32-bit compatibility) HKLM branch (see the code below for more detail). And third, all this may be to no avail.

The problem lies in a fact that child processes inherit compatibility layer from parents. If you set compatibility mode for executable A and then run executable B from it, executable B won't have any compatibility data set in the registry but nevertheless it will run under the compatibility layer. Still, information in those keys may be of some use sometime.

Demo

Try running it as a normal app and under the compatibility layer.

program IsVista;

{$APPTYPE CONSOLE}

uses
Windows,
SysUtils,
GpVersion,
DSiWin32;

var
testFile: string;

begin
Writeln('GetVersionEx API: ', CDSiWindowsVersionStr[DSiGetWindowsVersion]);
Writeln('HKLM CurrentVersion registry value: ', DSiReadRegistry(
'\SOFTWARE\Microsoft\Windows NT\CurrentVersion', 'CurrentVersion', '',
HKEY_LOCAL_MACHINE));
Writeln('HKLM ProductName registry value: ', DSiReadRegistry(
'\SOFTWARE\Microsoft\Windows NT\CurrentVersion', 'ProductName', '',
HKEY_LOCAL_MACHINE));
testFile := IncludeTrailingPathDelimiter(DSiGetSystemFolder) + 'ndfetw.dll';
Writeln('Presence of file ', testFile, ': ', BoolToStr(FileExists(testFile), true));
Writeln('Notepad.exe version: ',
CreateResourceVersionInfo(IncludeTrailingPathDelimiter(DSiGetWindowsFolder) +
'notepad.exe').GetFormattedVersion(verFullDotted));
Writeln('Presence of GetLocaleInfoEx API: ',
BoolToStr(GetProcAddress(GetModuleHandle('kernel32'), 'GetLocaleInfoEx') <> nil, true));
//http://msdn2.microsoft.com/en-us/library/aa480152.aspx
Writeln('HKCU AppCompatFlags for ', ParamStr(0),': ',
DSiReadRegistry('Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers',
ParamStr(0), '', HKEY_CURRENT_USER));
Writeln('HKLM AppCompatFlags for ', ParamStr(0),': ',
DSiReadRegistry('Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers',
ParamStr(0), '', HKEY_LOCAL_MACHINE));
if not DSiIsWow64 then
Writeln('Not a 64-bit OS, KEY_WOW64_64KEY not tested')
else begin
//http://msdn.microsoft.com/library/en-us/sysinfo/base/about_the_registry.asp
Writeln('HKLM64 CurrentVersion registry value: ', DSiReadRegistry(
'\SOFTWARE\Microsoft\Windows NT\CurrentVersion', 'CurrentVersion', '',
HKEY_LOCAL_MACHINE, KEY_QUERY_VALUE or KEY_WOW64_64KEY));
Writeln('HKLM64 ProductName registry value: ', DSiReadRegistry(
'\SOFTWARE\Microsoft\Windows NT\CurrentVersion', 'ProductName', '',
HKEY_LOCAL_MACHINE, KEY_QUERY_VALUE or KEY_WOW64_64KEY));
Writeln('HKLM64 AppCompatFlags for ', ParamStr(0),': ',
DSiReadRegistry('Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers',
ParamStr(0), '', HKEY_LOCAL_MACHINE, KEY_QUERY_VALUE or KEY_WOW64_64KEY));
end;
Writeln('DSiGetAppCompatFlags: ', DSiGetAppCompatFlags(ParamStr(0)));
Writeln('DSiGetTrueWindowsVersion: ', CDSiWindowsVersionStr[DSiGetTrueWindowsVersion]);
Readln;
end.

Friday, February 23, 2007

Nasty inline codegen bug in BDS 2006

I'm using assign-test-and-do-something code pattern quite frequently. You know, code like this:

Result := CalculateSomething(some, param); // assign
if not Result then // test
Exit; // do something

As all real programmers, I'm lazy and tend to rewrite such code as:

if Asgn(Result, CalculateSomething(some, param)) then
Exit;

Most of the dirty work is done by the Asgn function, which acts as a splitter - it copies one value to two destinations - output parameter and function result.

function Asgn(var output: integer; const value: integer): integer;
begin
output := value;
Result := value;
end;

(Actually, I have four overloaded Asgn functions, but that's beside the point.)


Yesterday, I did two things almost at the same time (which was, in hindsight, very lucky - it helped me to find the problem in "mere" 30 minutes). Firstly, I changed all Asgn functions to be inlined on modern Delphis and secondly, I used same variable as part of the value calculation and as the output parameter. In other words, I did this: 

function Asgn(var output: integer; const value: integer): integer; inline;
begin
output := value;
Result := value;
end;

copy := Asgn(v1, v1+v2);

(It was slightly more complicated than that but the code above nicely summarizes the problem.)


This code should calculate a sum of two variables and assign it to both v1 and copy. It should, I said, but it didn't. Correct value was assigned to v1, but something completely different was assigned to the copy variable. When I removed the inline, Delphi immediately started to generate correct code.


So, for now, I removed inline from all my Asgn functions. I also know that I'll be extremely causious when using inline in the future.


CodeGear has been notified of the problem (QC #41166) and I hope they'll be able to fix the codegen before Delphi 2007 is out.


If you don't believe me, try it for yourself. Demo is below. It should output four numbers 17, but it doesn't.

program Project8;

{$APPTYPE CONSOLE}

uses
SysUtils;

function Asgn1(var output: integer; const value: integer): integer;
begin
output := value;
Result := value;
end;

function Asgn2(var output: integer; const value: integer): integer; inline;
begin
output := value;
Result := value;
end;

var
v1, v2: integer;

begin
v1 := 10;
v2 := 7;
Writeln(Asgn1(v1, v1+v2));
Writeln(v1);
v1 := 10;
v2 := 7;
Writeln(Asgn2(v1, v1+v2));
Writeln(v1);
Readln;
end.

Thursday, February 08, 2007

Free tools I use for development

Now and then I find a nice free tool hidden on the web. Lately, I found some nice gems mentions on other blogs and I thought I should share my list with you, dear reader. Maybe you'll find something of use and maybe you could recommend some other good tool to me.

 

Another Lens  - A screen magnification tool, written in Delphi 4. [Bjorn Ischo; link from the author's home is not working, this is a correct one: http://www.b-zone.de/zips/lens.zip]

Desktop Icons Save and Restore* - Shell extension that allows you to save and restore position of icons on the desktop. [Jamie O'Connell, http://www.midiox.com/html/desktop.htm]

FireBird - Great SQL database server. I'm particularly interested in the embedded version. [Firebird Foundation, http://www.firebirdsql.org/, http://www.ibphoenix.com/]

GpProfile - My old but still useful profiler. (I am working on a new version.) [Primoz Gabrijelcic, http://gp.17slon.com/gpprofile/index.htm]

GraphEdit -  A tool for visual DirectX filter graph editing.  [Microsoft, part of the DirectX SDK, http://msdn2.microsoft.com/en-us/library/ms783346.aspx, but is easy to find on the web if you don't want to download and install entire SDK]

IcoFX - Great icon editor and newest addition to my toolbox. [Attila Kovrig, http://icofx.xhost.ro/]

Microsoft Spy++ - Window browser. [Microsoft, part of Visual Studio, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcug98/html/_asug_home_page.3a_.spy.2b2b.asp, easy to find on the web if you don't need complete Visual Studio]

Putty - Ssh and sftp client. [Simon Tatham, http://www.chiark.greenend.org.uk/~sgtatham/putty/]

IBExpert Personal Edition - InterBase/FireBird management tool. [HK Software, http://www.hk-software.net/ibexpert.com/content/doc_122943-5-10-00.php]

Synergy* - Control multiple computers with one mouse and keyboard. [http://synergy2.sourceforge.net/authors.html, http://synergy2.sourceforge.net/, http://www.stevetrefethen.com/blog/default,month,2006-06.aspx]

TightVNC* - Remote control client and server. [http://www.tightvnc.com/people.html, http://www.tightvnc.com/]

VLC Media player - Media player, which is of particular interest to me because it can play (and wrap/unwrap) DVB Transport Streams. [VideoLAN, http://www.videolan.org/]

WinCVS - CVS GUI which is so good that I don't want to switch to Subversion. [http://sourceforge.net/project/memberlist.php?group_id=10072, http://sourceforge.net/projects/cvsgui]

Window Clippings - Nice screen grabber, useful when writing documentation. [Kenny Kerr, http://weblogs.asp.net/kennykerr/archive/2005/09/30/Window-Clippings-1.0.aspx]

Windows SysInternals tools - I need them all. Constantly. [Mark Russinovich, http://www.microsoft.com/technet/sysinternals/default.mspx]

Wink - Records all changes on the computer screen and makes a movie. Good for tutorials and to demonstrate a problem to a coworker. [DebugMode, http://www.debugmode.com/wink/]

WinPCap - Tool to monitor and transmit data in link-layer network access, used in Wireshark (below). [http://www.winpcap.org/misc/credits.htm, http://www.winpcap.org/]

WinSCP - Graphical sftp interface, based on the Putty. [http://winscp.net/eng/docs/contributions, http://winscp.net/eng/index.php]

Wireshark [ex Ethereal] - Capture and analyze network packets. [http://www.wireshark.org/about.html, http://www.wireshark.org]

XanaNews - Good newsgroups reader, written in Delphi. [Colin Wilson, http://www.wilsonc.demon.co.uk/xananews.htm]

XN Resource Editor - Resource editor, written by the XanaNews author. Includes simple icon editor. [Colin Wilson, http://www.wilsonc.demon.co.uk/d10resourceeditor.htm]

 


*More on this product: Remote access to the fourth power

Friday, February 02, 2007

When Vista doesn't print

I installed Vista RTM immediately as it was available on the MSDN.

That was quite a surprise for me as I thought that many months would pass before my first Vista installation. But alas, I had to write an article on Vista for Slovenian leading computing magazine Monitor and I just got completely new Vista-ready laptop which I could use for playground. (It got Windows XP Home preinstalled - yuck!)

Anyway, I installed Vista (or, to be more precise, Windows Vista Ultimate) on that laptop. Everything worked fine (even though I had to scrap together few drivers from all over the internet). Everything except that I couldn't print. I have this nice colour laser printer (Samsung CLP-510, really smooth) in the office, and it is connected to the Windows 2003 Server. All other computers (running XP everyone of them) could use this shared printer but not the Vista, nooo sir.

It was quite an interesting malfunction, too - when I tried to connect to my shared printer, Vista started copying drivers (XP drivers, of course), and then reported that driver installation 'ran out of memory'. OK, I only have measly 2 gigs installed, but still...

I tried a different trick - installed printer driver locally (no problem here) and only after that tried to connect to the shared printer (in hope that currently installed driver will be used). But nooo, Vista reported same error again.

I gave up then saying: "Oh, well, it's only about two months until Vista is available for general public. Surely Samsung will have Vista drivers ready by then." But the problem was still nagging me and I spilled out my soul to my friends on Slovenian Delphi forum. Somebody has an interesting idea - why don't I connect to the Samsung as if it were a TCP/IP printer and then spool EMF content to the server. Still, he had no idea how to make this work in practice.

I searched for the solution occasionally but found out that Microsoft documentation on that topic is really hard to find if you don't know what you're searching for. I did'nt spend much time on that, though, as I could always use the printer from my main computer.

Solution, at last!

Today I returned to the problem and somehow managed to find the answer in Microsoft's documentation. It really is simple - when you already know how to do it :( .

For anybody trying to solve a similar problem - and for all of you that just want to learn more - here is the solution.

First, install Print Services for Unix on the Windows 2003 Server (Control Panel, Add or Remove Programs, Add/Remove Windows Components, Other Network File and Print Services, Details). Make sure that the TCP/IP Print Server service is running.

Next, install LPR Port Monitor feature on the Windows Vista (Control Panel, Programs and Features, Turn Windows features on or off, Print Services).

Create new port on the Windows Vista computer. Run Printers and clik Add Printer. Choose Add a local printer. Select Create a new port setting and set Type of port to LPR Port.

Click Next and enter address of the server providing Unix printing services and name of the printer.

Click OK and proceed as usual - select printer manufacturer etc.

All work to no avail?

Using this procedure I managed to get my Samsung working from the Vista.

I learned a lot and maybe I will be able to use this knowledge in the future.

Maybe it will help somebody out there with a similar (or completely different) problem.

It didn’t help me at all :(

You see, it is February already and Samsung published updated printer drivers two days ago.