Calling Shell32.dll from .NET Windows Service(从 .NET Windows 服务调用 Shell32.dll)
问题描述
我有一个 .NET 4.0 库,它使用 Shell32 和 Folder.GetDetailsOf() 从 WTV 文件中获取元数据.我已经成功地将它与控制台和 Windows 窗体应用程序一起使用,没有问题.但由于某种原因,当从 .NET 4.0 Windows 服务调用组件时,调用启动 Shell 类会导致 COM 错误.
I have a .NET 4.0 library that uses Shell32 and Folder.GetDetailsOf() to get metadata from WTV files. I have used it successfully with Console and Windows Forms apps without issue. But for some reason, when calling the component from a .NET 4.0 Windows Service, the call to initiate the Shell class causes a COM error.
库内失败的代码:
Shell32.Shell shell = new Shell();
Shell32.Shell shell = new Shell();
错误:
无法将System.__ComObject"类型的 COM 对象转换为接口类型Shell32.Shell".此操作失败,因为 IID 为{286E6F1B-7113-4355-9562-96B7E9D64C54}"的接口的 COM 组件上的 QueryInterface 调用失败,原因是以下错误:不支持此类接口(HRESULT 异常:0x80004002 (E_NOINTERFACE)).
Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{286E6F1B-7113-4355-9562-96B7E9D64C54}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
我阅读了我的 Apartment Threading、COM Interops、Dynamic、PIA's 等,等等 :) 但我找到的解决方案组合都没有解决这个问题.它必须是来自另一个看不到互操作的线程的调用.请帮忙:)
I read my fill of Apartment Threading, COM Interops, Dynamic, PIA's, etc, etc, etc :) But no combination of solutions I've found has solved the problem. It must be a calling from another thread that can't see the Interop. Help, please :)
推荐答案
我最近在使用命令行应用程序(控制台)时遇到了同样的问题.事实证明,需要使用 [STAThread]
属性来注释程序的 Main()
方法.还需要注意的是,如果入口点使用 [MTAThread]
进行注释,它会以完全相同的方式失败.希望对你有帮助.
I had the same problem just recently with a command line application (console). Turns out, it was required to annotate the program's Main()
method with the [STAThread]
attribute. It has also been noted that it fails miserably in the exact same way if the entry point is annotated with [MTAThread]
instead. I hope it helps.
这篇关于从 .NET Windows 服务调用 Shell32.dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 .NET Windows 服务调用 Shell32.dll
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01