Windows 7 小工具 - 用于开发的编程语言

发布于 2024-08-11 07:33:30 字数 235 浏览 5 评论 0原文

Windows 7 小工具的首选开发语言是哪一种?

我知道小工具使用 Xml、Html、CSS 和脚本(Java/VB),但我需要一些高级功能,例如:

  1. 写入/读取文件
  2. 获取正在运行的进程列表
  3. 将密钥发送到活动应用程序

对于上述任务,我将需要使用 Windows API,或者如果可能的话,使用 .NET。是否可以在 Gadget 中具备上述功能?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2024-08-18 07:33:31

使用 ActiveXObject 类,JScript 可以使用各种 COM 对象。您可以使用 .NET 编写自己的互操作程序集,但这会带来几个问题:

  • 程序集中的每个公共类在使用前都需要在主机上注册。在 x64 中写入和创建密钥时会出现问题,WScript.Shell 对象在注册表的某些部分会遇到这些问题。不过,WMI 非常适合注册。
  • 从实例化开始,程序集 dll 就被锁定,即使小工具关闭也无法删除。当卸载/软件更新发挥作用时,这主要是一个问题。一旦 sidebar.exe 进程结束,该文件就会解锁。
  • 即使使用 WMI,注销似乎也有问题。取消注册的时间通常是一个问题(您是在卸载时执行还是在实例化之后执行?如果有多个使用它打开的小工具怎么办?)。此外,如果用户决定不保留您的设备,则不取消注册会在用户的计算机上留下不需要的注册表项。
  • 如果小工具 100% 依赖于程序集,并且无论出于何种原因,程序集无法在用户的计算机上注册,则该用户将无法获得小工具的整个体验。请准备好接受 Windows Live Gallery 上的一些低评价评论。

如果您决定确实需要编写程序集。我建议,如果您正在为小工具编写自己的程序集,那么它是小工具基本功能的扩展,并且您确保小工具在用户无法使用该程序集时具有可用的功能。

我的公司针对上述问题有一个尚未发布的解决方案,我们计划在不久的将来(即经过更多测试后)向所有 Windows 桌面小工具开发人员提供该解决方案。

至于您指定的高级功能,这些都可以使用一些内置的 Windows COM 对象来完成,这些对象已经注册并且不随您的小工具一起分发,因此它们不会遇到上述相同的问题。作为对您的具体要求的回答,这些示例包括:

FileSystemObject
可以使用 FileSystemObject 写入/读取文件。用于读取文件的 FileSystemObject 的基本示例是:

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 具有广泛的用途。不过,它的某些部分需要管理权限(必须应用于 sidebar.exe)。 WMI 中的类之一是 Win32_Process可用于迭代正在运行的进程。请注意,在 JScript 中使用 WMI 比在 VBScript 中使用 WMI 困难得多,并且您在 Internet 上找到的大多数示例都是针对 VBScript 的(这使得移植代码变得很痛苦)。

Windows 脚本宿主 Shell
WshShell 对象 为Windows 桌面小工具的限制,包括 SendKeys方法。尽管该方法不能用于专门向应用程序发送密钥,但可以使用 AppActivate 方法,然后使用 SendKeys 模拟激活的应用程序中的击键。

我希望这有帮助:-)

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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文