WinUI3: How to print framework element using cpp

Jens Tirsvad Nielsen 0 Reputation points
2024-12-24T23:57:15.6233333+00:00

My app have to windows. Depending on which the print is called from we getting the actual hWnd.
My issue is how to to get an instance of printManager or is there another way to print from winui 3?

// file: MainWindow.xaml.cpp
...
#include <Printmanagerinterop.h>
...
// Manually define CLSID_PrintManager
DEFINE_GUID(CLSID_PrintManager, 0x92788047, 0x3b5e, 0x41c7, 0x86, 0x2e, 0x4c, 0xd3, 0x0d, 0x45, 0xa3, 0x10);
...    

	void MainWindow::printFrameworkElement(FrameworkElement const& element)
    {
        using namespace winrt::Windows::Graphics::Printing;
        using namespace Windows::Graphics::Printing;
        using namespace winrt::Microsoft::UI::Xaml::Printing;
        // Retrieve the window handle (HWND) of the current WinUI 3 window.
        // @see https://learn.microsoft.com/en-us/windows/apps/develop/ui-input/retrieve-hwnd
        auto windowNative{ this->m_inner.as<::IWindowNative>() };
        HWND hWnd{ nullptr };
        windowNative->get_WindowHandle(&hWnd);
        if (!hWnd)
        {
            OutputDebugStringW(L"Failed to retrieve the window handle of the current WinUI 3 window.");
            return;
        }
        // Initialize COM library
        HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
        if (FAILED(hr))
        {
            OutputDebugString(L"Failed to initialize COM library.\n");
            return;
        }
        // Create an instance of IPrintManagerInterop
        com_ptr<IPrintManagerInterop> printManagerInterop;
        hr = CoCreateInstance(CLSID_PrintManager, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(printManagerInterop.put()));
        if (FAILED(hr))
        {
            OutputDebugString(L"Failed to create instance of IPrintManagerInterop.\n");
            return;
        }

Its failing to create an instance of IPirntManagerInterop

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
804 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,806 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.