views:

457

answers:

2

Which is the preferred development language for Windows 7 gadgets?

I know that a gadget uses Xml, Html, CSS and Script(Java/VB) but I need some advanced features such as:

  1. Writing/Reading a file
  2. Getting list of running processes
  3. Sending keys to an active application

For the above tasks, I will need to use the Windows API or, if possible, .NET. Is it possible to have the above features in a Gadget?

A: 

Check out this article on MSDN. Might shed some insight.

Chris Thompson
+3  A: 

There are various COM objects available to JScript using the ActiveXObject class. You can write your own interop assembly using .NET but this introduces several problems:

  • Each public class within the assembly needs be registered on the host machine before use. There are problems posed when writing to and creating keys in x64, which the WScript.Shell object struggles with for certain parts of the registry. WMI works well for registering, however.
  • From instantiation, the assembly dll is locked and cannot be deleted even if the gadget is closed. This is mostly an issue when uninstallation/software updates come into play. Once the sidebar.exe process is ended, the file becomes unlocked.
  • Unregistering seems to be problematic, even with WMI. Timing the unregistration is often an issue (do you do it on unload or after instantiation? What if there are multiple gadgets open using it?). Also, not unregistering will leave unwanted registry keys on the user's machine if he/she decides not to keep your gadget.
  • If the gadget relies 100% on the assembly and, for whatever reason, the assembly cannot be registered on the user's machine then the entire experience of the gadget is unavailable to that user. Be prepared for some low rated reviews on Windows Live Gallery.

If you decide you really need to write an assembly. I would advise that if you are writing your own assembly for a gadget that it is an extension to the basic functionality of your gadget and that you ensure the gadget has features available should the user be unable to use the assembly.

My company has an as-of-yet unreleased solution to the above issues which we are planning on making available to all Windows Desktop Gadget developers in the near future (ie after more testing).

As for the advanced features you specified, these can all be done using some built-in Windows COM objects, which are already registered and aren't distributed with your gadget so they don't suffer the same issues noted above. As an answer to your specific requirements, examples of these are:

FileSystemObject
Writing/Reading files can be done using FileSystemObject. A basic example of FileSystemObject for reading files is:

var ForReading = 1, ForWriting = 2, ForAppending = 8;
var oFSO  = new ActiveXObject("Scripting.FileSystemObject");
var oFile = oFSO.OpenTextFile(System.Gadget.path+"\\test.txt", ForReading, true);
var sText = oFile.ReadAll();
window.prompt("", sText);

Windows Management Instrumentation (WMI)
WMI has an enourmous range of purposes. Certain parts of it will require administrative rights, though (which must be applied to sidebar.exe). One of the classes within WMI is Win32_Process which can be used to iterate through running processes. Note that it's much more difficult to use WMI in JScript than it is in VBScript and most examples you find on the internet are for VBScript (which makes porting the code a pain).

Windows Script Host Shell The WshShell Object provides another great extension to the limitations of Windows Desktop Gadgets, including the SendKeys method. Although the method cannot be used to send a key to an application specifically, it's possible to activate the application with the AppActivate method and then use SendKeys to emulate keystrokes in the activated application.

I hope that helps :-)

Andy E
Thank you very much Andy!! That was the kind of details I was looking for. One more question here: I need to show around 40 buttons on my Gadget. Do you recommend creating a HTML UI in Gadget itself or open another Forms application to show the UI because Gadgets seems to have very little area to for UI.
A9S6
Glad it helped you out :-) Gadgets do not have a maximum size (that I'm aware of) but on Windows Vista gadgets start in the sidebar unless they are dragged to a different part of the screen. I've noticed your questions have been mostly about gadgets for Windows 7, for which there is no sidebar so you're not really limited to a specific size, except that your gadget must meet the minimum height which is 60px. The flyout can also be used to display more information when necessary.
Andy E