#include <stdio.h> #include <string.h> #include <memory.h> #include <string> #include <iostream> #include <memory> #include <Windows.h> #include <wrl.h> #include <wrl\client.h> #include <wrl\wrappers\corewrappers.h> #include <roapi.h> #include <windows.storage.h> #include <windows.storage.streams.h> using namespace Microsoft::WRL; using namespace Microsoft::WRL::Wrappers; using namespace ABI::Windows::Foundation; using namespace ABI::Windows::Storage; using namespace ABI::Windows::Storage::Streams; int main() { RoInitialize(RO_INIT_MULTITHREADED); ComPtr<IKnownFoldersStatics> pKnownFolders; HRESULT hr = RoGetActivationFactory(HStringReference(RuntimeClass_Windows_Storage_KnownFolders).Get(), IID_PPV_ARGS(pKnownFolders.GetAddressOf())); ComPtr<IStorageFolder> pFolder; pKnownFolders->get_VideosLibrary(&pFolder); RoUninitialize(); getchar(); return 0; }好了,可以看到,我们拿到了用户的视频文件夹,从上面的代码大家可以发现,这些代码,头文件和using多生死,主函数真正有用的代码满打满算就 4行,是不是微软脑残?
__FIAsyncOperation_1_Windows__CStorage__CStorageFile **operation
#define __FIAsyncOperation_1_Windows__CStorage__CStorageFile ABI::Windows::Foundation::__FIAsyncOperation_1_Windows__CStorage__CStorageFile_t
typedef IAsyncOperation<ABI::Windows::Storage::StorageFile*> __FIAsyncOperation_1_Windows__CStorage__CStorageFile_t;
namespace ABI { namespace Windows { namespace Foundation { template <> struct __declspec(uuid("5e52f8ce-aced-5a42-95b4-f674dd84885e")) IAsyncOperation<ABI::Windows::Storage::StorageFile*> : IAsyncOperation_impl<ABI::Windows::Foundation::Internal::AggregateType<ABI::Windows::Storage::StorageFile*, ABI::Windows::Storage::IStorageFile*>> { static const wchar_t* z_get_rc_name_impl() { return L"Windows.Foundation.IAsyncOperation`1<Windows.Storage.StorageFile>"; } };
ComPtr<__FIAsyncOperation_1_Windows__CStorage__CStorageFile> pFile; pFolder->CreateFileAsync(HStringReference(L"123.mp4").Get(), CreationCollisionOption::CreationCollisionOption_ReplaceExisting, pFile.GetAddressOf());
ComPtr<IAsyncOperation<ABI::Windows::Storage::StorageFile*>> pFile; pFolder->CreateFileAsync(HStringReference(L"123.mp4").Get(), CreationCollisionOption::CreationCollisionOption_ReplaceExisting, pFile.GetAddressOf());
ComPtr<IUnknown> pUnk; pFolder->CreateFileAsync(HStringReference(L"123.mp4").Get(), CreationCollisionOption::CreationCollisionOption_ReplaceExisting, (__FIAsyncOperation_1_Windows__CStorage__CStorageFile**)pUnk.GetAddressOf());
ComPtr<IAsyncInfo> pAsyncInfo; pUnk->QueryInterface(IID_PPV_ARGS(pAsyncInfo.GetAddressOf()));
//await while (1) { AsyncStatus asyncStatus; HRESULT hr = pAsyncInfo->get_Status(&asyncStatus); if (SUCCEEDED(hr) && (asyncStatus == AsyncStatus::Completed)) break; SwitchToThread(); }
public interface IAsyncOperation<TResult> : IAsyncInfo { AsyncOperationCompletedHandler<TResult> Completed { get; set; } TResult GetResults(); }
namespace ABI { namespace Windows { namespace Foundation { template <> struct __declspec(uuid("5e52f8ce-aced-5a42-95b4-f674dd84885e")) IAsyncOperation<ABI::Windows::Storage::StorageFile*> : IAsyncOperation_impl<ABI::Windows::Foundation::Internal::AggregateType<ABI::Windows::Storage::StorageFile*, ABI::Windows::Storage::IStorageFile*>> { static const wchar_t* z_get_rc_name_impl() { return L"Windows.Foundation.IAsyncOperation`1<Windows.Storage.StorageFile>"; } };
ComPtr<IAsyncOperation<ABI::Windows::Storage::StorageFile*>> pNewFile; pUnk->QueryInterface(IID_PPV_ARGS(pNewFile.GetAddressOf()));
ComPtr<IStorageFile> pFile; pNewFile->GetResults(pFile.GetAddressOf());
PUSH EDX PUSH OFFSET ~1.__GUID_5e52f8ce_aced_5a42_95b4_f674dd84885e PUSH EAX MOV ECX,DWORD PTR DS:[EAX] CALL DWORD PTR DS:[ECX] -> QueryInterface
template <class TResult> struct IAsyncOperation_impl : IInspectable { private: typedef typename Windows::Foundation::Internal::GetAbiType<TResult>::type TResult_abi; typedef typename Windows::Foundation::Internal::GetLogicalType<TResult>::type TResult_logical; public: typedef TResult TResult_complex; virtual HRESULT STDMETHODCALLTYPE put_Completed( IAsyncOperationCompletedHandler<TResult_logical> *handler) = 0; virtual HRESULT STDMETHODCALLTYPE get_Completed( IAsyncOperationCompletedHandler<TResult_logical> **handler) = 0; virtual HRESULT STDMETHODCALLTYPE GetResults( TResult_abi *results) = 0; };
template <class TResult> struct IAsyncOperation_impl : IInspectable { public: virtual HRESULT STDMETHODCALLTYPE put_Completed( IAsyncOperationCompletedHandler<TResult> *handler) = 0; virtual HRESULT STDMETHODCALLTYPE get_Completed( IAsyncOperationCompletedHandler<TResult> **handler) = 0; virtual HRESULT STDMETHODCALLTYPE GetResults( TResult *results) = 0; };
ComPtr<IStorageFile> pFile; pNewFile->GetResults(pFile.GetAddressOf());
MIDL_INTERFACE("5e52f8ce-aced-5a42-95b4-f674dd84885e") IAsyncOperation_ : public IInspectable { public: virtual HRESULT STDMETHODCALLTYPE put_Completed( IUnknown *handler) = 0; virtual HRESULT STDMETHODCALLTYPE get_Completed( IUnknown **handler) = 0; virtual HRESULT STDMETHODCALLTYPE GetResults( IUnknown **results) = 0; };
/* ComPtr<IAsyncOperation<ABI::Windows::Storage::StorageFile*>> pNewFile; pUnk->QueryInterface(IID_PPV_ARGS(pNewFile.GetAddressOf())); ComPtr<IStorageFile> pFile; pNewFile->GetResults(pFile.GetAddressOf()); */ ComPtr<IAsyncOperation_> pNewFile; pUnk->QueryInterface(IID_PPV_ARGS(pNewFile.GetAddressOf())); ComPtr<IUnknown> pTempFile; pNewFile->GetResults(pTempFile.GetAddressOf()); ComPtr<IStorageFile> pFile; pTempFile->QueryInterface(IID_PPV_ARGS(pFile.GetAddressOf()));
卧槽,我们发现,我们不使用那个脑残的模板了,我们终于可以忽略那个浆糊的代码了。。。
来,这是全部的代码:
#include <stdio.h> #include <string.h> #include <memory.h> #include <string> #include <iostream> #include <memory> #include <Windows.h> #include <wrl.h> #include <wrl\client.h> #include <wrl\wrappers\corewrappers.h> #include <roapi.h> #include <windows.storage.h> #include <windows.storage.streams.h> using namespace Microsoft::WRL; using namespace Microsoft::WRL::Wrappers; using namespace ABI::Windows::Foundation; using namespace ABI::Windows::Storage; using namespace ABI::Windows::Storage::Streams; MIDL_INTERFACE("5e52f8ce-aced-5a42-95b4-f674dd84885e") IAsyncOperation_ : public IInspectable { public: virtual HRESULT STDMETHODCALLTYPE put_Completed( IUnknown *handler) = 0; virtual HRESULT STDMETHODCALLTYPE get_Completed( IUnknown **handler) = 0; virtual HRESULT STDMETHODCALLTYPE GetResults( IUnknown **results) = 0; }; int main() { RoInitialize(RO_INIT_MULTITHREADED); ComPtr<IKnownFoldersStatics> pKnownFolders; HRESULT hr = RoGetActivationFactory(HStringReference(RuntimeClass_Windows_Storage_KnownFolders).Get(), IID_PPV_ARGS(pKnownFolders.GetAddressOf())); ComPtr<IStorageFolder> pFolder; pKnownFolders->get_VideosLibrary(&pFolder); ComPtr<IUnknown> pUnk; pFolder->CreateFileAsync(HStringReference(L"123.mp4").Get(), CreationCollisionOption::CreationCollisionOption_ReplaceExisting, (__FIAsyncOperation_1_Windows__CStorage__CStorageFile**)pUnk.GetAddressOf()); ComPtr<IAsyncInfo> pAsyncInfo; pUnk->QueryInterface(IID_PPV_ARGS(pAsyncInfo.GetAddressOf())); //await while (1) { AsyncStatus asyncStatus; HRESULT hr = pAsyncInfo->get_Status(&asyncStatus); if (SUCCEEDED(hr) && (asyncStatus == AsyncStatus::Completed)) break; SwitchToThread(); } /* ComPtr<IAsyncOperation<ABI::Windows::Storage::StorageFile*>> pNewFile; pUnk->QueryInterface(IID_PPV_ARGS(pNewFile.GetAddressOf())); ComPtr<IStorageFile> pFile; pNewFile->GetResults(pFile.GetAddressOf()); */ ComPtr<IAsyncOperation_> pNewFile; pUnk->QueryInterface(IID_PPV_ARGS(pNewFile.GetAddressOf())); ComPtr<IUnknown> pTempFile; pNewFile->GetResults(pTempFile.GetAddressOf()); ComPtr<IStorageFile> pFile; pTempFile->QueryInterface(IID_PPV_ARGS(pFile.GetAddressOf())); printf("CreateFile Async OK."); RoUninitialize(); getchar(); return 0; }