mnovakovic
mnovakovic

Reputation: 61

How to load a resource from a dll in UWP?

I have some .cur files embedded in a DLL file and I want to load and use them in my UWP C++ project. Is this at all possible?

I know that in Win32 I can use LoadImage or LoadCursor, but that API is not available for UWP. I can load a dll using LoadPackagedLibrary but no idea how to get the cursor from it.

Upvotes: 1

Views: 1243

Answers (2)

Franklin Chen - MSFT
Franklin Chen - MSFT

Reputation: 4923

I have some .cur files embedded in a DLL file and I want to load and use them in my UWP C++ project. Is this at all possible?

Based on my experience, it is impossible currently. Here are my explanations:

About how to set custom Cursor using .cur file, there are some discussions before:

This article is outdated for UWP app, but the idea is the same: Define a custom cursor in a native resource library and set cursor using CoreWindow.PointerCursor property

We need to create a custom cursor in a DLL(Universal Windows) project, most of steps are the same, please see CREATE A CUSTOM CURSOR part in the above article.

Then add the existing item .rc file to our C++ UWP app, use the following code to set custom cursor:

Windows::UI::Core::CoreCursorType cursorType = Windows::UI::Core::CoreCursorType::Custom;
CoreCursor ^* theCursor = new CoreCursor ^ (nullptr);
*theCursor = ref new CoreCursor(cursorType, 101); //101 is the resource id number
CoreWindow::GetForCurrentThread()->PointerCursor = *theCursor;

enter image description here

I've tried to package the resource file into a Windows Runtime Component, although it has been added as reference, the resource can't be recognized correctly.

So we have to integrate the resource in our uwp app for such special requirement.

Please see my sample: https://github.com/Myfreedom614/UWP-Samples/tree/master/UWPCreateCurCPPAPP1/UWPCreateCurCPPAPP1

Upvotes: 2

Sunius
Sunius

Reputation: 2907

You can use CoreCursor APIs to do this:

template <typename T>
HRESULT RestoreCursor(T* cursorOwner, uint32_t resourceId)
{
    using namespace ABI::Windows::UI::Core;
    using namespace Microsoft::WRL;
    using namespace Microsoft::WRL::Wrappers;

    ComPtr<ICoreCursorFactory> cursorFactory;
    hr = RoGetActivationFactory(HStringReference(L"Windows.UI.Core.CoreCursor").Get(), __uuidof(ICoreCursorFactory), &cursorFactory);
    if (FAILED(hr)) return hr;

    ComPtr<ICoreCursor> cursor;
    hr = cursorFactory->CreateCursor(CoreCursorType_Arrow, resourceId, &cursor);
    if (FAILED(hr)) return hr;

    return cursorOwner->put_PointerCursor(cursor.Get());
}

T here may be ICorePointerInputSource (like CoreIndependentInputSource) or ICoreWindow, depending on whether you're using XAML or not.

I'm just not 100% positive whether you can include resources into DLL, or whether they must be included in the .exe itself.

Upvotes: 0

Related Questions