|
| 1 | +#include "ShareExchange.h" |
| 2 | + |
| 3 | +#include <ShObjIdl_core.h> |
| 4 | +#include <winrt/base.h> |
| 5 | +#include <winrt/Windows.Foundation.h> |
| 6 | +#include <winrt/Windows.Foundation.Collections.h> |
| 7 | +#include <winrt/Windows.ApplicationModel.DataTransfer.h> |
| 8 | +#include <winrt/Windows.Storage.h> |
| 9 | + |
| 10 | +namespace winrt |
| 11 | +{ |
| 12 | + using Windows::ApplicationModel::DataTransfer::DataTransferManager; |
| 13 | + using Windows::ApplicationModel::DataTransfer::DataRequestedEventArgs; |
| 14 | + using Windows::ApplicationModel::DataTransfer::DataPackageOperation; |
| 15 | + using Windows::Storage::IStorageItem; |
| 16 | + using Windows::Storage::StorageFile; |
| 17 | +} |
| 18 | + |
| 19 | +K7ShareExchangeInterop::K7ShareExchangeInterop( |
| 20 | + _In_ std::vector<std::wstring> const& SharingPaths) : |
| 21 | + m_StdPaths(SharingPaths) |
| 22 | +{ |
| 23 | +} |
| 24 | + |
| 25 | +void K7ShareExchangeInterop::SharingByWindowsMessaging( |
| 26 | + _In_opt_ HWND const& ParentWindowHandle) |
| 27 | +{ |
| 28 | + if (this->m_MapiFiles.empty()) |
| 29 | + { |
| 30 | + this->m_MapiFiles.reserve(m_StdPaths.size()); |
| 31 | + for (auto& Path : m_StdPaths) |
| 32 | + { |
| 33 | + MapiFileDescW Cache{}; |
| 34 | + Cache.nPosition = static_cast<ULONG>(-1); |
| 35 | + Cache.lpszPathName = Path.data(); |
| 36 | + //Cache.lpszFileName = |
| 37 | + this->m_MapiFiles.emplace_back(Cache); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + static decltype(::MAPISendMailW)* volatile pMAPISendMailW{ |
| 42 | + reinterpret_cast<decltype(::MAPISendMailW)*>( |
| 43 | + winrt::check_pointer(::GetProcAddress( |
| 44 | + winrt::check_pointer(::LoadLibraryExW( |
| 45 | + L"mapi32.dll", |
| 46 | + nullptr, |
| 47 | + LOAD_LIBRARY_SEARCH_SYSTEM32)), |
| 48 | + "MAPISendMailW"))) }; |
| 49 | + |
| 50 | + MapiMessageW Message{}; |
| 51 | + Message.nFileCount = static_cast<ULONG>(this->m_MapiFiles.size()); |
| 52 | + Message.lpFiles = this->m_MapiFiles.data(); |
| 53 | + |
| 54 | + ::EnableWindow(ParentWindowHandle, false); |
| 55 | + pMAPISendMailW( |
| 56 | + reinterpret_cast<LHANDLE>(nullptr), |
| 57 | + reinterpret_cast<ULONG_PTR>(ParentWindowHandle), |
| 58 | + &Message, |
| 59 | + MAPI_DIALOG & MAPI_LOGON_UI & MAPI_FORCE_UNICODE, |
| 60 | + 0); |
| 61 | + ::EnableWindow(ParentWindowHandle, true); |
| 62 | +} |
| 63 | + |
| 64 | +void K7ShareExchangeInterop::SharingByDataTransferManager( |
| 65 | + _In_ HWND const& ParentWindowHandle) |
| 66 | +{ |
| 67 | + winrt::DataTransferManager Manager{ nullptr }; |
| 68 | + winrt::com_ptr<IDataTransferManagerInterop> Interop{ |
| 69 | + winrt::get_activation_factory |
| 70 | + <winrt::DataTransferManager, IDataTransferManagerInterop>() }; |
| 71 | + winrt::check_hresult(Interop->GetForWindow( |
| 72 | + ParentWindowHandle, |
| 73 | + winrt::guid_of<winrt::DataTransferManager>(), |
| 74 | + winrt::put_abi(Manager))); |
| 75 | + |
| 76 | + if (Manager.IsSupported()) |
| 77 | + { |
| 78 | + if (!this->m_WinRtFiles) |
| 79 | + { |
| 80 | + this->m_WinRtFiles = |
| 81 | + winrt::single_threaded_vector |
| 82 | + <winrt::Windows::Storage::IStorageItem>(); |
| 83 | + for (auto const& Path : this->m_StdPaths) |
| 84 | + { |
| 85 | + this->m_WinRtFiles.Append( |
| 86 | + winrt::StorageFile::GetFileFromPathAsync(Path).get()); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + Manager.DataRequested( |
| 91 | + [this](winrt::DataTransferManager const&, |
| 92 | + winrt::DataRequestedEventArgs const& Args) |
| 93 | + { |
| 94 | + Args.Request().Data().Properties().Title(L"NanaZip"); |
| 95 | + Args.Request().Data().SetStorageItems(this->m_WinRtFiles); |
| 96 | + Args.Request().Data().RequestedOperation( |
| 97 | + winrt::DataPackageOperation::None); |
| 98 | + }); |
| 99 | + winrt::check_hresult(Interop->ShowShareUIForWindow( |
| 100 | + ParentWindowHandle)); |
| 101 | + } |
| 102 | +} |
| 103 | + |
0 commit comments