#include "pch.h" #include "RAWinRT.WRL.h" using namespace Microsoft::WRL::Wrappers; using namespace Microsoft::WRL; using namespace ABI::RAWinRT::WRL; using namespace ABI::Windows::ApplicationModel::Background; class ABI::RAWinRT::WRL::TestTask : public RuntimeClass < RuntimeClassFlags<WinRt>, IBackgroundTask > { InspectableClass(RuntimeClass_RAWinRT_WRL_TestTask, BaseTrust); public: STDMETHODIMP Run(IBackgroundTaskInstance *taskInstance) override { return S_OK; } }; ActivatableClass(TestTask);
del "$(OutDir)$(TargetName).winmd" && mdmerge -partial -i "$(OutDir)." -o "$(OutDir)Output" -metadata_dir "$(WindowsSDK_MetadataPath)" && del "$(OutDir)*.winmd" && copy /y "$(OutDir)Output\*" "$(OutDir)" && rd /q /s "$(OutDir)Output"
EXPORTS DllGetActivationFactory PRIVATE DllCanUnloadNow PRIVATE
import "Windows.ApplicationModel.Background.idl"; namespace NMSPC { namespace TestComponent { [version(1.0)] [activatable(1.0)] [marshaling_behavior(agile)] [threading(both)] runtimeclass TestBackgroundTask { [default] interface Windows.ApplicationModel.Background.IBackgroundTask; }; } }
#pragma once #include "targetver.h" #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers #endif #include <windows.h> #include <activation.h> #include <new>
#include "pch.h" #include "TestComponent.h" // using namespace ABI::NMSPC::TestComponent; // ABI::Windows::ApplicationModel::Background using ABI::Windows::ApplicationModel::Background::IBackgroundTask; using ABI::Windows::ApplicationModel::Background::IBackgroundTaskInstance;
// . // "" IBackgroundTask class ABI::NMSPC::TestComponent::TestBackgroundTask sealed : public IBackgroundTask { // ULONG m_count; public: TestBackgroundTask() throw() : m_count(1) { // InterlockedIncrement(&m_objectsCount); } ~TestBackgroundTask() throw() { // InterlockedDecrement(&m_objectsCount); } #pragma region IUnknown // COM STDMETHODIMP_(ULONG) AddRef() throw() override final { // return InterlockedIncrement(&m_count); } // COM STDMETHODIMP_(ULONG) Release() throw() override { // auto const count = InterlockedDecrement(&m_count); // if (0 == count) { // delete this; } // return count; } // COM STDMETHODIMP QueryInterface(const IID& riid, void** ppvObject) throw() override final { // // IBackgroundTask IInspectable // IInspectable IUnknown if (__uuidof(IUnknown) == riid || __uuidof(IInspectable) == riid || __uuidof(IBackgroundTask) == riid) { *ppvObject = this; } else { *ppvObject = nullptr; // , return E_NOINTERFACE; } // // static_cast<IInspectable*>(*ppvObject)->AddRef(); return S_OK; } #pragma endregion #pragma region IInspectable // WINRT STDMETHODIMP GetIids(ULONG* iidCount, IID** iids) throw() override { // GUID, .. // CoTaskMemAlloc, .. CoTaskMemFree *iids = static_cast<GUID*>(CoTaskMemAlloc(sizeof(GUID))); // NULL if (!*iids) { // return E_OUTOFMEMORY; } // *iidCount = 1; // IBackgroundTask (*iids)[0] = __uuidof(IBackgroundTask); return S_OK; } // WINRT Runtime STDMETHODIMP GetRuntimeClassName(HSTRING* className) throw() override final { // // E_OUTOFMEMORY // if (S_OK != WindowsCreateString( RuntimeClass_NMSPC_TestComponent_TestBackgroundTask, _countof(RuntimeClass_NMSPC_TestComponent_TestBackgroundTask), className)) { return E_OUTOFMEMORY; } return S_OK; } // WINRT TrustLevel STDMETHODIMP GetTrustLevel(TrustLevel* trustLevel) throw() override final { *trustLevel = BaseTrust; return S_OK; } #pragma endregion #pragma region IBackgroundTask // IBackgroundTask STDMETHODIMP Run(IBackgroundTaskInstance* task_instance) throw() override final { // OutputDebugStringW(L"Hello from background task.\r\n"); return S_OK; } #pragma endregion };
virtual HRESULT STDMETHODCALLTYPE ActivateInstance( /* [out] */ __RPC__deref_out_opt IInspectable **instance) = 0;
// . class TestBackgroundTaskFactory sealed : public IActivationFactory { // ULONG m_count; public: TestBackgroundTaskFactory() throw() : m_count(1) { // InterlockedIncrement(&m_objectsCount); } ~TestBackgroundTaskFactory() throw() { // InterlockedDecrement(&m_objectsCount); } // COM STDMETHODIMP_(ULONG) AddRef() throw() override final { // return InterlockedIncrement(&m_count); } // COM STDMETHODIMP_(ULONG) Release() throw() override { // auto const count = InterlockedDecrement(&m_count); // if (0 == count) { // delete this; } // return count; } // COM STDMETHODIMP QueryInterface(const IID& riid, void** ppvObject) throw() override final { if (__uuidof(IUnknown) == riid || __uuidof(IInspectable) == riid || __uuidof(IActivationFactory) == riid) { *ppvObject = this; } else { *ppvObject = nullptr; return E_NOINTERFACE; } static_cast<IInspectable*>(*ppvObject)->AddRef(); return S_OK; } // WINRT STDMETHODIMP GetIids(ULONG* iidCount, IID** iids) throw() override final { // GUID, .. // CoTaskMemAlloc, .. CoTaskMemFree *iids = static_cast<GUID*>(CoTaskMemAlloc(sizeof(GUID))); // NULL if (*iids) { // return E_OUTOFMEMORY; } // *iidCount = 1; // IBackgroundTask (*iids)[0] = __uuidof(IActivationFactory); return S_OK; } // WINRT Runtime STDMETHODIMP GetRuntimeClassName(HSTRING*) throw() override final { // , .. return E_ILLEGAL_METHOD_CALL; } // WINRT TrustLevel STDMETHODIMP GetTrustLevel(TrustLevel* trustLevel) throw() override final { *trustLevel = BaseTrust; return S_OK; } // IActivationFactory STDMETHODIMP ActivateInstance(IInspectable** instance) throw() override final { // null if (nullptr == instance) { // return E_INVALIDARG; } // // , *instance = new (std::nothrow) TestBackgroundTask(); // return *instance ? S_OK : E_OUTOFMEMORY; } };
// HRESULT WINAPI DllCanUnloadNow() throw() { // return m_objectsCount ? S_FALSE : S_OK; }
// , activatableClassId HRESULT WINAPI DllGetActivationFactory(HSTRING activatableClassId, IActivationFactory **factory) throw() { // if (WindowsIsStringEmpty(activatableClassId) || nullptr == factory) { // return E_INVALIDARG; } // if (0 == wcscmp(RuntimeClass_NMSPC_TestComponent_TestBackgroundTask, WindowsGetStringRawBuffer(activatableClassId, nullptr))) { // *factory = new (std::nothrow) TestBackgroundTaskFactory(); return *factory ? S_OK : E_OUTOFMEMORY; } *factory = nullptr; return E_NOINTERFACE; }
#include "pch.h" BOOL APIENTRY DllMain(HMODULE /* hModule */, DWORD ul_reason_for_call, LPVOID /* lpReserved */) { OutputDebugStringW(L"Hello from DLL.\r\n"); return TRUE; }
foreach (var pair in BackgroundTaskRegistration.AllTasks) { pair.Value.Unregister(true); } var taskBuilder = new BackgroundTaskBuilder { Name = "TestBackgroundTask", TaskEntryPoint = "NMSPC.TestComponent.TestBackgroundTask" }; taskBuilder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, true)); taskBuilder.Register();
Hello from DLL. Hello from background task.
Source: https://habr.com/ru/post/252759/
All Articles