在 COM 对象上使用早期绑定

发布于 2024-07-17 07:30:34 字数 280 浏览 13 评论 0原文

我有这段代码运行得很好,并为我提供了用户开始菜单的路径:

    Dim oShell As Object = CreateObject("Shell.Application")
    MsgBox(oShell.NameSpace(11).Self.Path)

这显然使用了后期绑定。 现在假设我想在 C# 或 VB.NET 严格模式中执行此操作,这两种模式都不支持这种带有后期绑定的语法。

这可能吗? 如何?

谢谢你的帮助!

I have this piece of code that works very well and gives me the path the user's start menu:

    Dim oShell As Object = CreateObject("Shell.Application")
    MsgBox(oShell.NameSpace(11).Self.Path)

This obviously uses late binding. Now say I want to do this in C#, or in VB.NET strict mode, neither of which support this kind of syntax with late binding.

Is this possible? How?

Thanks for you help!

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

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

发布评论

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

评论(4

绾颜 2024-07-24 07:30:34

如果你想用 COM 方式解决这个问题,你必须弄清楚在 VB 项目中添加哪个 COM 引用。

打开regedit并导航到HKEY_CLASSES_ROOT\\CLSID,即

HKEY_CLASSES_ROOT\Shell.Application\CLSID

您将找到唯一标识COM组件的类id。

HKEY_CLASSES_ROOT\CLSID 下,您现在可以查找 COM 组件后面的文件:

HKEY_CLASSES_ROOT\CLSID\{13709620-C279-11CE-A49E-444553540000}\InProcServer32

显示以下值:

%SystemRoot%\system32\SHELL32.dll

现在转到 Visual Studio,并添加对此文件的引用(在浏览< 添加引用对话框的 /em> 选项卡)。 如果打开项目属性,您实际上会看到添加的 COM 组件的好听名称是 Microsoft Shell Controls and Automation

添加引用后,您可以使用 Shell.Application 对象,如下所示:

Option Strict On

Module PrintStartMenuLocation

    Sub Main()
        Dim shell As New Shell32.Shell
        Dim folder As Shell32.Folder
        Dim folderItem As Shell32.FolderItem
        Dim startMenuPath As String

        folder = shell.NameSpace(Shell32.ShellSpecialFolderConstants.ssfSTARTMENU)
        folderItem = CType(folder.Items(0), Shell32.FolderItem)
        startMenuPath = folderItem.Path

        Console.WriteLine(startMenuPath)
    End Sub

End Module

C# 中的版本如下所示:

class Program
{
    static void Main(string[] args)
    {
        Shell32.Shell shell = new Shell32.Shell();
        Shell32.Folder folder = shell.NameSpace(Shell32.ShellSpecialFolderConstants.ssfSTARTMENU);
        Shell32.FolderItem folderItem = folder.Items().Item(0) as Shell32.FolderItem;
        string startMenuPath = folderItem.Path;

        Console.WriteLine(startMenuPath);
    }
}

但是,如果您只需要检索“开始”菜单文件夹的位置,您可以执行以下操作:直接在.NET中使用相同

Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)

If you want to solve this the COM way you have to figure out, which COM reference to add in your VB project.

Open regedit and navigate to HKEY_CLASSES_ROOT\<class id>\CLSID, i.e.

HKEY_CLASSES_ROOT\Shell.Application\CLSID

and you will find the class id which uniquely identifies the COM component.

Under HKEY_CLASSES_ROOT\CLSID you can now look up which file is behind the COM component:

HKEY_CLASSES_ROOT\CLSID\{13709620-C279-11CE-A49E-444553540000}\InProcServer32

shows the following value:

%SystemRoot%\system32\SHELL32.dll

Now go to Visual Studio, and add a reference to this file (on the Browse tab of the Add References dialog). If you open up the projects properties, you will actually see that the nice name of the COM component added is Microsoft Shell Controls and Automation.

Once the reference is added you can use the Shell.Application object as follows:

Option Strict On

Module PrintStartMenuLocation

    Sub Main()
        Dim shell As New Shell32.Shell
        Dim folder As Shell32.Folder
        Dim folderItem As Shell32.FolderItem
        Dim startMenuPath As String

        folder = shell.NameSpace(Shell32.ShellSpecialFolderConstants.ssfSTARTMENU)
        folderItem = CType(folder.Items(0), Shell32.FolderItem)
        startMenuPath = folderItem.Path

        Console.WriteLine(startMenuPath)
    End Sub

End Module

A version in C# would look as follows:

class Program
{
    static void Main(string[] args)
    {
        Shell32.Shell shell = new Shell32.Shell();
        Shell32.Folder folder = shell.NameSpace(Shell32.ShellSpecialFolderConstants.ssfSTARTMENU);
        Shell32.FolderItem folderItem = folder.Items().Item(0) as Shell32.FolderItem;
        string startMenuPath = folderItem.Path;

        Console.WriteLine(startMenuPath);
    }
}

However, if you simply need to retrieve the location of the Start Menu folder you can do the same directly in .NET using

Dim path As String = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)
已下线请稍等 2024-07-24 07:30:34

实际上你可以使用反射:

Type shellType = Type.GetTypeFromProgID("Shell.Application", true);
object shell = Activator.CreateInstance(shellType);
object folder = shellType.InvokeMember("NameSpace", BindingFlags.InvokeMethod, null, shell, new object[] { 11 });
object self = folder.GetType().InvokeMember("Self", BindingFlags.GetProperty, null, folder, new object[] { });
object path = self.GetType().InvokeMember("Path", BindingFlags.GetProperty, null, self, new object[] { });
Console.WriteLine(path);

不是我喜欢的代码,但在 C# 4.0 中你可以使用 动态类型 来清理这个混乱。

Well actually you could use reflection:

Type shellType = Type.GetTypeFromProgID("Shell.Application", true);
object shell = Activator.CreateInstance(shellType);
object folder = shellType.InvokeMember("NameSpace", BindingFlags.InvokeMethod, null, shell, new object[] { 11 });
object self = folder.GetType().InvokeMember("Self", BindingFlags.GetProperty, null, folder, new object[] { });
object path = self.GetType().InvokeMember("Path", BindingFlags.GetProperty, null, self, new object[] { });
Console.WriteLine(path);

Not the kind of code I like, but in C# 4.0 you could use the dynamic type to clean up this mess.

鲜血染红嫁衣 2024-07-24 07:30:34
Dim DirPath As String = _
    System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)

请参阅此处了解更多信息。

Dim DirPath As String = _
    System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu)

See here for more.

与往事干杯 2024-07-24 07:30:34

如果我没记错的话,您所要做的就是将对象引用转换为适当的接口。 如果您在 .NET 中使用 COM 对象,通常会导入类型库,然后就可以使用可用的接口。

If I remember correctly, all you have to do is to cast the object reference into the appropriate interface. If you use a COM object in .NET, you typically import the type library and then have the interfaces readily available.

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