All posts by onlyconnect

C# code to detect UAC elevation on Vista

A couple of days ago I posted about programmatically detecting whether UAC is enabled. I was proposing to read a registry entry. Thanks to the power of blogs, my post drew a comment from Andrei Belogortseff, who has posted C++ code that does this properly. His library has functions for detecting whether the current process is elevated, detecting whether UAC is enabled, and running new processes both elevated and non-elevated (the last of these is the hardest to do).

I liked the libary but wanted to use it from .NET. I compiled his code to a DLL and did a C# wrapper; then I considered the hassles of adding a dependency on the Visual C++ runtime libraries. I therefore did a quick C# implementation of the functions I cared about; I haven’t included the code for running new processes. Please go ahead and download the code, and let me know if you have any suggestions, or even if there is a much easier way to do this that I have missed. There is an app called UACElevationCheck, which calls functions in VistaTools.cs.

The code is only intended to be called from Vista and will throw an exception otherwise; of course you can modify it as you like.

I’ve included a function IsReallyVista that looks for a Vista-only API call, rather than relying on the reported version information. This is because the version information is unreliable if the app runs in a compatibility mode. On the other hand, IsReallyVista is a hack; if you don’t like it, use IsVista which uses the version information instead.

You may be able to do this using the System.Security namespace rather than PInvoke. I had a quick look but the PInvoke code seemed easier to me, especially since Belogortseff has already done the real work.

 

Technorati tags: , , , , , ,

More Windows Installer confusion: managed code custom actions a no-no

People who should know say not to use managed code custom actions in your Windows Installer setups. Like Rob Mensching of Wix fame who says:

…today the Windows Installer does not support managed code CustomActions and the general direction appears to be to try to reduce the need for CustomActions overall.

Read the blog entry for the reasons. The strange thing is, Visual Studio 2005 has specific support for managed code custom actions; there is an Installer class and an MSDN walkthrough on how to use it

Now, I have always avoided managed code custom actions anyway, but only from instinct. Windows Installer setups are problematic enough as it is; adding .NET Framework dependency seems unnecessary. Mensching’s post above explains why it is also error-prone. See also Aaron Stebner’s post from March 2005.

… in summary, I strongly encourage you to not use managed code in your product setup.  I realize that some teams do this here at Microsoft, but please don’t use our bad (in my opinion) examples to justify doing so in your own setup….

Presuming these guys are right, might it not be wise for the Visual Studio folk to remove support for managed code custom actions from the product? A point of confusion is that the managed installer class works with a utility called InstallUtil which is distinct from the Windows Installer; in fact, I believe that Visual Studio setup projects which include managed code custom actions actually call InstallUtil.

I suggest Delphi as a handy alternative, if you would rather avoid Visual C++.

Finally, I hear many good things about Wix, the free XML-based authoring too for Windows Installer setups. If you are embarking on a new setup project it is worth a look.

 

Technorati tags: , , ,

Programmatically detecting whether UAC is enabled

An application may sometimes need to know whether or not the current user has administrative rights. This is complicated in Windows Vista by User Account Control (UAC) – the user may be in the Adminstrators group on the local machine, but nevertheless running with limited rights. When I came across this blog entry on COM elevation by Christoph Wille(thanks to Daniel Moth) I was interested to see the function he mentions called IsUACEnabledOS. I downloaded his code, but was disappointed to see this:

// a really simple check that does not account for possible UAC-disabledness via group policy
public static bool IsUACEnabledOS()
{
int majorVersion = Environment.OSVersion.Version.Major;
int minorVersion = Environment.OSVersion.Version.Minor;

return (majorVersion >= 6);
}

This just detects Vista or higher; in fact, it won’t always return the correct result, since Vista will lie about the version number if an application is running under compatibility settings. How then do we discover if UAC is enabled? The best I’ve come across so far is to query this registry entry:

HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA

If the entry does not exist or is 0, UAC is not enabled. Of course there is no guarantee that this always work, but it’s unlikely to change. I also haven’t checked that it is there in the base versions of Vista; again, it is probably the same.

Update: See the comments to this post for a better solution from Andrei Belogortseff, using the GetTokenInformation API call.  

I’ve now posted a C# implementation of Andrei’s code.

Technorati tags: ,

A bug in Vista’s UAC

Vista’s User Account Control (UAC) elevation prompt sometimes appears when it shouldn’t. Here is an example which works every time for me. I have a folder in Documents (within my home directory) called recordings, containing MP3 files. I double-click one of these files and it opens in Windows Media Player. Now let’s say I try to rename the recordings folder. I get a dialog saying I need to confirm this operation, complete with a UAC shield.

I click Continue and get the screen flash and the elvation prompt. It’s not enough though; I now get a dialog that says “Destination Folder Access Denied – You need permission to perform this action.” If I click Try Again, I get the same dialog, for ever.

It’s nonsense of course. I don’t need permission; I need to close the MP3 file. Indeed, if I close the file I can then rename the folder.

Oddly, if I try this with a Word document, Vista correctly reports that the folder in in use by Word. But if I try a second time, I get the inappropriate UAC prompt.

It’s possible that some Windows API call is returning an access denied error, when it should indicate a file or folder in use by another process, or there could be some other explanation. The end result is a poor user experience.

If Microsoft can’t always get this right, it illustrates how hard it is for developers to give users appropriate error messages when working with UAC on Vista.

 

Technorati tags: , ,

Google apps in the real world

Or at least the semi-real world: Wired’s Michael Calore spent a month working (mostly) with Google apps rather than his usual desktop software (on a Mac). I’ve thought of trying this same experiment myself but haven’t yet felt that it is worth the risk.

A few points interested me. First, that he could live with the apps themselves, but ran into interoperability problems. One that surprised me:

One of our copy editors couldn’t open some docs I had exported, so I was forced to copy and paste those articles into Microsoft Word just for her.

I’d have thought the other way (Word to Google) would be more difficult, because of missing features. And Google’s response (product manager Jonathan Rochelle):

It works best when everybody in the group is sharing on the same platform,” he says. “The experience you’d have if you were just sharing stored docs rather than your co-workers asking you to save down to the desktop would be much closer to ‘Wow, this is an incredible product’ instead of ‘Wow, this really stinks’.

Oh, so if we all use Google we will be fine. That’s no better than Microsoft telling us all to use Office. Nevertheless I take the point to some extent – this web collaboration thing really only works if everyone plays. That doesn’t excuse the compatibility issues.

Calore mentions the privacy aspect, but more needs to be said here. I have no problem with internet storage; for example, I’m happy to save stuff to Amazon S3 without worrying that Jeff Bezos will start poking through the data. In fact, I consider S3 more secure than just saving files to a typical Linux box out on the Internet, especially shared hosts. Google troubles me though, because its business model is contextual advertising and you agree to let it mine your data, albeit with self-imposed limitations.

Finally there’s the question of whether the apps themselves are good enough. Again, I’d have liked Calore to have said more about this, though he reports how much he misses drag-and-drop. The impression I get is that the browser-based apps were a bit frustrating, but he says:

Eventually, I learned to accept that the browser had certain performance limitations that I would have to live with in exchange for the convenience of centralized storage and easy access. Rochelle says it’s just the way our brains are wired from decades of using desktop apps.

Calore has missed a point here – you can have centralized storage and easy access without necessarily using browser-based apps. Moving the server to the cloud is spot-on, but the case for solely browser-based apps is weaker. Its main advantage is zero install; but that’s the way desktop apps are going as well.

 

Using WordPress pages

Yesterday I posted an article on Office Open XML which is too long for a blog entry. Rather than creating a separate HTML file I used a WordPress page entry. WordPress pages are authored in the same way as blog posts, but are not part of the blog itself; they “live outside of the normal blog chronology.” You can organize them into a hierarchy of pages and sub-pages; they are important because they make it possible to build an entire web site in WordPress, using it as a simple content management system.

Curiously the page template in many WordPress themes omits comments. This caught me out: I marked the page as enabled for comments, but no comment form appeared. I fixed this by adding the following line to page.php:

<?php comments_template(); ?>

I’m now happy with the result and will probably use WordPress for further longer articles. In fact, I’ve already added a further page, this being my blog archive. When I migrated from bBlog to WordPress, I left the old blog engine in place so as not to break existing incoming links. However, although the old entries were still in place, most were left with no index link; they were effectively invisible. The new archive page fixes this; you can see all the posts since I started blogging in 2003: errors, insights and all.

 

Technorati tags: , ,

Mono’s new GUI library: how is this hype justified?

Mono’s Miguel de Icaza reports on a new GUI library for Mono:

We have been working on a technology that we believe will revolutionize user interfaces. Today we are announcing the response to Microsoft’s WPF/XAML, a response to Flash and WPF/E. A cross-platform GUI toolkit (supports Windows, MacOS and Linux and is easily ported to new platforms) written entirely in managed code and 100% open source.

There is some skeletal documentation for gui.cs.

Maybe this will be fantastic, but for the moment this looks like an early prototype, not a revolution in user interfaces. How is this hype justified?

Personally I reckon Mono will have to implement WPF eventually, if it wants to keep in touch with what is happening with Microsoft .NET.

 

Technorati tags: , ,

WPF/E is now Silverlight

Microsoft’s Flash alternative has a new name: Silverlight. Undoubtedly a radical shift in naming conventions. Back in 2005, Microsoft renamed Avalon to Windows Presentation Foundation, and I noted that:

These new names seem to be deliberately chosen to be forgettable.

Now we have memorable back. Interesting.

The name may be there, but the product is still in preview; the latest release remains the February CTP, with full release promised before July 2007. The emphasis is on video and vector graphics; there’ s no common language runtime implementation yet. You can write Silverlight code in JavaScript. It’s cross-platform, but currently only supports Windows and Mac; no device support yet. See the faq here for more information; and a useful summary from Tim Sneath who says there is big Silverlight news to come at Mix07.

 

Technorati tags: , , ,