Damir Arh's Corner
Search
Categories
  Development
 .NET
 Batch
 C++
 SQL
 VB6
 Vista
 Web
 Win32
  Downloads
 Amiga
 Articles
 Presentations
 Sources
 Windows
  Personal
 Education
 Software
 Website
Archives
July, 2008 (1)
June, 2008 (1)
April, 2008 (2)
December, 2007 (1)
November, 2007 (3)
July, 2007 (4)
June, 2007 (1)
May, 2007 (2)
March, 2007 (3)
January, 2007 (1)
December, 2006 (4)
October, 2006 (5)
September, 2006 (3)
August, 2006 (2)
June, 2006 (8)
May, 2006 (5)
April, 2006 (1)
March, 2006 (4)
February, 2006 (3)
January, 2006 (3)
March, 2003 (1)
February, 2002 (1)
January, 2002 (2)
August, 2001 (1)
July, 2001 (1)
February, 2001 (1)
December, 2000 (1)
September, 2000 (1)
July, 2000 (1)
Other Sites
Potepanja v naravi (sl)
Picasa Web Albums (sl)
moj-album.com Gallery (sl)
Bolha.com Auctions (sl)
My Game Space
LinkedIn Public Profile
My GamerTag
Sponsored Links
Administration
Sign In
Tuesday, April 29, 2008

Old ActiveX controls under .NET 2.0 SP1 (Development | .NET | Vista)

A .NET application in the company I work for recently started crashing under Windows Vista when trying to open a window implemented in an ActiveX DLL. Investigations showed that they were caused by an AccessViolationException "Attempted to read or write protected memory. This is often an indication that other memory is corrupt.". The cause was one of the ActiveX controls in the window. When instantited directly in a .NET Form the exception above was contained in a TargetInvocationException "Unable to get the window handle for the '<control name>' control. Windowless ActiveX controls are not supported.". Knowing that the control didn't suddenly turn windowless we dug deeper.

It turned out that the problem appeared with .NET Framework 2.0 Service Pack 1. Apparently it caused the C# compiler in Visual Studio 2005 and later to set the NXCOMPAT bit for all build targets without an option to turn this new behavior off. For those who don't know, this means that DEP (data execution prevention) will kick in unless it is turned off completely in the operating system. This wouldn't be a big deal unless ATL before Visual Studio 2005 didn't have a bug which caused the heap allocated memory not to be flagged as executable, which under the new circumstances results as the already mentioned exception. Windows XP has DEP turned off by default therefore everything still works but in Windows Vista it is turned on and prevents such application from functioning properly.

The best solution would of course be to recompile the ActiveX controls in Visual Studio 2005 or later but this might not be possible if they are supplied by a third party. In this case the most obvious approach to dealing with the situation is to disable DEP in Vista. There is a Data Execution Prevention tab in the Performance Options which open when you click the Settings button in the Performance frame of the Advanced tab in the System Properties dialog but it only allows switching between DEP for all processes with defined exceptions and DEP for essential Windows programs and services, i.e. executables flagged with the NXCOMPAT bit. The only way to turn DEP completely off is executing the following command with administrative privileges:

bcdedit.exe /set {current} nx AlwaysOff

After restart DEP will be turned off and problematic binaries will work once again. To restore the previous (default) state replace AlwaysOff with OptIn. The AlwaysOn option enables DEP for all processes and might cause additional problems (e.g. Google Calendar Sync in its current version 0.9.3.2 doesn't work in this mode).

Unfortunately, this solution would require all your Vista using customers to disable DEP as well which really isn't an option for commercial software. Fortunately, there is another solution. Although there is no compiler option to turn keep the NXCOMPAT bit unset, you can still do this after compilation using the editbin.exe which comes with C++ compiler for Visual Studio 2005 and later:

editbin.exe /NXCOMPAT:NO <filename.exe>

This command removes the NXCOMPAT bit and restores the behavior before .NET 2.0 SP1. If your assembly was signed it also invalidates the signature so you'll have to resign it:

sn.exe -R <filename.exe> <keyfile.snk>

You can automate this by putting the two commands in the Post-build event command line for the project:

editbin.exe /NXCOMPAT:NO "$(TargetPath)"
sn.exe -R "$(TargetPath)" "$(ProjectDir)<keyfile.snk>"

You might need to call vcvars32.bat before that to put the required executables in path and enable editbin.exe dependencies to be resolved. Note that in a completely automated build scenario using MSBuild you'll have to specify full path for the vcvars32.bat because $(DevEnvDir) resolves to *Undefined* outside Visual Studio 2005. Also your strong name key should be in a snk file instead of a password protected pfx file because sn.exe doesn't allow the password to be read from a redirected standard input.

4/29/2008 2:29:08 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [1]

Monday, April 14, 2008

MonthCalendar BoldedDates in Windows Vista (Development | .NET | Vista)

The MonthCalendar control's BoldedDates functionality doesn't appear to work properly on Windows Vista. The dates added to any of the BoldedDates, MonthlyBoldedDates and AnnuallyBoldedDates collections are rendered just the same as those not added to any of the collections. The same code works just fine on Windows XP and causes those dates to be rendered bold. The only workaround i've managed to find is disabling visual styles in the application, i.e. commenting out the first line in the Program.Main() method of a new Windows Application:

Application.EnableVisualStyles();

4/14/2008 8:54:58 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [0]

Monday, December 11, 2006

More Sidebar Gadgets available (Development | Vista | Personal | Software)

I you haven't checked out our Vista site recently, now is the right time to do it. Since my last posting two new gadgets have been released:

  • Slovenian Portfolio provides information from Ljubljana Stock Exchange and gives you the ability to track your personal portfolio.
  • Slovenian Rates provides information on exchange rates from 4 Slovenian banks: Bank of Slovenia, NLB, SKB and NKBM.
An update for Slovenian Radio has also been released in the meantime. Apart from bug fixes the list of radio stations has been extended.
 
Try them out if you're using Windows Vista and haven't done so already.

12/11/2006 12:58:18 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [0]

Friday, December 01, 2006

Our first Sidebar Gadget released (Development | Vista | Personal | Software)

We have just released our first Sidebar Gadget – Slovenian Radio.

The gadget features a centralized list of radio stations (retrieved from our server) and basic controls for selecting the station and adjusting the volume. Everything is packaged in a nice and simple GUI.

Try it out. It really is the most convenient way of listening to Slovenian radio stations online. You can download it for free here. Spread the word if you like it.

12/1/2006 8:23:14 AM (Central Europe Standard Time, UTC+01:00)  #  Comments [0]

Sunday, October 29, 2006

Running Windows Sidebar gadgets directly in IE7 (Development | Vista | Web)

Since gadgets are HTML applications and the Windows Sidebar uses Internet Explorer 7 to render them, I prefer running their code directly in IE7 during development. It makes debugging easier and allows me to develop them in Windows XP on my primary development machine with all the tools properly setup.

While this works great in early development stages, it becomes a problem later on when you start using the Windows Sidebar object model (to implement settings, visibility changes handlers etc.). This specific code understandably can only be debugged while running in Windows Sidebar but the unfortunate side effect is that the calls to objects which don’t exist in IE7 break the scripts and effectively prevent running the would-be gadget in IE7 even without this functionality.

The following function can help you keep the gadget running directly in IE7 even after the gadget specific functionalities are already implemented:

function isGadget()
{
    try
    {
        var dummy = System;
        return true;
    }
    catch (err)
    {
        return false;
    }
}

Just check if the application is running in Windows Sidebar at gadget startup and make sure to use the object model only when this is true. You can also add alternative initialization for running directly in IE7 to provide default values for settings and any other gadget specific variables. By doing this you can still test new features directly in IE7 if they are not gadget specific.

10/29/2006 2:11:59 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [0]

Sunday, October 22, 2006

A sample Windows Sidebar gadget (Development | Vista | Web | Downloads | Sources | Windows) HelloWorld.zip (3.99 KB)

The whitepaper sample code doesn’t exactly follow the guidelines for web development therefore I wrote my own sample with clearly separated HTML structure, CSS styles and JavaScript code. I decided to make it publicly available in case anyone else finds it useful. A few things are worth mentioning.

  • Empty XHTML tags. For some reason the XHTML style empty tags don’t work as expected when they are filled from JavaScript code. It seems as if the engine would parse them as opened tags without the corresponding closing tag. Therefore you should always use the long form <span id=”mySpan”></span> instead of the short one <span id=”mySpan” />. This problem seems to be specific for gadgets. The same code runs just fine in IE7.
  • Localization. I haven’t managed to get the localization to work at all. I suppose the engine uses the files from the locale directory corresponding to the current display language not the locale settings. This does make sense but with only English language available in Vista there’s no way to check that the current file organization in my sample gadget is really ok. I kept it in the sample since I’m pretty convinced that it is.
  • Deployment. If you double click a file with a .gadget extension in Vista, you will automatically start the gadget installation process. The file needs to be a zip or cab archive containing the actual gadget directory structure. Vista behaves as if an application setup was started. This means that you should sign the file to increase the end user trust. You can use the Sign Tool to do that but in this case you need to make a cab file since zip files can’t be signed with it. I tried to make the cab file using a CAB Project template in Visual Studio 2005 but I couldn’t make the correct directory structure. I ended up using Cabarc. After all, I only had to run the following line in the gadget directory to build the cab file correctly.
    cabarc –p –r N ..\HelloWorld.gadget *

10/22/2006 1:14:03 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [0]

Writing your first Windows Sidebar gadget (Development | Vista | Web)

This whitepaper should definitely be your first source of information when starting with gadget development. Unfortunately I didn’t find it as helpful as it could be. Find below a few issues that could be covered better since I’ve been struggling with them for some time before I get everything to work as expected.

  • The style definition for body in both pages is missing the unit – px. Without it the definition is invalid and the gadget shows up with minimum size. The correct style definition would be in the likes of:
    body
    {
       width: 130px;
       height: 50px;
    }
  • The settingsUI property is a member of System.Gadget not System.Gadget.Settings. This time the error is in the reference not the whitepaper.
  • If you’re only replacing the files in the C:\Users\<username>\AppData\Local\Microsoft\Windows Sidebar\Gadgets directory to update the gadget you might have some problems with caching even if you close all gadget instances as the whitepaper suggests. In my case the old image files and gadget.xml file were still used although they have been replaced. Restarting Windows Sidebar should help.
  • Don’t forget that gadget settings are stored with a particular gadget instance. This makes it possible to display two instances of the gadget with different settings. The side effect is that they get lost every time you remove the gadget from the sidebar and put it back on. It works like that by design and if you need persistent common settings for all instances you’ll have to use a different solution.

10/22/2006 12:48:31 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [0]

Resources for Windows Sidebar gadget development (Development | Vista | Web)

I though I’d gather in one place all the useful links I found with information on development of Windows Sidebar gadgets.

  • Gadget Development Overview whitepaper should be you first stop. It’s a short article explaining the basics of gadget development.
  • Windows Sidebar Reference is the only documentation that is currently available. It’s not very detailed, neither always accurate but it should still serve you well.
  • Gadget Corner is a blog maintained by the Windows Sidebar team. It contains a few articles with information that can’t be found anywhere else.
  • Sidebar Gadgets Forum is the only forum dedicated to gadget development. On top of that the Windows Sidebar team members have the habit of answering the posted questions.
Last but not least, don't underestimate the gadgets that come with Windows Vista. Since it’s all HTML, CSS and JavaScript, all the source code is available and can be a good learning material when trying to determine how some functionality has been achieved. You can find them in C:\Program Files\Windows Sidebar\Gadgets directory.
 
[Updated on: 29.10.2006, Sidebar Gadgets Forum link added]

10/22/2006 12:40:45 PM (Central Europe Standard Time, UTC+01:00)  #  Comments [0]

Blog Feeds
RSS 2.0 RSS 2.0
Atom 1.0 ATOM 1.0
Fellow Bloggers
 Andrej Tozon
 Dejan Sarka
 Dusan Zupancic
 Matevz Gacnik
 Miha Markic
Disclaimer
The content of this site are my own personal opinions and do not represent my employer's view in anyway. In addition, my thoughts and opinions often change, and as a weblog is intended to provide a semi-permanent point in time snapshot you should not consider out of date posts to reflect my current thoughts and opinions.

Powered by:
newtelligence dasBlog 1.8.5223.2

© 2008 Damir Arh, M. Sc. Send mail to the author(s)

Microsoft Certified Professional
Currently Reading
Currently Playing
Currently Watching