com4j.ComException错误:80004005.\ invoke.cpp:51,同时打开了窗口和选定的项目 [英] Error com4j.ComException: 80004005 .\invoke.cpp:51 while getting open windows and selected items

查看:252
本文介绍了com4j.ComException错误:80004005.\ invoke.cpp:51,同时打开了窗口和选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Windows资源管理器中获取所有选定的文件和文件夹.我正在使用com4j访问Win Shell32 API (由于Tom91136,请参考 this 您需要学习如何安装和初始化com4j).

I need to get all selected files and folders in Windows Explorer. I'm using com4j to access win Shell32 API (thanks to Tom91136, refer this if you need to learn how can you install and initialize com4j).

此代码类获取Windows资源管理器中的选定文件或文件夹并进行打印.

This code class gets the selected files or folders in Windows Explorer and prints.

import java.io.File;
import com4j.*;
import test.wsh.*;
import java.util.*;
import java.util.Timer;
import javax.swing.*;


public class DetectSelection {

    public static void main(String[] argv)
    {
            list(); //there is a timer actually, calls every second
    }

    public static void list(){
        System.out.println("SELECTION DETECT:");

        String newResults="";

        try
        {
            List<IWebBrowser2> browsers = getIWebBrowser2();
            for(IWebBrowser2 browser : browsers){
                IShellFolderViewDual3 view = getIShellFolderViewDual3(browser);
                if (view != null && browser.visible()) {

                    FolderItems items = view.selectedItems();

                    for (Com4jObject object : items) {
                        FolderItem item = object.queryInterface(FolderItem.class);
                        if (item != null) {
                            newResults+=item.path()+" - "+item.type()+"\n\n";
                        }
                    }
                }
            }
        }
        catch(Exception error)
        {
            System.out.println("Error in list: "+error.toString());
        }

        System.out.println(newResults);
    }

    public static List<IWebBrowser2> getIWebBrowser2() {
        // TODO this can be potentially optimized
        try
        {
            List<IWebBrowser2> rWindows=new ArrayList<IWebBrowser2>();

            IShellWindows windows = ClassFactory.createShellWindows()
                    .queryInterface(IShellWindows.class);
            for (Com4jObject window : windows) {

                IWebBrowser2 browser = window.queryInterface(IWebBrowser2.class);
                    rWindows.add(browser);
            }
            return rWindows;
        }
        catch(Exception error)
        {
            System.out.println("Error in getIWebBrowser2: "+error.toString());
            return null;
        }
    }

    public static IShellFolderViewDual3 getIShellFolderViewDual3(IWebBrowser2 browser) {
        if (browser == null)
            return null;

        try
        {
            return browser.document().queryInterface(IShellFolderViewDual3.class);
        }
        catch(Exception error)
        {
            System.out.println("Error in getIShellFolderViewDual3: "+error.toString());
            return null;
        }
    }
}

我只张贴了重要部分.我正在使用计时器来定期检查打开的文件.每秒调用list()方法.它工作正常,但是如果关闭窗口,则会出现以下错误.

I posted only essential parts. I am using a timer to check open files periodically. Calls list() method every second. It works fine but I am getting the error below if I close a Window.

    com4j.ComException: 80004005 
 .\invoke.cpp:517
        at com4j.Wrapper.invoke(Wrapper.java:166)
        at com.sun.proxy.$Proxy10.document(Unknown Source)
        at DetectSelection.getIShellFolderViewDual3(DetectSelection.java:79)
        at DetectSelection.list(DetectSelection.java:32)
    Caused by: com4j.ComException: 80004005 Belirtilmemiş hata : Belirtilmemiş hata : .\invoke.cpp:517
        at com4j.Native.invoke(Native Method)
        at com4j.StandardComMethod.invoke(StandardComMethod.java:35)
        at com4j.Wrapper$InvocationThunk.call(Wrapper.java:340)
        at com4j.Task.invoke(Task.java:51)
        at com4j.ComThread.run0(ComThread.java:153)
        at com4j.ComThread.run(ComThread.java:134)

我开始使用try-catch,我明白了:

I started to use try-catch and I got this:

Error in getIShellFolderViewDual3: com4j.ComException: 80004005  .\invoke.cpp:517

"IShellFolderViewDual3"方法出了点问题.

Something wrong with "IShellFolderViewDual3" method.

推荐答案

我猜您的问题与计时器有关.如果有一个计时器并且list()方法每秒运行一次,那么当您关闭窗口时,getIShellFolderViewDual3()方法也会尝试同时访问关闭的窗口.

I guess your problem is about timer. If there is a timer and the list() method is running every second then when you close a window, getIShellFolderViewDual3() method also tries to access a closing window, at the same time.

使用之前,请检查浏览器"对象是否可见并且不为null.

Check if "browser" object is visible and not null before use it.

try
        {
            List<IWebBrowser2> browsers = getIWebBrowser2();
            for(IWebBrowser2 browser : browsers){
                if(browser.visible())
                {

                    IShellFolderViewDual3 view = getIShellFolderViewDual3(browser);
                    if (view != null && browser.visible()) {

                        FolderItems items = view.selectedItems();

                        for (Com4jObject object : items) {
                            FolderItem item = object.queryInterface(FolderItem.class);
                            if (item != null) {
                                newResults+=item.path()+" - "+item.type()+"\n\n";
                            }
                        }
                    }
                }
            }
        }

这篇关于com4j.ComException错误:80004005.\ invoke.cpp:51,同时打开了窗口和选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆