簡體   English   中英

預覽處理程序僅在從資源管理器調用應用程序時有效

[英]Preview handler only works when application is invoked from Explorer

我做了一個簡單的應用程序,它創建一個窗口,然后使用預覽處理程序顯示作為參數傳遞給應用程序的文件的預覽,請參見下面的代碼。

當只需將文件拖放到 .exe 文件上進行預覽時(或者只是硬編碼文件路徑,然后直接從資源管理器啟動 .exe),這工作得非常好,但在調用時幾乎所有預覽處理程序都失敗了它來自終端(powershell、cmd 等),例如./main.exe testfile.docx 嘗試從其他進程調用它時也會失敗。

我描述的問題發生在以下預覽處理程序中(除其他外):

  • 用於 Excel 文件的 MS Office 預覽處理程序,CLSID {00020827-0000-0000-C000-000000000046}
  • Word 文件的 MS Office 預覽處理程序,CLSID {84F66100-FF7C-4fb4-B0C0-02CD7FB668FE}
  • pdf 文件的邊緣預覽處理程序,CLSID {3A84F9C2-6164-485C-A7D9-4B27F8AC009E}
  • PowerPoint 文件的 MS Office 預覽處理程序,CLSID {65235197-874B-4A07-BDC5-E65EA825B718} 對於 power point 預覽處理程序,對IInitializeWithFile接口的初始化調用失敗hresult 0x86420003

它適用於以下預覽處理程序:

  • Windows 為文本文件提供了預覽處理程序,CLSID {1531d583-8375-4d3f-b5fb-d23bbd169f22}
  • Windows 為 html 文件提供了預覽處理程序,CLSID {f8b8412b-dea3-4130-b36c-5e8be73106ac}

當我說它“失敗”時,我的意思是它只是不顯示預覽,沒有一個函數調用失敗。 見圖片:

將 test.xlsx excel 文件拖到 .exe 文件上,當硬編碼路徑到 test.xlsx 並剛剛從資源管理器啟動應用程序時,結果相同: 在此處輸入圖像描述

test.xlsx 的路徑作為命令行參數傳遞(powershell 中的./main.exe ./test.xlsx ): 在此處輸入圖像描述

我不確定是什么導致了這個,或者從哪里開始尋找或搜索什么,所以對此的任何輸入將不勝感激。

注意:幾天前我問了一個類似的問題,但這是一個單獨的問題。

使用cl /std:c++20 /EHsc main.cpp編譯,期望文件的路徑作為第一個命令行參數進行預覽。 由於我使用了一點winrt,這需要Windows 10。

#pragma comment(lib, "Shlwapi.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "WindowsApp.lib")
#pragma comment(lib, "Ole32.lib")

#include <iostream>
#include <string>
#include <string_view>
#include <array>
#include <filesystem>

#include <Windows.h>
#include <shlwapi.h>
#include <winrt/base.h>
#include <ShObjIdl.h>


class Preview{
    winrt::com_ptr<IPreviewHandler> mPreviewHandler;
    bool mIsActive = false;
    bool mIsInitialized = false;
    std::filesystem::path mFilePath;
    HWND mHwnd;
    RECT mRect;
public:
    Preview(const std::filesystem::path& filePath, const HWND hwnd, const RECT& rect):
        mFilePath(filePath), mHwnd(hwnd), mRect(rect){
        createPreviewHandler();
        initialize();
    };
    void setRect(const RECT& rect){
        mPreviewHandler->SetRect(&rect);
        mRect = rect;
    }
    void setWindow(const HWND hwnd, const RECT& rect){
        mPreviewHandler->SetWindow(hwnd, &rect);
        mHwnd = hwnd;
        setRect(rect);
    }
    void startPreview(){
        if(!mIsActive){
            if(!mIsInitialized){
                initialize();
            }
            mPreviewHandler->DoPreview();
            setRect(mRect);
            mIsActive = true;
        }
    }
    void stopPreview(){
        if(mIsActive){
            mPreviewHandler->Unload();
            mIsActive = false;
            mIsInitialized = false;
        }
    }

private:
    void createPreviewHandler(){
        CLSID previewHandlerId = getShellExClsidForType(mFilePath.extension());
        mPreviewHandler.capture(CoCreateInstance, previewHandlerId, nullptr, CLSCTX_LOCAL_SERVER);
    }
    CLSID getShellExClsidForType(const std::wstring& extension){
        winrt::hresult res;
        DWORD size;
        std::array<wchar_t, 39> interfaceIdWstr;
        size = StringFromGUID2(IID_IPreviewHandler, interfaceIdWstr.data(), interfaceIdWstr.size());
        if(!size){
            winrt::throw_hresult(HRESULT_FROM_WIN32(GetLastError()));
        }

        std::array<wchar_t, 39> exIdWstr;
        res = AssocQueryStringW(ASSOCF_INIT_DEFAULTTOSTAR, 
                                ASSOCSTR_SHELLEXTENSION, 
                                extension.c_str(), 
                                interfaceIdWstr.data(), 
                                exIdWstr.data(), 
                                &size);
        winrt::check_hresult(res);

        CLSID exId;
        res = IIDFromString(exIdWstr.data(), &exId);
        winrt::check_hresult(res);

        return(exId);
    }
    void initialize(){
        initializeFromPath(mFilePath);
        setWindow(mHwnd, mRect);
        mIsInitialized = true;
    }
    void initializeFromPath(const std::filesystem::path& filePath){
        if(!initWithFile(filePath) && !initWithStream(filePath)){
            winrt::throw_hresult(E_NOINTERFACE);
        }
    }
    bool initWithFile(const std::filesystem::path& filePath){
        if(!mPreviewHandler.try_as<IInitializeWithFile>()){
            return(false);
        }
        winrt::check_hresult(mPreviewHandler.as<IInitializeWithFile>()->Initialize(filePath.c_str(), STGM_READ));
        return(true);
    }
    bool initWithStream(const std::filesystem::path& filePath){
        if(!mPreviewHandler.try_as<IInitializeWithStream>()){
            return(false);
        }
        winrt::com_ptr<IStream> stream;
        stream.capture([](LPCWSTR pszFile, DWORD grfMode, REFIID riid, void **ppstm)
                       {return(SHCreateStreamOnFileEx(pszFile, grfMode, 0, false, nullptr, reinterpret_cast<IStream**>(ppstm)));},
                       filePath.c_str(), STGM_READ | STGM_SHARE_DENY_WRITE | STGM_FAILIFTHERE);
        winrt::check_hresult(mPreviewHandler.as<IInitializeWithStream>()->Initialize(stream.get(), STGM_READ));
        return(true);
    }
};

HMODULE getCurrentModule(){
    HMODULE hModule;
    GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
                      (LPCTSTR)getCurrentModule,
                      &hModule);
    return(hModule);
}

LRESULT windowProc(HWND hwnd,
                   UINT msg,
                   WPARAM wParam,
                   LPARAM lParam){
    LPARAM ret = 0;
    switch(msg){
        case WM_CLOSE:
        {
            PostQuitMessage(0);
            ret = 0;
        }break;
        default:
        {
            ret = DefWindowProc(hwnd, msg, wParam, lParam);
        }break;
    }
    return(ret);
}

HWND createTestWindow(){
    WNDCLASSW wndClass;
    wndClass.style = 0;
    wndClass.lpfnWndProc = windowProc;
    wndClass.cbClsExtra = 0;
    wndClass.cbWndExtra = 0;
    wndClass.hInstance = getCurrentModule();
    wndClass.hIcon = nullptr;
    wndClass.hCursor = nullptr;
    wndClass.hbrBackground = nullptr;
    wndClass.lpszMenuName = nullptr;
    wndClass.lpszClassName = L"test";

    ATOM wndAtom = RegisterClassW(&wndClass);
    if(!wndAtom){
        winrt::throw_hresult(HRESULT_FROM_WIN32(GetLastError()));
    }
    HWND window = CreateWindowExW(0, 
                                  L"Test", 
                                  L"", 
                                  WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, 
                                  CW_USEDEFAULT, 
                                  CW_USEDEFAULT, 
                                  CW_USEDEFAULT, 
                                  CW_USEDEFAULT, 
                                  0, 
                                  0,
                                  wndClass.hInstance, 
                                  nullptr);
    if(!window){
        winrt::throw_hresult(HRESULT_FROM_WIN32(GetLastError()));
    }

    ShowWindow(window, SW_NORMAL);

    return(window);
}

int main(int argc, char *argv[]){
    try{
        winrt::check_hresult(CoInitialize(nullptr));
        if(!SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE)){
            winrt::throw_hresult(E_FAIL);
        }

        if(argc != 2){
            return(1);
        }

        std::filesystem::path filePath(argv[1]);
        HWND window = createTestWindow();

        RECT rect;
        GetClientRect(window, &rect);
        Preview preview(filePath, window, rect);
        preview.startPreview();

        MSG msg;
        while(GetMessageW(&msg, nullptr, 0, 0) != 0){
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }catch(winrt::hresult_error err){
        std::wcout << "0x" << std::hex << err.code() << " " << static_cast<std::wstring_view>(err.message());
    }catch(...){}
}

argv[1]上調用GetFullPathName shell 函數無法將相對路徑解析為 pidl。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM