从 .NET Windows 服务调用 Shell32.dll

3

本文介绍了从 .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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14