[PATCH 0/9] MR11297: windows.storage: Implement InMemoryRandomAccessStream.
From: Conor McCarthy <cmccarthy@codeweavers.com> --- dlls/windows.storage/tests/storage.c | 499 +++++++++++++++++++++++++++ 1 file changed, 499 insertions(+) diff --git a/dlls/windows.storage/tests/storage.c b/dlls/windows.storage/tests/storage.c index 85bda00e044..ae842ab1f78 100644 --- a/dlls/windows.storage/tests/storage.c +++ b/dlls/windows.storage/tests/storage.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2025 Mohamad Al-Jaf + * Copyright 2026 Conor McCarthy for CodeWeavers * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -30,6 +31,7 @@ #include "windows.foundation.h" #define WIDL_using_Windows_Storage_Streams #include "windows.storage.streams.h" +#include "robuffer.h" #include "wine/test.h" @@ -46,6 +48,114 @@ static void check_interface_( unsigned int line, void *obj, const IID *iid, BOOL IUnknown_Release( unk ); } +#define DEFINE_ASYNC_COMPLETED_HANDLER( name, iface_type, async_type ) \ + struct name \ + { \ + iface_type iface_type##_iface; \ + LONG ref; \ + BOOL invoked; \ + HANDLE event; \ + }; \ + \ + static HRESULT WINAPI name##_QueryInterface( iface_type *iface, REFIID iid, void **out ) \ + { \ + if (IsEqualGUID( iid, &IID_IUnknown ) || IsEqualGUID( iid, &IID_IAgileObject ) || \ + IsEqualGUID( iid, &IID_##iface_type )) \ + { \ + IUnknown_AddRef( iface ); \ + *out = iface; \ + return S_OK; \ + } \ + \ + trace( "%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid( iid ) ); \ + *out = NULL; \ + return E_NOINTERFACE; \ + } \ + \ + static ULONG WINAPI name##_AddRef( iface_type *iface ) \ + { \ + struct name *impl = CONTAINING_RECORD( iface, struct name, iface_type##_iface ); \ + return InterlockedIncrement( &impl->ref ); \ + } \ + \ + static ULONG WINAPI name##_Release( iface_type *iface ) \ + { \ + struct name *impl = CONTAINING_RECORD( iface, struct name, iface_type##_iface ); \ + ULONG ref = InterlockedDecrement( &impl->ref ); \ + if (!ref) free( impl ); \ + return ref; \ + } \ + \ + static HRESULT WINAPI name##_Invoke( iface_type *iface, async_type *async, AsyncStatus status ) \ + { \ + struct name *impl = CONTAINING_RECORD( iface, struct name, iface_type##_iface ); \ + ok( !impl->invoked, "invoked twice\n" ); \ + impl->invoked = TRUE; \ + if (impl->event) SetEvent( impl->event ); \ + return S_OK; \ + } \ + \ + static iface_type##Vtbl name##_vtbl = \ + { \ + name##_QueryInterface, \ + name##_AddRef, \ + name##_Release, \ + name##_Invoke, \ + }; \ + \ + static iface_type *name##_create( HANDLE event ) \ + { \ + struct name *impl; \ + \ + if (!(impl = calloc( 1, sizeof(*impl) ))) return NULL; \ + impl->iface_type##_iface.lpVtbl = &name##_vtbl; \ + impl->event = event; \ + impl->ref = 1; \ + \ + return &impl->iface_type##_iface; \ + } \ + \ + static DWORD await_##async_type( async_type *async, DWORD timeout ) \ + { \ + iface_type *handler; \ + HANDLE event; \ + HRESULT hr; \ + DWORD ret; \ + \ + event = CreateEventW( NULL, FALSE, FALSE, NULL ); \ + ok( !!event, "CreateEventW failed, error %lu\n", GetLastError() ); \ + handler = name##_create( event ); \ + ok( !!handler, "Failed to create completion handler\n" ); \ + hr = async_type##_put_Completed( async, handler ); \ + ok( hr == S_OK, "put_Completed returned %#lx\n", hr ); \ + ret = WaitForSingleObject( event, timeout ); \ + ok( !ret, "WaitForSingleObject returned %#lx\n", ret ); \ + CloseHandle( event ); \ + iface_type##_Release( handler ); \ + \ + return ret; \ + } + +static HRESULT get_activation_factory( const WCHAR *name, IActivationFactory **factory ) +{ + HSTRING str = NULL; + HRESULT hr; + + hr = WindowsCreateString( name, wcslen( name ), &str ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + hr = RoGetActivationFactory( str, &IID_IActivationFactory, (void **)factory ); + WindowsDeleteString( str ); + todo_wine + ok( hr == S_OK || broken( hr == REGDB_E_CLASSNOTREG ), "got hr %#lx.\n", hr ); + + if (hr == REGDB_E_CLASSNOTREG) + win_skip( "%s runtimeclass not registered, skipping tests.\n", wine_dbgstr_w( name ) ); + else if (hr == CLASS_E_CLASSNOTAVAILABLE) + skip( "%s runtimeclass not available, skipping tests.\n", wine_dbgstr_w( name ) ); + + return hr; +} + static void test_RandomAccessStreamReference(void) { static const WCHAR *random_access_stream_reference_statics_name = L"Windows.Storage.Streams.RandomAccessStreamReference"; @@ -84,6 +194,394 @@ static void test_RandomAccessStreamReference(void) ok( ref == 0, "got ref %ld.\n", ref ); } +DEFINE_ASYNC_COMPLETED_HANDLER( async_uint32_uint32_completed_handler, \ + IAsyncOperationWithProgressCompletedHandler_UINT32_UINT32, IAsyncOperationWithProgress_UINT32_UINT32 ) + +DEFINE_ASYNC_COMPLETED_HANDLER( async_buffer_uint32_completed_handler, \ + IAsyncOperationWithProgressCompletedHandler_IBuffer_UINT32, IAsyncOperationWithProgress_IBuffer_UINT32 ) + +#define check_async_info( a, b, c ) check_async_info_( __LINE__, a, b, c ) +static void check_async_info_( int line, void *async, AsyncStatus expect_status, HRESULT expect_hr ) +{ + AsyncStatus async_status; + IAsyncInfo *async_info; + HRESULT hr, async_hr; + UINT32 async_id; + + hr = IInspectable_QueryInterface( async, &IID_IAsyncInfo, (void **)&async_info ); + ok_(__FILE__, line)( hr == S_OK, "QueryInterface returned %#lx\n", hr ); + + async_id = 0xdeadbeef; + hr = IAsyncInfo_get_Id( async_info, &async_id ); + if (expect_status < 4) ok_(__FILE__, line)( hr == S_OK, "get_Id returned %#lx\n", hr ); + else ok_(__FILE__, line)( hr == E_ILLEGAL_METHOD_CALL, "get_Id returned %#lx\n", hr ); + ok_(__FILE__, line)( !!async_id, "got id %u\n", async_id ); + + async_status = 0xdeadbeef; + hr = IAsyncInfo_get_Status( async_info, &async_status ); + if (expect_status < 4) ok_(__FILE__, line)( hr == S_OK, "get_Status returned %#lx\n", hr ); + else ok_(__FILE__, line)( hr == E_ILLEGAL_METHOD_CALL, "get_Status returned %#lx\n", hr ); + ok_(__FILE__, line)( async_status == expect_status, "got status %u\n", async_status ); + + async_hr = 0xdeadbeef; + hr = IAsyncInfo_get_ErrorCode( async_info, &async_hr ); + if (expect_status < 4) ok_(__FILE__, line)( hr == S_OK, "get_ErrorCode returned %#lx\n", hr ); + else ok_(__FILE__, line)( hr == E_ILLEGAL_METHOD_CALL, "get_ErrorCode returned %#lx\n", hr ); + if (expect_status < 4) ok_(__FILE__, line)( async_hr == expect_hr, "got error %#lx\n", async_hr ); + else ok_(__FILE__, line)( async_hr == E_ILLEGAL_METHOD_CALL, "got error %#lx\n", async_hr ); + + IAsyncInfo_Release( async_info ); +} + +#define output_stream_write( a, b, c ) output_stream_write_(__LINE__, a, b, c ) +static void output_stream_write_( unsigned int line, IOutputStream *output_stream, IBuffer *buffer, UINT32 count ) +{ + IAsyncOperationWithProgress_UINT32_UINT32 *operation; + UINT32 written; + HRESULT hr; + UINT res; + + hr = IBuffer_put_Length( buffer, count ); + ok_(__FILE__, line)( hr == S_OK, "got hr %#lx.\n", hr ); + hr = IOutputStream_WriteAsync( output_stream, buffer, &operation ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + if (FAILED(hr)) return; + res = await_IAsyncOperationWithProgress_UINT32_UINT32( operation, 1000 ); + ok( res == 0, "await_IAsyncOperationWithProgress_UINT32_UINT32 returned %#x\n", res ); + check_async_info( operation, Completed, S_OK ); + hr = IAsyncOperationWithProgress_UINT32_UINT32_GetResults( operation, &written ); + ok_(__FILE__, line)( hr == S_OK, "got hr %#lx.\n", hr ); + ok_(__FILE__, line)( written == count, "wrote %u bytes.\n", written ); + IAsyncOperationWithProgress_UINT32_UINT32_Release( operation ); +} + +#define input_stream_read( a, b, c, d, e, f ) input_stream_read_(__LINE__, a, b, c, d, e, f ) +static void input_stream_read_( unsigned int line, IInputStream *input_stream, IBuffer *buffer, UINT32 count, + AsyncStatus expect_status, HRESULT expect_hr, IBuffer **res_buffer ) +{ + IAsyncOperationWithProgress_IBuffer_UINT32 *operation; + HRESULT hr; + UINT res; + + hr = IInputStream_ReadAsync( input_stream, buffer, count, 0, &operation ); + todo_wine + ok_(__FILE__, line)( hr == S_OK, "got hr %#lx.\n", hr ); + if (FAILED(hr)) return; + res = await_IAsyncOperationWithProgress_IBuffer_UINT32( operation, 1000 ); + ok_(__FILE__, line)( res == 0, "await_IAsyncOperationWithProgress_IBuffer_UINT32 returned %#x\n", res ); + check_async_info( operation, expect_status, expect_hr ); + hr = IAsyncOperationWithProgress_IBuffer_UINT32_GetResults( operation, res_buffer ); + ok_(__FILE__, line)( hr == expect_hr, "got hr %#lx.\n", hr ); + IAsyncOperationWithProgress_IBuffer_UINT32_Release( operation ); +} + +static BYTE *buffer_get_data( IBuffer *buffer ) +{ + IBufferByteAccess *access; + BYTE *data = NULL; + HRESULT hr; + + hr = IBuffer_QueryInterface( buffer, &IID_IBufferByteAccess, (void **)&access ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + hr = IBufferByteAccess_Buffer( access, &data ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + IBufferByteAccess_Release( access ); + + return data; +} + +static void test_InMemoryRandomAccessStream(void) +{ + static const WCHAR *in_memory_stream_statics_name = L"Windows.Storage.Streams.InMemoryRandomAccessStream"; + static const WCHAR *buffer_statics_name = L"Windows.Storage.Streams.Buffer"; + IRandomAccessStream *in_memory_stream = (void *)0xdeadbeef; + IInspectable *in_memory_inspectable = (void *)0xdeadbeef; + IAsyncOperationWithProgress_IBuffer_UINT32 *operation; + IAsyncOperationWithProgress_UINT32_UINT32 *write_op; + IActivationFactory *factory = (void *)0xdeadbeef; + const UINT64 uint64_value = 0xdeadbeefcafef00d; + IBuffer *buffer, *res_buffer = NULL; + IBufferFactory *buffer_factory; + IOutputStream *output_stream; + IInputStream *input_stream; + BYTE byte_value = 0xab; + BOOLEAN value_bool = 0; + IClosable *closable; + UINT64 value64 = 0; + BYTE *data = NULL; + UINT32 value = 0; + HRESULT hr; + LONG ref; + + if (FAILED(hr = get_activation_factory( in_memory_stream_statics_name, &factory ))) + return; + + check_interface( factory, &IID_IUnknown, FALSE ); + check_interface( factory, &IID_IInspectable, FALSE ); + check_interface( factory, &IID_IAgileObject, TRUE /* Supported after Windows 10 1607 */ ); + + hr = IActivationFactory_ActivateInstance( factory, &in_memory_inspectable ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + ref = IActivationFactory_Release( factory ); + ok( ref == 1, "got ref %ld.\n", ref ); + hr = IInspectable_QueryInterface( in_memory_inspectable, &IID_IRandomAccessStream, (void **)&in_memory_stream ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + IInspectable_Release( in_memory_inspectable ); + + check_interface( in_memory_stream, &IID_IClosable, FALSE ); + + hr = IRandomAccessStream_QueryInterface( in_memory_stream, &IID_IInputStream, (void **)&input_stream ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + check_interface( input_stream, &IID_IUnknown, FALSE ); + check_interface( input_stream, &IID_IInspectable, FALSE ); + check_interface( input_stream, &IID_IRandomAccessStream, FALSE ); + check_interface( input_stream, &IID_IOutputStream, FALSE ); + check_interface( input_stream, &IID_IClosable, FALSE ); + + hr = IRandomAccessStream_QueryInterface( in_memory_stream, &IID_IOutputStream, (void **)&output_stream ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + check_interface( output_stream, &IID_IUnknown, FALSE ); + check_interface( output_stream, &IID_IInspectable, FALSE ); + check_interface( output_stream, &IID_IRandomAccessStream, FALSE ); + check_interface( output_stream, &IID_IInputStream, FALSE ); + + if (FAILED(hr = get_activation_factory( buffer_statics_name, &factory ))) + return; + + hr = IActivationFactory_QueryInterface( factory, &IID_IBufferFactory, (void **)&buffer_factory ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + hr = IBufferFactory_Create( buffer_factory, 17, &buffer ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + IBufferFactory_Release( buffer_factory ); + + IActivationFactory_Release( factory ); + + data = buffer_get_data( buffer ); + + hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + ok( value64 == 0, "got size %I64u.\n", value64 ); + hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + ok( value64 == 0, "got pos %I64u.\n", value64 ); + hr = IRandomAccessStream_Seek( in_memory_stream, 16 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + ok( value64 == 0, "got size %I64u.\n", value64 ); + hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + todo_wine + ok( value64 == 16, "got pos %I64u.\n", value64 ); + + /* Write 0 bytes at position 16 */ + output_stream_write( output_stream, buffer, 0 ); + hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + ok( value64 == 0, "got size %I64u.\n", value64 ); + /* Write 1 byte at position 16 */ + data[0] = byte_value; + output_stream_write( output_stream, buffer, 1 ); + hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + todo_wine + ok( value64 == 17, "got size %I64u.\n", value64 ); + + hr = IRandomAccessStream_Seek( in_memory_stream, 0 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + ok( value64 == 0, "got pos %I64u.\n", value64 ); + memcpy( data, &uint64_value, 8 ); + output_stream_write( output_stream, buffer, 8 ); + hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + todo_wine + ok( value64 == 17, "got size %I64u.\n", value64 ); + hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + todo_wine + ok( value64 == 8, "got pos %I64u.\n", value64 ); + + hr = IOutputStream_WriteAsync( output_stream, NULL, &write_op ); + todo_wine + ok( hr == E_POINTER, "got hr %#lx.\n", hr ); + /* Crashes on Windows if the op pointer is null + * hr = IOutputStream_WriteAsync( output_stream, NULL, NULL ); */ + + memset( data, 0xcd, 17 ); + + hr = IRandomAccessStream_Seek( in_memory_stream, 0 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + res_buffer = NULL; + input_stream_read( input_stream, buffer, 0, Completed, S_OK, &res_buffer ); + todo_wine + ok( res_buffer == buffer, "got different buffer.\n" ); + if (!res_buffer) IBuffer_AddRef( res_buffer = buffer ); + hr = IBuffer_get_Length( res_buffer, &value ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + todo_wine + ok( value == 0, "got read %u.\n", value ); + IBuffer_Release( res_buffer ); + + res_buffer = (void *)0xdeadbeef; + input_stream_read( input_stream, buffer, 20, Error, E_INVALIDARG, &res_buffer ); + todo_wine + ok( !res_buffer, "got res_buffer %p.\n", res_buffer ); + + res_buffer = NULL; + input_stream_read( input_stream, buffer, 17, Completed, S_OK, &res_buffer ); + todo_wine + ok( res_buffer == buffer, "got different buffer.\n" ); + if (!res_buffer) IBuffer_AddRef( res_buffer = buffer ); + hr = IBuffer_get_Length( res_buffer, &value ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + todo_wine + ok( value == 17, "got read %u.\n", value ); + hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + todo_wine + ok( value64 == 17, "got pos %I64u.\n", value64 ); + data = buffer_get_data( res_buffer ); + memcpy( &value64, data, sizeof(value64 ) ); + todo_wine + ok( value64 == uint64_value, "got value64 %#I64x.\n", value64 ); + todo_wine + ok ( data[16] == byte_value, "got byte value %#x.\n", data[16]); + IBuffer_Release( res_buffer ); + + hr = IRandomAccessStream_Seek( in_memory_stream, 18 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + res_buffer = NULL; + input_stream_read( input_stream, buffer, 1, Completed, S_OK, &res_buffer ); + todo_wine + ok( res_buffer == buffer, "got different buffer.\n" ); + if (!res_buffer) IBuffer_AddRef( res_buffer = buffer ); + hr = IBuffer_get_Length( res_buffer, &value ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + todo_wine + ok( value == 0, "got read %u.\n", value ); + IBuffer_Release( res_buffer ); + + operation = (void *)0xdeadbeef; + hr = IInputStream_ReadAsync( input_stream, NULL, 1, 0, &operation ); + todo_wine + ok( hr == E_POINTER, "got hr %#lx.\n", hr ); + ok( !operation, "got operation %p.\n", operation ); + /* Crashes on Windows if the op pointer is null + * hr = IInputStream_ReadAsync( input_stream, NULL, 1, 0, NULL ); */ + + value_bool = 0; + hr = IRandomAccessStream_get_CanRead( in_memory_stream, &value_bool ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + ok( value_bool == TRUE, "got bool %#x.\n", value_bool ); + value_bool = 0; + hr = IRandomAccessStream_get_CanWrite( in_memory_stream, &value_bool ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + ok( value_bool == TRUE, "got bool %#x.\n", value_bool ); + + /* Test large put size and read. Native truncates put_Size() to 32-bit, + * but only allows reading from positions less than 1 << 31 + * Both of these seem potentially subject to change, so are untested. */ + hr = IRandomAccessStream_put_Size( in_memory_stream, 0x100000 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + todo_wine + ok( value64 == 0x100000, "got size %I64u.\n", value64 ); + hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + todo_wine + ok( value64 == 18, "got pos %I64u.\n", value64 ); + + hr = IRandomAccessStream_Seek( in_memory_stream, 0xffff8 ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + res_buffer = NULL; + input_stream_read( input_stream, buffer, 8, Completed, S_OK, &res_buffer ); + todo_wine + ok( res_buffer == buffer, "got different buffer.\n" ); + if (!res_buffer) IBuffer_AddRef( res_buffer = buffer ); + hr = IBuffer_get_Length( res_buffer, &value ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + ok( value == 8, "got read %u.\n", value ); + data = buffer_get_data( res_buffer ); + memcpy( &value64, data, sizeof(value64) ); + todo_wine + ok( value64 == 0, "got value64 %#I64x.\n", value64 ); + IBuffer_Release( res_buffer ); + + hr = IOutputStream_QueryInterface( output_stream, &IID_IClosable, (void **)&closable ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + hr = IClosable_Close( closable ); + todo_wine + ok( hr == S_OK, "got hr %#lx.\n", hr ); + IClosable_Release( closable ); + + write_op = (void *)0xdeadbeef; + hr = IOutputStream_WriteAsync( output_stream, buffer, &write_op ); + todo_wine + ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); + ok( !write_op, "got operation %p.\n", write_op ); + operation = (void *)0xdeadbeef; + hr = IInputStream_ReadAsync( input_stream, buffer, 0, 0, &operation ); + todo_wine + ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); + ok( !operation, "got operation %p.\n", operation ); + + hr = IRandomAccessStream_Seek( in_memory_stream, 0 ); + todo_wine + ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); + value64 = uint64_value; + hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); + todo_wine + ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); + todo_wine + ok( value64 == 0, "got size %I64u.\n", value64 ); + hr = IRandomAccessStream_put_Size( in_memory_stream, 1 ); + todo_wine + ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); + value64 = uint64_value; + hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); + todo_wine + ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); + todo_wine + ok( value64 == 0x100000, "got pos %I64u.\n", value64 ); + value_bool = 0; + hr = IRandomAccessStream_get_CanRead( in_memory_stream, &value_bool ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + ok( value_bool == TRUE, "got bool %#x.\n", value_bool ); + value_bool = 0; + hr = IRandomAccessStream_get_CanWrite( in_memory_stream, &value_bool ); + ok( hr == S_OK, "got hr %#lx.\n", hr ); + ok( value_bool == TRUE, "got bool %#x.\n", value_bool ); + + IBuffer_Release( buffer ); + IOutputStream_Release( output_stream ); + IInputStream_Release( input_stream ); + + ref = IRandomAccessStream_Release( in_memory_stream ); + ok( ref == 0, "got ref %ld.\n", ref ); +} + START_TEST(storage) { HRESULT hr; @@ -92,6 +590,7 @@ START_TEST(storage) ok( hr == S_OK, "RoInitialize failed, hr %#lx\n", hr ); test_RandomAccessStreamReference(); + test_InMemoryRandomAccessStream(); RoUninitialize(); } -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/11297
From: Conor McCarthy <cmccarthy@codeweavers.com> --- dlls/windows.storage/main.c | 2 + dlls/windows.storage/private.h | 1 + dlls/windows.storage/streams.c | 473 +++++++++++++++++++++++++++ dlls/windows.storage/tests/storage.c | 1 - 4 files changed, 476 insertions(+), 1 deletion(-) diff --git a/dlls/windows.storage/main.c b/dlls/windows.storage/main.c index 390f2dec742..909817a263d 100644 --- a/dlls/windows.storage/main.c +++ b/dlls/windows.storage/main.c @@ -38,6 +38,8 @@ HRESULT WINAPI DllGetActivationFactory( HSTRING classid, IActivationFactory **fa if (!wcscmp( buffer, RuntimeClass_Windows_Storage_Streams_RandomAccessStreamReference )) IActivationFactory_QueryInterface( random_access_stream_reference_factory, &IID_IActivationFactory, (void **)factory ); + if (!wcscmp( buffer, RuntimeClass_Windows_Storage_Streams_InMemoryRandomAccessStream )) + IActivationFactory_QueryInterface( memory_stream_activation_factory, &IID_IActivationFactory, (void **)factory ); if (*factory) return S_OK; return CLASS_E_CLASSNOTAVAILABLE; diff --git a/dlls/windows.storage/private.h b/dlls/windows.storage/private.h index 62c9d226d07..6387288a399 100644 --- a/dlls/windows.storage/private.h +++ b/dlls/windows.storage/private.h @@ -40,6 +40,7 @@ #include "windows.storage.streams.h" extern IActivationFactory *random_access_stream_reference_factory; +extern IActivationFactory *memory_stream_activation_factory; #define DEFINE_IINSPECTABLE_( pfx, iface_type, impl_type, impl_from, iface_mem, expr ) \ static inline impl_type *impl_from( iface_type *iface ) \ diff --git a/dlls/windows.storage/streams.c b/dlls/windows.storage/streams.c index f2499062778..4a834312eaa 100644 --- a/dlls/windows.storage/streams.c +++ b/dlls/windows.storage/streams.c @@ -1,6 +1,7 @@ /* WinRT Windows.Storage.Streams Implementation * * Copyright (C) 2025 Mohamad Al-Jaf + * Copyright 2026 Conor McCarthy for CodeWeavers * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -164,3 +165,475 @@ static struct random_access_stream_reference_statics random_access_stream_refere }; IActivationFactory *random_access_stream_reference_factory = &random_access_stream_reference_statics.IActivationFactory_iface; + +/* + * InMemoryRandomAccessStream + */ + +struct memory_stream +{ + IRandomAccessStream IRandomAccessStream_iface; + IInputStream IInputStream_iface; + IOutputStream IOutputStream_iface; + IClosable IClosable_iface; + LONG ref; +}; + +static inline struct memory_stream *impl_from_IRandomAccessStream( IRandomAccessStream *iface ) +{ + return CONTAINING_RECORD( iface, struct memory_stream, IRandomAccessStream_iface ); +} + +static HRESULT WINAPI memory_stream_random_access_QueryInterface( IRandomAccessStream *iface, REFIID iid, void **out ) +{ + struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); + + TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid( iid ), out ); + + if (IsEqualGUID( iid, &IID_IUnknown ) + || IsEqualGUID( iid, &IID_IInspectable ) + || IsEqualGUID( iid, &IID_IRandomAccessStream )) + { + *out = iface; + } + else if (IsEqualGUID( iid, &IID_IInputStream )) + { + *out = &impl->IInputStream_iface; + } + else if (IsEqualGUID( iid, &IID_IOutputStream )) + { + *out = &impl->IOutputStream_iface; + } + else if (IsEqualGUID( iid, &IID_IClosable )) + { + *out = &impl->IClosable_iface; + } + else + { + WARN( "%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid( iid )); + *out = NULL; + return E_NOINTERFACE; + } + + IRandomAccessStream_AddRef( iface ); + return S_OK; +} + +static ULONG WINAPI memory_stream_random_access_AddRef( IRandomAccessStream *iface ) +{ + struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); + ULONG ref = InterlockedIncrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + return ref; +} + +static ULONG WINAPI memory_stream_random_access_Release( IRandomAccessStream *iface ) +{ + struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); + ULONG ref = InterlockedDecrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + return ref; +} + +static HRESULT WINAPI memory_stream_random_access_GetIids( IRandomAccessStream *iface, ULONG *iid_count, IID **iids ) +{ + FIXME( "iface %p, iid_count %p, iids %p stub!\n", iface, iid_count, iids ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_random_access_GetRuntimeClassName( IRandomAccessStream *iface, HSTRING *class_name ) +{ + FIXME( "iface %p, class_name %p stub!\n", iface, class_name ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_random_access_GetTrustLevel( IRandomAccessStream *iface, TrustLevel *trust_level ) +{ + FIXME( "iface %p, trust_level %p stub!\n", iface, trust_level ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_random_access_get_Size( IRandomAccessStream *iface, UINT64 *value ) +{ + FIXME( "iface %p, value %p stub!\n", iface, value ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_random_access_put_Size( IRandomAccessStream *iface, UINT64 value ) +{ + FIXME( "iface %p, value %I64u stub!\n", iface, value ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_random_access_GetInputStreamAt( IRandomAccessStream *iface, UINT64 position, + IInputStream **stream ) +{ + FIXME( "iface %p, position %I64u, stream %p stub!\n", iface, position, stream ); + + *stream = NULL; + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_random_access_GetOutputStreamAt( IRandomAccessStream *iface, UINT64 position, + IOutputStream **stream ) +{ + FIXME( "iface %p, position %I64u, stream %p stub!\n", iface, position, stream ); + + *stream = NULL; + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_random_access_get_Position( IRandomAccessStream *iface, UINT64 *value ) +{ + FIXME( "iface %p, value %p stub!\n", iface, value ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_random_access_Seek( IRandomAccessStream *iface, UINT64 position ) +{ + FIXME( "iface %p, position %I64u stub!\n", iface, position ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_random_access_CloneStream( IRandomAccessStream *iface, IRandomAccessStream **stream ) +{ + FIXME( "iface %p, stream %p stub!\n", iface, stream ); + + *stream = NULL; + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_random_access_get_CanRead( IRandomAccessStream *iface, BOOLEAN *value ) +{ + TRACE( "iface %p, value %p.\n", iface, value ); + + *value = TRUE; + return S_OK; +} + +static HRESULT WINAPI memory_stream_random_access_get_CanWrite( IRandomAccessStream *iface, BOOLEAN *value ) +{ + TRACE( "iface %p, value %p.\n", iface, value ); + + *value = TRUE; + return S_OK; +} + +static const struct IRandomAccessStreamVtbl memory_stream_random_access_vtbl = +{ + /* IUnknown methods */ + memory_stream_random_access_QueryInterface, + memory_stream_random_access_AddRef, + memory_stream_random_access_Release, + /* IInspectable methods */ + memory_stream_random_access_GetIids, + memory_stream_random_access_GetRuntimeClassName, + memory_stream_random_access_GetTrustLevel, + /* IRandomAccessStream methods */ + memory_stream_random_access_get_Size, + memory_stream_random_access_put_Size, + memory_stream_random_access_GetInputStreamAt, + memory_stream_random_access_GetOutputStreamAt, + memory_stream_random_access_get_Position, + memory_stream_random_access_Seek, + memory_stream_random_access_CloneStream, + memory_stream_random_access_get_CanRead, + memory_stream_random_access_get_CanWrite, +}; + +DEFINE_IINSPECTABLE( memory_stream_closable, IClosable, struct memory_stream, IRandomAccessStream_iface ) + +static HRESULT WINAPI memory_stream_closable_Close( IClosable *iface ) +{ + FIXME( "iface %p stub!\n", iface ); + return E_NOTIMPL; +} + +static const struct IClosableVtbl memory_stream_closable_vtbl = +{ + /* IUnknown methods */ + memory_stream_closable_QueryInterface, + memory_stream_closable_AddRef, + memory_stream_closable_Release, + /* IInspectable methods */ + memory_stream_closable_GetIids, + memory_stream_closable_GetRuntimeClassName, + memory_stream_closable_GetTrustLevel, + /* IClosable methods */ + memory_stream_closable_Close, +}; + +static inline struct memory_stream *impl_from_IInputStream( IInputStream *iface ) +{ + return CONTAINING_RECORD( iface, struct memory_stream, IInputStream_iface ); +} + +static HRESULT WINAPI memory_stream_input_QueryInterface( IInputStream *iface, REFIID iid, void **out ) +{ + struct memory_stream *impl = impl_from_IInputStream( iface ); + TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid( iid ), out ); + return IRandomAccessStream_QueryInterface( &impl->IRandomAccessStream_iface, iid, out ); +} + +static ULONG WINAPI memory_stream_input_AddRef( IInputStream *iface ) +{ + struct memory_stream *impl = impl_from_IInputStream( iface ); + TRACE( "iface %p.\n", iface ); + return IRandomAccessStream_AddRef( &impl->IRandomAccessStream_iface ); +} + +static ULONG WINAPI memory_stream_input_Release( IInputStream *iface ) +{ + struct memory_stream *impl = impl_from_IInputStream( iface ); + TRACE( "iface %p.\n", iface ); + return IRandomAccessStream_Release( &impl->IRandomAccessStream_iface ); +} + +static HRESULT WINAPI memory_stream_input_GetIids( IInputStream *iface, ULONG *iid_count, IID **iids ) +{ + FIXME( "iface %p, iid_count %p, iids %p stub!\n", iface, iid_count, iids ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_input_GetRuntimeClassName( IInputStream *iface, HSTRING *class_name ) +{ + FIXME( "iface %p, class_name %p stub!\n", iface, class_name ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_input_GetTrustLevel( IInputStream *iface, TrustLevel *trust_level ) +{ + FIXME( "iface %p, trust_level %p stub!\n", iface, trust_level ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_input_ReadAsync( IInputStream *iface, IBuffer *buffer, UINT32 count, + InputStreamOptions options, IAsyncOperationWithProgress_IBuffer_UINT32 **operation ) +{ + FIXME( "iface %p, buffer %p, count %u, options %d, operation %p stub!\n", iface, buffer, count, options, operation ); + + *operation = NULL; + return E_NOTIMPL; +} + +static const struct IInputStreamVtbl memory_stream_input_vtbl = +{ + /* IUnknown methods */ + memory_stream_input_QueryInterface, + memory_stream_input_AddRef, + memory_stream_input_Release, + /* IInspectable methods */ + memory_stream_input_GetIids, + memory_stream_input_GetRuntimeClassName, + memory_stream_input_GetTrustLevel, + /* IInputStream methods */ + memory_stream_input_ReadAsync +}; + +static inline struct memory_stream *impl_from_IOutputStream( IOutputStream *iface ) +{ + return CONTAINING_RECORD( iface, struct memory_stream, IOutputStream_iface ); +} + +static HRESULT WINAPI memory_stream_output_QueryInterface( IOutputStream *iface, REFIID iid, void **out ) +{ + struct memory_stream *impl = impl_from_IOutputStream( iface ); + TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid( iid ), out ); + return IRandomAccessStream_QueryInterface( &impl->IRandomAccessStream_iface, iid, out ); +} + +static ULONG WINAPI memory_stream_output_AddRef( IOutputStream *iface ) +{ + struct memory_stream *impl = impl_from_IOutputStream( iface ); + TRACE( "iface %p.\n", iface ); + return IRandomAccessStream_AddRef( &impl->IRandomAccessStream_iface ); +} + +static ULONG WINAPI memory_stream_output_Release( IOutputStream *iface ) +{ + struct memory_stream *impl = impl_from_IOutputStream( iface ); + TRACE( "iface %p.\n", iface ); + return IRandomAccessStream_Release( &impl->IRandomAccessStream_iface ); +} + +static HRESULT WINAPI memory_stream_output_GetIids( IOutputStream *iface, ULONG *iid_count, IID **iids ) +{ + FIXME( "iface %p, iid_count %p, iids %p stub!\n", iface, iid_count, iids ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_output_GetRuntimeClassName( IOutputStream *iface, HSTRING *class_name ) +{ + FIXME( "iface %p, class_name %p stub!\n", iface, class_name ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_output_GetTrustLevel( IOutputStream *iface, TrustLevel *trust_level ) +{ + FIXME( "iface %p, trust_level %p stub!\n", iface, trust_level ); + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_output_WriteAsync( IOutputStream *iface, IBuffer *buffer, + IAsyncOperationWithProgress_UINT32_UINT32 **operation ) +{ + FIXME( "iface %p, buffer %p, operation %p stub!\n", iface, buffer, operation ); + + *operation = NULL; + return E_NOTIMPL; +} + +static HRESULT WINAPI memory_stream_output_FlushAsync( IOutputStream *iface, IAsyncOperation_boolean **operation ) +{ + FIXME( "iface %p, operation %p stub!\n", iface, operation ); + + *operation = NULL; + return E_NOTIMPL; +} + +static const struct IOutputStreamVtbl memory_stream_output_vtbl = +{ + /* IUnknown methods */ + memory_stream_output_QueryInterface, + memory_stream_output_AddRef, + memory_stream_output_Release, + /* IInspectable methods */ + memory_stream_output_GetIids, + memory_stream_output_GetRuntimeClassName, + memory_stream_output_GetTrustLevel, + /* IOutputStream methods */ + memory_stream_output_WriteAsync, + memory_stream_output_FlushAsync, +}; + +static HRESULT memory_stream_create( IRandomAccessStream **out ) +{ + struct memory_stream *impl; + + TRACE( "out %p.\n", out ); + + if (!(impl = calloc( 1, sizeof(*impl) ))) + { + *out = NULL; + return E_OUTOFMEMORY; + } + + impl->ref = 1; + impl->IRandomAccessStream_iface.lpVtbl = &memory_stream_random_access_vtbl; + impl->IInputStream_iface.lpVtbl = &memory_stream_input_vtbl; + impl->IOutputStream_iface.lpVtbl = &memory_stream_output_vtbl; + impl->IClosable_iface.lpVtbl = &memory_stream_closable_vtbl; + + *out = &impl->IRandomAccessStream_iface; + return S_OK; +} + +struct memory_stream_factory +{ + IActivationFactory IActivationFactory_iface; + LONG ref; +}; + +static inline struct memory_stream_factory *impl_memory_stream_factory_from_IActivationFactory( IActivationFactory *iface ) +{ + return CONTAINING_RECORD( iface, struct memory_stream_factory, IActivationFactory_iface ); +} + +static HRESULT STDMETHODCALLTYPE memory_stream_activation_factory_QueryInterface( IActivationFactory *iface, REFIID iid, + void **out ) +{ + TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid( iid ), out ); + + if (IsEqualGUID( iid, &IID_IUnknown ) + || IsEqualGUID( iid, &IID_IInspectable ) + || IsEqualGUID( iid, &IID_IAgileObject ) + || IsEqualGUID( iid, &IID_IActivationFactory )) + { + IUnknown_AddRef( iface ); + *out = iface; + return S_OK; + } + + WARN( "%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid( iid )); + *out = NULL; + return E_NOINTERFACE; +} + +static ULONG STDMETHODCALLTYPE memory_stream_activation_factory_AddRef( IActivationFactory *iface ) +{ + struct memory_stream_factory *impl = impl_memory_stream_factory_from_IActivationFactory( iface ); + ULONG ref = InterlockedIncrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + return ref; +} + +static ULONG STDMETHODCALLTYPE memory_stream_activation_factory_Release( IActivationFactory *iface ) +{ + struct memory_stream_factory *impl = impl_memory_stream_factory_from_IActivationFactory( iface ); + ULONG ref = InterlockedDecrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + return ref; +} + +static HRESULT STDMETHODCALLTYPE memory_stream_activation_factory_GetIids( IActivationFactory *iface, ULONG *iid_count, + IID **iids ) +{ + FIXME( "iface %p, iid_count %p, iids %p stub!\n", iface, iid_count, iids ); + return E_NOTIMPL; +} + +static HRESULT STDMETHODCALLTYPE memory_stream_activation_factory_GetRuntimeClassName( IActivationFactory *iface, + HSTRING *class_name ) +{ + FIXME( "iface %p, class_name %p stub!\n", iface, class_name ); + return E_NOTIMPL; +} + +static HRESULT STDMETHODCALLTYPE memory_stream_activation_factory_GetTrustLevel( IActivationFactory *iface, + TrustLevel *trust_level ) +{ + FIXME( "iface %p, trust_level %p stub!\n", iface, trust_level ); + return E_NOTIMPL; +} + +static HRESULT STDMETHODCALLTYPE memory_stream_activation_factory_ActivateInstance( IActivationFactory *iface, + IInspectable **instance ) +{ + IRandomAccessStream *out; + HRESULT hr; + + TRACE( "iface %p, instance %p.\n", iface, instance ); + + *instance = NULL; + + if (SUCCEEDED(hr = memory_stream_create( &out ))) + { + hr = IRandomAccessStream_QueryInterface( out, &IID_IInspectable, (void **)instance ); + IRandomAccessStream_Release( out ); + if (SUCCEEDED(hr)) + TRACE( "created InMemoryRandomAccessStream %p.\n", *instance ); + } + + return hr; +} + +static const struct IActivationFactoryVtbl memory_stream_activation_factory_vtbl = +{ + memory_stream_activation_factory_QueryInterface, + memory_stream_activation_factory_AddRef, + memory_stream_activation_factory_Release, + /* IInspectable methods */ + memory_stream_activation_factory_GetIids, + memory_stream_activation_factory_GetRuntimeClassName, + memory_stream_activation_factory_GetTrustLevel, + /* IActivationFactory methods */ + memory_stream_activation_factory_ActivateInstance, +}; + +struct memory_stream_factory memory_stream_factory = +{ + {&memory_stream_activation_factory_vtbl}, + 1 +}; + +IActivationFactory *memory_stream_activation_factory = &memory_stream_factory.IActivationFactory_iface; diff --git a/dlls/windows.storage/tests/storage.c b/dlls/windows.storage/tests/storage.c index ae842ab1f78..56d14b37d73 100644 --- a/dlls/windows.storage/tests/storage.c +++ b/dlls/windows.storage/tests/storage.c @@ -145,7 +145,6 @@ static HRESULT get_activation_factory( const WCHAR *name, IActivationFactory **f ok( hr == S_OK, "got hr %#lx.\n", hr ); hr = RoGetActivationFactory( str, &IID_IActivationFactory, (void **)factory ); WindowsDeleteString( str ); - todo_wine ok( hr == S_OK || broken( hr == REGDB_E_CLASSNOTREG ), "got hr %#lx.\n", hr ); if (hr == REGDB_E_CLASSNOTREG) -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/11297
From: Conor McCarthy <cmccarthy@codeweavers.com> --- dlls/windows.storage/streams.c | 54 ++++++++++++++++++++++++++-- dlls/windows.storage/tests/storage.c | 8 ----- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/dlls/windows.storage/streams.c b/dlls/windows.storage/streams.c index 4a834312eaa..7edb9eedf04 100644 --- a/dlls/windows.storage/streams.c +++ b/dlls/windows.storage/streams.c @@ -177,6 +177,8 @@ struct memory_stream IOutputStream IOutputStream_iface; IClosable IClosable_iface; LONG ref; + + BOOL closed; }; static inline struct memory_stream *impl_from_IRandomAccessStream( IRandomAccessStream *iface ) @@ -255,13 +257,28 @@ static HRESULT WINAPI memory_stream_random_access_GetTrustLevel( IRandomAccessSt static HRESULT WINAPI memory_stream_random_access_get_Size( IRandomAccessStream *iface, UINT64 *value ) { + struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); + FIXME( "iface %p, value %p stub!\n", iface, value ); + + if (impl->closed) + { + *value = 0; + return RO_E_CLOSED; + } + return E_NOTIMPL; } static HRESULT WINAPI memory_stream_random_access_put_Size( IRandomAccessStream *iface, UINT64 value ) { + struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); + FIXME( "iface %p, value %I64u stub!\n", iface, value ); + + if (impl->closed) + return RO_E_CLOSED; + return E_NOTIMPL; } @@ -285,13 +302,22 @@ static HRESULT WINAPI memory_stream_random_access_GetOutputStreamAt( IRandomAcce static HRESULT WINAPI memory_stream_random_access_get_Position( IRandomAccessStream *iface, UINT64 *value ) { + struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); + FIXME( "iface %p, value %p stub!\n", iface, value ); - return E_NOTIMPL; + + return impl->closed ? RO_E_CLOSED : E_NOTIMPL; } static HRESULT WINAPI memory_stream_random_access_Seek( IRandomAccessStream *iface, UINT64 position ) { + struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); + FIXME( "iface %p, position %I64u stub!\n", iface, position ); + + if (impl->closed) + return RO_E_CLOSED; + return E_NOTIMPL; } @@ -345,8 +371,12 @@ DEFINE_IINSPECTABLE( memory_stream_closable, IClosable, struct memory_stream, IR static HRESULT WINAPI memory_stream_closable_Close( IClosable *iface ) { - FIXME( "iface %p stub!\n", iface ); - return E_NOTIMPL; + struct memory_stream *impl = CONTAINING_RECORD( iface, struct memory_stream, IClosable_iface ); + + TRACE( "iface %p.\n", iface ); + + impl->closed = TRUE; + return S_OK; } static const struct IClosableVtbl memory_stream_closable_vtbl = @@ -410,9 +440,15 @@ static HRESULT WINAPI memory_stream_input_GetTrustLevel( IInputStream *iface, Tr static HRESULT WINAPI memory_stream_input_ReadAsync( IInputStream *iface, IBuffer *buffer, UINT32 count, InputStreamOptions options, IAsyncOperationWithProgress_IBuffer_UINT32 **operation ) { + struct memory_stream *impl = impl_from_IInputStream( iface ); + FIXME( "iface %p, buffer %p, count %u, options %d, operation %p stub!\n", iface, buffer, count, options, operation ); *operation = NULL; + + if (impl->closed) + return RO_E_CLOSED; + return E_NOTIMPL; } @@ -477,17 +513,29 @@ static HRESULT WINAPI memory_stream_output_GetTrustLevel( IOutputStream *iface, static HRESULT WINAPI memory_stream_output_WriteAsync( IOutputStream *iface, IBuffer *buffer, IAsyncOperationWithProgress_UINT32_UINT32 **operation ) { + struct memory_stream *impl = impl_from_IOutputStream( iface ); + FIXME( "iface %p, buffer %p, operation %p stub!\n", iface, buffer, operation ); *operation = NULL; + + if (impl->closed) + return RO_E_CLOSED; + return E_NOTIMPL; } static HRESULT WINAPI memory_stream_output_FlushAsync( IOutputStream *iface, IAsyncOperation_boolean **operation ) { + struct memory_stream *impl = impl_from_IOutputStream( iface ); + FIXME( "iface %p, operation %p stub!\n", iface, operation ); *operation = NULL; + + if (impl->closed) + return RO_E_CLOSED; + return E_NOTIMPL; } diff --git a/dlls/windows.storage/tests/storage.c b/dlls/windows.storage/tests/storage.c index 56d14b37d73..71aa00adb19 100644 --- a/dlls/windows.storage/tests/storage.c +++ b/dlls/windows.storage/tests/storage.c @@ -531,36 +531,28 @@ static void test_InMemoryRandomAccessStream(void) hr = IOutputStream_QueryInterface( output_stream, &IID_IClosable, (void **)&closable ); ok( hr == S_OK, "got hr %#lx.\n", hr ); hr = IClosable_Close( closable ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); IClosable_Release( closable ); write_op = (void *)0xdeadbeef; hr = IOutputStream_WriteAsync( output_stream, buffer, &write_op ); - todo_wine ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); ok( !write_op, "got operation %p.\n", write_op ); operation = (void *)0xdeadbeef; hr = IInputStream_ReadAsync( input_stream, buffer, 0, 0, &operation ); - todo_wine ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); ok( !operation, "got operation %p.\n", operation ); hr = IRandomAccessStream_Seek( in_memory_stream, 0 ); - todo_wine ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); value64 = uint64_value; hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); - todo_wine ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); - todo_wine ok( value64 == 0, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_put_Size( in_memory_stream, 1 ); - todo_wine ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); value64 = uint64_value; hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); - todo_wine ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); todo_wine ok( value64 == 0x100000, "got pos %I64u.\n", value64 ); -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/11297
From: Conor McCarthy <cmccarthy@codeweavers.com> --- dlls/windows.storage/Makefile.in | 2 + dlls/windows.storage/async.c | 531 +++++++++++++++++++++++++ dlls/windows.storage/async_private.idl | 46 +++ dlls/windows.storage/private.h | 7 + dlls/windows.storage/streams.c | 77 +++- dlls/windows.storage/tests/storage.c | 3 - 6 files changed, 661 insertions(+), 5 deletions(-) create mode 100644 dlls/windows.storage/async.c create mode 100644 dlls/windows.storage/async_private.idl diff --git a/dlls/windows.storage/Makefile.in b/dlls/windows.storage/Makefile.in index bb446a2be99..adc8597dd11 100644 --- a/dlls/windows.storage/Makefile.in +++ b/dlls/windows.storage/Makefile.in @@ -2,6 +2,8 @@ MODULE = windows.storage.dll IMPORTS = shell32 combase SOURCES = \ + async.c \ + async_private.idl \ classes.idl \ main.c \ streams.c diff --git a/dlls/windows.storage/async.c b/dlls/windows.storage/async.c new file mode 100644 index 00000000000..090ae84fac9 --- /dev/null +++ b/dlls/windows.storage/async.c @@ -0,0 +1,531 @@ +/* WinRT IAsync* implementation + * + * Copyright 2022 Bernhard Kölbl for CodeWeavers + * Copyright 2022 Rémi Bernon for CodeWeavers + * Copyright 2026 Conor McCarthy for CodeWeavers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#define WIDL_using_Wine_Internal +#include "private.h" +#include "initguid.h" + +#include "wine/debug.h" + +WINE_DEFAULT_DEBUG_CHANNEL(storage); + +#define Closed 4 +#define HANDLER_NOT_SET ((void *)~(ULONG_PTR)0) + +struct async_info +{ + IAsyncInfoImpl IAsyncInfoImpl_iface; + IAsyncInfo IAsyncInfo_iface; + IInspectable *IInspectable_outer; + LONG ref; + + async_operation_callback callback; + TP_WORK *async_run_work; + IUnknown *invoker; + IUnknown *param; + + CRITICAL_SECTION cs; + IAsyncOperationCompletedHandlerImpl *handler; + PROPVARIANT result; + AsyncStatus status; + HRESULT hr; +}; + +static inline struct async_info *impl_from_IAsyncInfoImpl( IAsyncInfoImpl *iface ) +{ + return CONTAINING_RECORD( iface, struct async_info, IAsyncInfoImpl_iface ); +} + +static HRESULT WINAPI async_impl_QueryInterface( IAsyncInfoImpl *iface, REFIID iid, void **out ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + + TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid( iid ), out ); + + if (IsEqualGUID( iid, &IID_IUnknown ) || + IsEqualGUID( iid, &IID_IInspectable ) || + IsEqualGUID( iid, &IID_IAgileObject ) || + IsEqualGUID( iid, &IID_IAsyncInfoImpl )) + { + IInspectable_AddRef( (*out = &impl->IAsyncInfoImpl_iface) ); + return S_OK; + } + + if (IsEqualGUID( iid, &IID_IAsyncInfo )) + { + IInspectable_AddRef( (*out = &impl->IAsyncInfo_iface) ); + return S_OK; + } + + FIXME( "%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid( iid ) ); + *out = NULL; + return E_NOINTERFACE; +} + +static ULONG WINAPI async_impl_AddRef( IAsyncInfoImpl *iface ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + ULONG ref = InterlockedIncrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + return ref; +} + +static ULONG WINAPI async_impl_Release( IAsyncInfoImpl *iface ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + ULONG ref = InterlockedDecrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + + if (!ref) + { + if (impl->handler && impl->handler != HANDLER_NOT_SET) IAsyncOperationCompletedHandlerImpl_Release( impl->handler ); + IAsyncInfo_Close( &impl->IAsyncInfo_iface ); + if (impl->param) IUnknown_Release( impl->param ); + if (impl->invoker) IUnknown_Release( impl->invoker ); + PropVariantClear( &impl->result ); + impl->cs.DebugInfo->Spare[0] = 0; + DeleteCriticalSection( &impl->cs ); + free( impl ); + } + + return ref; +} + +static HRESULT WINAPI async_impl_put_Completed( IAsyncInfoImpl *iface, IAsyncOperationCompletedHandlerImpl *handler ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p, handler %p.\n", iface, handler ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) hr = E_ILLEGAL_METHOD_CALL; + else if (impl->handler != HANDLER_NOT_SET) hr = E_ILLEGAL_DELEGATE_ASSIGNMENT; + else if ((impl->handler = handler)) + { + IAsyncOperationCompletedHandlerImpl_AddRef( impl->handler ); + + if (impl->status > Started) + { + IInspectable *operation = impl->IInspectable_outer; + AsyncStatus status = impl->status; + impl->handler = NULL; /* Prevent concurrent invoke. */ + LeaveCriticalSection( &impl->cs ); + + IAsyncOperationCompletedHandlerImpl_Invoke( handler, operation, status ); + IAsyncOperationCompletedHandlerImpl_Release( handler ); + + return S_OK; + } + } + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_impl_get_Completed( IAsyncInfoImpl *iface, IAsyncOperationCompletedHandlerImpl **handler ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p, handler %p.\n", iface, handler ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) hr = E_ILLEGAL_METHOD_CALL; + if (impl->handler == NULL || impl->handler == HANDLER_NOT_SET) *handler = NULL; + else IAsyncOperationCompletedHandlerImpl_AddRef( (*handler = impl->handler) ); + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_impl_get_Result( IAsyncInfoImpl *iface, PROPVARIANT *result ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + HRESULT hr = E_ILLEGAL_METHOD_CALL; + + TRACE( "iface %p, result %p.\n", iface, result ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Completed || impl->status == Error) + { + PropVariantCopy( result, &impl->result ); + hr = impl->hr; + } + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static BOOL async_info_complete( struct async_info *impl, BOOL called_async ) +{ + IInspectable *operation = impl->IInspectable_outer; + PROPVARIANT result = {0}; + HRESULT hr; + + hr = impl->callback( impl->invoker, impl->param, &result, called_async ); + if (!called_async && hr == STATUS_PENDING) return FALSE; + + EnterCriticalSection( &impl->cs ); + if (impl->status != Closed) impl->status = FAILED(hr) ? Error : Completed; + PropVariantCopy( &impl->result, &result ); + impl->hr = hr; + + if (impl->handler != NULL && impl->handler != HANDLER_NOT_SET) + { + IAsyncOperationCompletedHandlerImpl *handler = impl->handler; + AsyncStatus status = impl->status; + impl->handler = NULL; /* Prevent concurrent invoke. */ + LeaveCriticalSection( &impl->cs ); + + IAsyncOperationCompletedHandlerImpl_Invoke( handler, operation, status ); + IAsyncOperationCompletedHandlerImpl_Release( handler ); + } + else LeaveCriticalSection( &impl->cs ); + + /* release refcount acquired in Start */ + IInspectable_Release( operation ); + + PropVariantClear( &result ); + return TRUE; +} + +static HRESULT WINAPI async_impl_Start( IAsyncInfoImpl *iface ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + + TRACE( "iface %p.\n", iface ); + + /* keep the async alive in the callback */ + IInspectable_AddRef( impl->IInspectable_outer ); + if (!async_info_complete( impl, FALSE )) SubmitThreadpoolWork( impl->async_run_work ); + + return S_OK; +} + +static const struct IAsyncInfoImplVtbl async_impl_vtbl = +{ + /* IUnknown methods */ + async_impl_QueryInterface, + async_impl_AddRef, + async_impl_Release, + /* IAsyncInfoImpl */ + async_impl_put_Completed, + async_impl_get_Completed, + async_impl_get_Result, + async_impl_Start, +}; + +DEFINE_IINSPECTABLE_OUTER( async_info, IAsyncInfo, struct async_info, IInspectable_outer ) + +static HRESULT WINAPI async_info_get_Id( IAsyncInfo *iface, UINT32 *id ) +{ + struct async_info *impl = impl_from_IAsyncInfo( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p, id %p.\n", iface, id ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) hr = E_ILLEGAL_METHOD_CALL; + *id = 1; + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_info_get_Status( IAsyncInfo *iface, AsyncStatus *status ) +{ + struct async_info *impl = impl_from_IAsyncInfo( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p, status %p.\n", iface, status ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) hr = E_ILLEGAL_METHOD_CALL; + *status = impl->status; + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_info_get_ErrorCode( IAsyncInfo *iface, HRESULT *error_code ) +{ + struct async_info *impl = impl_from_IAsyncInfo( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p, error_code %p.\n", iface, error_code ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) *error_code = hr = E_ILLEGAL_METHOD_CALL; + else *error_code = impl->hr; + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_info_Cancel( IAsyncInfo *iface ) +{ + struct async_info *impl = impl_from_IAsyncInfo( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p.\n", iface ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Closed) hr = E_ILLEGAL_METHOD_CALL; + else if (impl->status == Started) impl->status = Canceled; + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static HRESULT WINAPI async_info_Close( IAsyncInfo *iface ) +{ + struct async_info *impl = impl_from_IAsyncInfo( iface ); + HRESULT hr = S_OK; + + TRACE( "iface %p.\n", iface ); + + EnterCriticalSection( &impl->cs ); + if (impl->status == Started) + hr = E_ILLEGAL_STATE_CHANGE; + else if (impl->status != Closed) + { + CloseThreadpoolWork( impl->async_run_work ); + impl->async_run_work = NULL; + impl->status = Closed; + } + LeaveCriticalSection( &impl->cs ); + + return hr; +} + +static const struct IAsyncInfoVtbl async_info_vtbl = +{ + /* IUnknown methods */ + async_info_QueryInterface, + async_info_AddRef, + async_info_Release, + /* IInspectable methods */ + async_info_GetIids, + async_info_GetRuntimeClassName, + async_info_GetTrustLevel, + /* IAsyncInfo */ + async_info_get_Id, + async_info_get_Status, + async_info_get_ErrorCode, + async_info_Cancel, + async_info_Close, +}; + +static void CALLBACK async_info_callback( TP_CALLBACK_INSTANCE *instance, void *iface, TP_WORK *work ) +{ + struct async_info *impl = impl_from_IAsyncInfoImpl( iface ); + + async_info_complete( impl, TRUE ); +} + +static HRESULT async_info_create( IUnknown *invoker, IUnknown *param, async_operation_callback callback, + IInspectable *outer, IAsyncInfoImpl **out ) +{ + struct async_info *impl; + HRESULT hr; + + if (!(impl = calloc( 1, sizeof(struct async_info) ))) return E_OUTOFMEMORY; + impl->IAsyncInfoImpl_iface.lpVtbl = &async_impl_vtbl; + impl->IAsyncInfo_iface.lpVtbl = &async_info_vtbl; + impl->IInspectable_outer = outer; + impl->ref = 1; + + impl->callback = callback; + impl->handler = HANDLER_NOT_SET; + impl->status = Started; + if (!(impl->async_run_work = CreateThreadpoolWork( async_info_callback, &impl->IAsyncInfoImpl_iface, NULL ))) + { + hr = HRESULT_FROM_WIN32( GetLastError() ); + free( impl ); + return hr; + } + + if ((impl->invoker = invoker)) IUnknown_AddRef( impl->invoker ); + if ((impl->param = param)) IUnknown_AddRef( impl->param ); + + InitializeCriticalSectionEx( &impl->cs, 0, RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO ); + impl->cs.DebugInfo->Spare[0] = (DWORD_PTR)( __FILE__ ": async_info.cs" ); + + *out = &impl->IAsyncInfoImpl_iface; + return S_OK; +} + +#define DEFINE_IASYNCOPERATIONWITHPROGRESS( iface_type, progress_type, completed_type, cs_type_str, impl_type, \ + result_type, propvar_type, propvar_name, creation_fn ) \ +static inline struct impl_type *impl_from_##iface_type( iface_type *iface ) \ +{ \ + return CONTAINING_RECORD( iface, struct impl_type, iface_type##_iface ); \ +} \ +static HRESULT WINAPI impl_type##_QueryInterface( iface_type *iface, REFIID iid, void **out ) \ +{ \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + \ + TRACE( "iface %p, iid %s, out %p.\n", iface, debugstr_guid( iid ), out ); \ + \ + if (IsEqualGUID( iid, &IID_IUnknown ) || \ + IsEqualGUID( iid, &IID_IInspectable ) || \ + IsEqualGUID( iid, &IID_IAgileObject ) || \ + IsEqualGUID( iid, &IID_##iface_type )) \ + { \ + IInspectable_AddRef( (*out = &impl->iface_type##_iface) ); \ + return S_OK; \ + } \ + \ + return IAsyncInfoImpl_QueryInterface( impl->IAsyncInfoImpl_inner, iid, out ); \ +} \ +static ULONG WINAPI impl_type##_AddRef( iface_type *iface ) \ +{ \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + ULONG ref = InterlockedIncrement( &impl->ref ); \ + TRACE( "iface %p, ref %lu.\n", iface, ref ); \ + return ref; \ +} \ +static ULONG WINAPI impl_type##_Release( iface_type *iface ) \ +{ \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + ULONG ref = InterlockedDecrement( &impl->ref ); \ + TRACE( "iface %p, ref %lu.\n", iface, ref ); \ + \ + if (!ref) \ + { \ + /* guard against re-entry if inner releases an outer iface */ \ + InterlockedIncrement( &impl->ref ); \ + IAsyncInfoImpl_Release( impl->IAsyncInfoImpl_inner ); \ + free( impl ); \ + } \ + \ + return ref; \ +} \ +static HRESULT WINAPI impl_type##_GetIids( iface_type *iface, ULONG *iid_count, IID **iids ) \ +{ \ + FIXME( "iface %p, iid_count %p, iids %p stub!\n", iface, iid_count, iids ); \ + return E_NOTIMPL; \ +} \ +static HRESULT WINAPI impl_type##_GetRuntimeClassName( iface_type *iface, HSTRING *class_name ) \ +{ \ + return WindowsCreateString( L"Windows.Foundation.IAsyncOperationWithProgress`2<"cs_type_str">", \ + ARRAY_SIZE(L"Windows.Foundation.IAsyncOperationWithProgress`2<"cs_type_str">"), \ + class_name ); \ +} \ +static HRESULT WINAPI impl_type##_GetTrustLevel( iface_type *iface, TrustLevel *trust_level ) \ +{ \ + FIXME( "iface %p, trust_level %p stub!\n", iface, trust_level ); \ + return E_NOTIMPL; \ +} \ +static HRESULT WINAPI impl_type##_put_Progress( iface_type *iface, progress_type *handler ) \ +{ \ + FIXME( "iface %p, handler %p stub!\n", iface, handler ); \ + return E_NOTIMPL; \ +} \ +static HRESULT WINAPI impl_type##_get_Progress( iface_type *iface, progress_type **handler ) \ +{ \ + FIXME( "iface %p, handler %p stub!\n", iface, handler ); \ + return E_NOTIMPL; \ +} \ +static HRESULT WINAPI impl_type##_put_Completed( iface_type *iface, completed_type *typed_handler ) \ +{ \ + IAsyncOperationCompletedHandlerImpl *handler = (IAsyncOperationCompletedHandlerImpl *)typed_handler; \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + TRACE( "iface %p, handler %p.\n", iface, handler ); \ + return IAsyncInfoImpl_put_Completed( impl->IAsyncInfoImpl_inner, handler ); \ +} \ +static HRESULT WINAPI impl_type##_get_Completed( iface_type *iface, completed_type **typed_handler ) \ +{ \ + IAsyncOperationCompletedHandlerImpl **handler = (IAsyncOperationCompletedHandlerImpl **)typed_handler; \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + TRACE( "iface %p, handler %p.\n", iface, handler ); \ + return IAsyncInfoImpl_get_Completed( impl->IAsyncInfoImpl_inner, handler ); \ +} \ +static HRESULT WINAPI impl_type##_GetResults( iface_type *iface, result_type *results ) \ +{ \ + struct impl_type *impl = impl_from_##iface_type( iface ); \ + PROPVARIANT result = {.vt = propvar_type}; \ + HRESULT hr; \ + \ + TRACE( "iface %p, results %p.\n", iface, results ); \ + \ + hr = IAsyncInfoImpl_get_Result( impl->IAsyncInfoImpl_inner, &result ); \ + \ + *results = (result_type)result.propvar_name; \ + if (propvar_type == VT_UNKNOWN && result.punkVal) IUnknown_AddRef( result.punkVal ); \ + PropVariantClear( &result ); \ + return hr; \ +} \ +static const struct iface_type##Vtbl impl_type##_vtbl = \ +{ \ + /* IUnknown methods */ \ + impl_type##_QueryInterface, \ + impl_type##_AddRef, \ + impl_type##_Release, \ + /* IInspectable methods */ \ + impl_type##_GetIids, \ + impl_type##_GetRuntimeClassName, \ + impl_type##_GetTrustLevel, \ + /* IAsyncOperationWithProgress<result_type,prog_type> */ \ + impl_type##_put_Progress, \ + impl_type##_get_Progress, \ + impl_type##_put_Completed, \ + impl_type##_get_Completed, \ + impl_type##_GetResults, \ +}; \ +HRESULT creation_fn( IUnknown *invoker, IUnknown *param, \ + async_operation_callback callback, iface_type **out ) \ +{ \ + struct impl_type *impl; \ + HRESULT hr; \ + \ + *out = NULL; \ + if (!(impl = calloc( 1, sizeof(*impl) ))) return E_OUTOFMEMORY; \ + impl->iface_type##_iface.lpVtbl = &impl_type##_vtbl; \ + impl->ref = 1; \ + \ + if (FAILED(hr = async_info_create( invoker, param, callback, \ + (IInspectable *)&impl->iface_type##_iface, &impl->IAsyncInfoImpl_inner )) \ + || FAILED(hr = IAsyncInfoImpl_Start( impl->IAsyncInfoImpl_inner ))) \ + { \ + TRACE("hr %#lx\n", hr ); \ + if (impl->IAsyncInfoImpl_inner) IAsyncInfoImpl_Release( impl->IAsyncInfoImpl_inner ); \ + free( impl ); \ + return hr; \ + } \ + \ + *out = &impl->iface_type##_iface; \ + TRACE( "created IAsyncOperationWithProgress %p\n", *out ); \ + return S_OK; \ +} \ + +struct async_uint32_uint32 +{ + IAsyncOperationWithProgress_UINT32_UINT32 IAsyncOperationWithProgress_UINT32_UINT32_iface; + IAsyncInfoImpl *IAsyncInfoImpl_inner; + LONG ref; +}; + +DEFINE_IASYNCOPERATIONWITHPROGRESS( IAsyncOperationWithProgress_UINT32_UINT32, \ + IAsyncOperationProgressHandler_UINT32_UINT32, \ + IAsyncOperationWithProgressCompletedHandler_UINT32_UINT32, \ + "UInt32,UInt32", async_uint32_uint32, UINT32, VT_UI4, ulVal, async_operation_uint32_uint32_create ) diff --git a/dlls/windows.storage/async_private.idl b/dlls/windows.storage/async_private.idl new file mode 100644 index 00000000000..40ea63ca4c1 --- /dev/null +++ b/dlls/windows.storage/async_private.idl @@ -0,0 +1,46 @@ +/* + * Copyright 2022 Rémi Bernon for CodeWeavers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#pragma makedep header + +#ifdef __WIDL__ +#pragma winrt ns_prefix +#endif + +import "propidl.idl"; +import "inspectable.idl"; +import "asyncinfo.idl"; +import "windowscontracts.idl"; + +namespace Wine.Internal { + /* type-pruning version of AsyncOperationCompletedHandlerImpl<T> */ + delegate HRESULT AsyncOperationCompletedHandlerImpl([in] IInspectable *async, [in] AsyncStatus status); + + [ + uuid(d81ab70d-82e0-481c-983d-401225d98a2c) + ] + interface IAsyncInfoImpl : IUnknown + { + [propput] HRESULT Completed([in] AsyncOperationCompletedHandlerImpl *handler); + [propget] HRESULT Completed([out, retval] AsyncOperationCompletedHandlerImpl **handler); + [propget] HRESULT Result([out, retval] PROPVARIANT *result); + HRESULT Start(); + } + + typedef HRESULT (*async_operation_callback)( IUnknown *invoker, IUnknown *param, PROPVARIANT *result, BOOL called_async ); +} diff --git a/dlls/windows.storage/private.h b/dlls/windows.storage/private.h index 6387288a399..fdf7ad48769 100644 --- a/dlls/windows.storage/private.h +++ b/dlls/windows.storage/private.h @@ -38,6 +38,11 @@ #define WIDL_using_Windows_Storage_Streams #include "windows.storage.h" #include "windows.storage.streams.h" +#include "robuffer.h" +#include "async_private.h" + +HRESULT async_operation_uint32_uint32_create( IUnknown *invoker, IUnknown *param, async_operation_callback callback, + IAsyncOperationWithProgress_UINT32_UINT32 **out ); extern IActivationFactory *random_access_stream_reference_factory; extern IActivationFactory *memory_stream_activation_factory; @@ -79,5 +84,7 @@ extern IActivationFactory *memory_stream_activation_factory; } #define DEFINE_IINSPECTABLE( pfx, iface_type, impl_type, base_iface ) \ DEFINE_IINSPECTABLE_( pfx, iface_type, impl_type, impl_from_##iface_type, iface_type##_iface, &impl->base_iface ) +#define DEFINE_IINSPECTABLE_OUTER( pfx, iface_type, impl_type, outer_iface ) \ + DEFINE_IINSPECTABLE_( pfx, iface_type, impl_type, impl_from_##iface_type, iface_type##_iface, impl->outer_iface ) #endif diff --git a/dlls/windows.storage/streams.c b/dlls/windows.storage/streams.c index 7edb9eedf04..292b3d25748 100644 --- a/dlls/windows.storage/streams.c +++ b/dlls/windows.storage/streams.c @@ -18,6 +18,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ +#include <assert.h> #include "private.h" WINE_DEFAULT_DEBUG_CHANNEL(storage); @@ -178,9 +179,35 @@ struct memory_stream IClosable IClosable_iface; LONG ref; + BYTE *buffer; + size_t capacity; + size_t size; + size_t pos; BOOL closed; }; +static HRESULT memory_stream_require_capacity( struct memory_stream *impl, size_t capacity ) +{ + BYTE *new_buffer; + + if (capacity <= impl->capacity) + return S_OK; + + capacity = max( capacity, impl->capacity + impl->capacity / 2u ); + capacity = max( capacity, 0x1000 ); + new_buffer = realloc( impl->buffer, capacity ); + + if (!new_buffer) + return HRESULT_FROM_WIN32( ERROR_DISK_FULL ); + + /* Zero memory for security and to silence address sanitisers */ + memset( &new_buffer[impl->capacity], 0, capacity - impl->capacity ); + impl->capacity = capacity; + impl->buffer = new_buffer; + + return S_OK; +} + static inline struct memory_stream *impl_from_IRandomAccessStream( IRandomAccessStream *iface ) { return CONTAINING_RECORD( iface, struct memory_stream, IRandomAccessStream_iface ); @@ -233,7 +260,12 @@ static ULONG WINAPI memory_stream_random_access_Release( IRandomAccessStream *if { struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); ULONG ref = InterlockedDecrement( &impl->ref ); + TRACE( "iface %p, ref %lu.\n", iface, ref ); + + if (!ref) + free( impl->buffer ); + return ref; } @@ -510,19 +542,60 @@ static HRESULT WINAPI memory_stream_output_GetTrustLevel( IOutputStream *iface, return E_NOTIMPL; } +static HRESULT memory_stream_output_async( IUnknown *invoker, IUnknown *param, PROPVARIANT *result, BOOL called_async ) +{ + struct memory_stream *impl = impl_from_IOutputStream( (IOutputStream *)invoker ); + IBuffer *buffer = (IBuffer *)param; + IBufferByteAccess *access; + size_t capacity; + UINT32 length; + HRESULT hr; + BYTE *data; + + assert( !called_async ); + + IBuffer_get_Length( buffer, &length ); + + if (!length) + return S_OK; + + capacity = impl->pos + length; + if (capacity < impl->pos || capacity < length) + return HRESULT_FROM_WIN32( ERROR_DISK_FULL ); + + if (FAILED(hr = memory_stream_require_capacity( impl, capacity ))) + return hr; + + IBuffer_QueryInterface( buffer, &IID_IBufferByteAccess, (void **)&access ); + IBufferByteAccess_Buffer( access, &data ); + IBufferByteAccess_Release( access ); + + memcpy( &impl->buffer[impl->pos], data, length ); + impl->pos += length; + impl->size = max( impl->size, impl->pos ); + + result->vt = VT_UI4; + result->ulVal = length; + + return S_OK; +} + static HRESULT WINAPI memory_stream_output_WriteAsync( IOutputStream *iface, IBuffer *buffer, IAsyncOperationWithProgress_UINT32_UINT32 **operation ) { struct memory_stream *impl = impl_from_IOutputStream( iface ); - FIXME( "iface %p, buffer %p, operation %p stub!\n", iface, buffer, operation ); + TRACE( "iface %p, buffer %p, operation %p.\n", iface, buffer, operation ); *operation = NULL; + if (!buffer) + return E_POINTER; + if (impl->closed) return RO_E_CLOSED; - return E_NOTIMPL; + return async_operation_uint32_uint32_create( (IUnknown *)iface, (IUnknown *)buffer, memory_stream_output_async, operation ); } static HRESULT WINAPI memory_stream_output_FlushAsync( IOutputStream *iface, IAsyncOperation_boolean **operation ) diff --git a/dlls/windows.storage/tests/storage.c b/dlls/windows.storage/tests/storage.c index 71aa00adb19..bade06c1ae6 100644 --- a/dlls/windows.storage/tests/storage.c +++ b/dlls/windows.storage/tests/storage.c @@ -243,9 +243,7 @@ static void output_stream_write_( unsigned int line, IOutputStream *output_strea hr = IBuffer_put_Length( buffer, count ); ok_(__FILE__, line)( hr == S_OK, "got hr %#lx.\n", hr ); hr = IOutputStream_WriteAsync( output_stream, buffer, &operation ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); - if (FAILED(hr)) return; res = await_IAsyncOperationWithProgress_UINT32_UINT32( operation, 1000 ); ok( res == 0, "await_IAsyncOperationWithProgress_UINT32_UINT32 returned %#x\n", res ); check_async_info( operation, Completed, S_OK ); @@ -415,7 +413,6 @@ static void test_InMemoryRandomAccessStream(void) ok( value64 == 8, "got pos %I64u.\n", value64 ); hr = IOutputStream_WriteAsync( output_stream, NULL, &write_op ); - todo_wine ok( hr == E_POINTER, "got hr %#lx.\n", hr ); /* Crashes on Windows if the op pointer is null * hr = IOutputStream_WriteAsync( output_stream, NULL, NULL ); */ -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/11297
From: Conor McCarthy <cmccarthy@codeweavers.com> --- dlls/windows.storage/streams.c | 5 +++-- dlls/windows.storage/tests/storage.c | 7 +------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/dlls/windows.storage/streams.c b/dlls/windows.storage/streams.c index 292b3d25748..e9ff6213cd2 100644 --- a/dlls/windows.storage/streams.c +++ b/dlls/windows.storage/streams.c @@ -291,7 +291,7 @@ static HRESULT WINAPI memory_stream_random_access_get_Size( IRandomAccessStream { struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); - FIXME( "iface %p, value %p stub!\n", iface, value ); + TRACE( "iface %p, value %p.\n", iface, value ); if (impl->closed) { @@ -299,7 +299,8 @@ static HRESULT WINAPI memory_stream_random_access_get_Size( IRandomAccessStream return RO_E_CLOSED; } - return E_NOTIMPL; + *value = impl->size; + return S_OK; } static HRESULT WINAPI memory_stream_random_access_put_Size( IRandomAccessStream *iface, UINT64 value ) diff --git a/dlls/windows.storage/tests/storage.c b/dlls/windows.storage/tests/storage.c index bade06c1ae6..1e1e09ea284 100644 --- a/dlls/windows.storage/tests/storage.c +++ b/dlls/windows.storage/tests/storage.c @@ -357,7 +357,6 @@ static void test_InMemoryRandomAccessStream(void) data = buffer_get_data( buffer ); hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); ok( value64 == 0, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); @@ -368,7 +367,6 @@ static void test_InMemoryRandomAccessStream(void) todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); ok( value64 == 0, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); @@ -380,14 +378,12 @@ static void test_InMemoryRandomAccessStream(void) /* Write 0 bytes at position 16 */ output_stream_write( output_stream, buffer, 0 ); hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); ok( value64 == 0, "got size %I64u.\n", value64 ); /* Write 1 byte at position 16 */ data[0] = byte_value; output_stream_write( output_stream, buffer, 1 ); hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); todo_wine ok( value64 == 17, "got size %I64u.\n", value64 ); @@ -398,11 +394,11 @@ static void test_InMemoryRandomAccessStream(void) hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); + todo_wine ok( value64 == 0, "got pos %I64u.\n", value64 ); memcpy( data, &uint64_value, 8 ); output_stream_write( output_stream, buffer, 8 ); hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); todo_wine ok( value64 == 17, "got size %I64u.\n", value64 ); @@ -498,7 +494,6 @@ static void test_InMemoryRandomAccessStream(void) todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); todo_wine ok( value64 == 0x100000, "got size %I64u.\n", value64 ); -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/11297
From: Conor McCarthy <cmccarthy@codeweavers.com> --- dlls/windows.storage/streams.c | 10 ++++++++-- dlls/windows.storage/tests/storage.c | 2 -- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/dlls/windows.storage/streams.c b/dlls/windows.storage/streams.c index e9ff6213cd2..d61788fc0a8 100644 --- a/dlls/windows.storage/streams.c +++ b/dlls/windows.storage/streams.c @@ -306,13 +306,19 @@ static HRESULT WINAPI memory_stream_random_access_get_Size( IRandomAccessStream static HRESULT WINAPI memory_stream_random_access_put_Size( IRandomAccessStream *iface, UINT64 value ) { struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); + HRESULT hr; - FIXME( "iface %p, value %I64u stub!\n", iface, value ); + TRACE( "iface %p, value %I64u.\n", iface, value ); if (impl->closed) return RO_E_CLOSED; - return E_NOTIMPL; + /* Native truncates the size to 32 bits, which is not replicated here if size_t is 64 bits. */ + if (FAILED(hr = memory_stream_require_capacity( impl, value ))) + return hr; + + impl->size = min( value, SIZE_MAX ); + return S_OK; } static HRESULT WINAPI memory_stream_random_access_GetInputStreamAt( IRandomAccessStream *iface, UINT64 position, diff --git a/dlls/windows.storage/tests/storage.c b/dlls/windows.storage/tests/storage.c index 1e1e09ea284..3a09b67aa9f 100644 --- a/dlls/windows.storage/tests/storage.c +++ b/dlls/windows.storage/tests/storage.c @@ -491,11 +491,9 @@ static void test_InMemoryRandomAccessStream(void) * but only allows reading from positions less than 1 << 31 * Both of these seem potentially subject to change, so are untested. */ hr = IRandomAccessStream_put_Size( in_memory_stream, 0x100000 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); ok( hr == S_OK, "got hr %#lx.\n", hr ); - todo_wine ok( value64 == 0x100000, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); todo_wine -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/11297
From: Conor McCarthy <cmccarthy@codeweavers.com> --- dlls/windows.storage/streams.c | 5 +++-- dlls/windows.storage/tests/storage.c | 6 ------ 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/dlls/windows.storage/streams.c b/dlls/windows.storage/streams.c index d61788fc0a8..70e8de96684 100644 --- a/dlls/windows.storage/streams.c +++ b/dlls/windows.storage/streams.c @@ -343,9 +343,10 @@ static HRESULT WINAPI memory_stream_random_access_get_Position( IRandomAccessStr { struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); - FIXME( "iface %p, value %p stub!\n", iface, value ); + TRACE( "iface %p, value %p.\n", iface, value ); - return impl->closed ? RO_E_CLOSED : E_NOTIMPL; + *value = impl->pos; + return impl->closed ? RO_E_CLOSED : S_OK; } static HRESULT WINAPI memory_stream_random_access_Seek( IRandomAccessStream *iface, UINT64 position ) diff --git a/dlls/windows.storage/tests/storage.c b/dlls/windows.storage/tests/storage.c index 3a09b67aa9f..ed2bf9275da 100644 --- a/dlls/windows.storage/tests/storage.c +++ b/dlls/windows.storage/tests/storage.c @@ -360,7 +360,6 @@ static void test_InMemoryRandomAccessStream(void) ok( hr == S_OK, "got hr %#lx.\n", hr ); ok( value64 == 0, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); ok( value64 == 0, "got pos %I64u.\n", value64 ); hr = IRandomAccessStream_Seek( in_memory_stream, 16 ); @@ -370,7 +369,6 @@ static void test_InMemoryRandomAccessStream(void) ok( hr == S_OK, "got hr %#lx.\n", hr ); ok( value64 == 0, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); todo_wine ok( value64 == 16, "got pos %I64u.\n", value64 ); @@ -392,7 +390,6 @@ static void test_InMemoryRandomAccessStream(void) todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); todo_wine ok( value64 == 0, "got pos %I64u.\n", value64 ); @@ -403,7 +400,6 @@ static void test_InMemoryRandomAccessStream(void) todo_wine ok( value64 == 17, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); todo_wine ok( value64 == 8, "got pos %I64u.\n", value64 ); @@ -444,7 +440,6 @@ static void test_InMemoryRandomAccessStream(void) todo_wine ok( value == 17, "got read %u.\n", value ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); todo_wine ok( value64 == 17, "got pos %I64u.\n", value64 ); @@ -496,7 +491,6 @@ static void test_InMemoryRandomAccessStream(void) ok( hr == S_OK, "got hr %#lx.\n", hr ); ok( value64 == 0x100000, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); todo_wine ok( value64 == 18, "got pos %I64u.\n", value64 ); -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/11297
From: Conor McCarthy <cmccarthy@codeweavers.com> --- dlls/windows.storage/streams.c | 5 +++-- dlls/windows.storage/tests/storage.c | 11 ----------- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/dlls/windows.storage/streams.c b/dlls/windows.storage/streams.c index 70e8de96684..15e13d1436e 100644 --- a/dlls/windows.storage/streams.c +++ b/dlls/windows.storage/streams.c @@ -353,12 +353,13 @@ static HRESULT WINAPI memory_stream_random_access_Seek( IRandomAccessStream *ifa { struct memory_stream *impl = impl_from_IRandomAccessStream( iface ); - FIXME( "iface %p, position %I64u stub!\n", iface, position ); + TRACE( "iface %p, position %I64u.\n", iface, position ); if (impl->closed) return RO_E_CLOSED; - return E_NOTIMPL; + impl->pos = position; + return S_OK; } static HRESULT WINAPI memory_stream_random_access_CloneStream( IRandomAccessStream *iface, IRandomAccessStream **stream ) diff --git a/dlls/windows.storage/tests/storage.c b/dlls/windows.storage/tests/storage.c index ed2bf9275da..7781f089388 100644 --- a/dlls/windows.storage/tests/storage.c +++ b/dlls/windows.storage/tests/storage.c @@ -363,14 +363,12 @@ static void test_InMemoryRandomAccessStream(void) ok( hr == S_OK, "got hr %#lx.\n", hr ); ok( value64 == 0, "got pos %I64u.\n", value64 ); hr = IRandomAccessStream_Seek( in_memory_stream, 16 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); ok( hr == S_OK, "got hr %#lx.\n", hr ); ok( value64 == 0, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); ok( hr == S_OK, "got hr %#lx.\n", hr ); - todo_wine ok( value64 == 16, "got pos %I64u.\n", value64 ); /* Write 0 bytes at position 16 */ @@ -383,25 +381,20 @@ static void test_InMemoryRandomAccessStream(void) output_stream_write( output_stream, buffer, 1 ); hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); ok( hr == S_OK, "got hr %#lx.\n", hr ); - todo_wine ok( value64 == 17, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_Seek( in_memory_stream, 0 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); ok( hr == S_OK, "got hr %#lx.\n", hr ); - todo_wine ok( value64 == 0, "got pos %I64u.\n", value64 ); memcpy( data, &uint64_value, 8 ); output_stream_write( output_stream, buffer, 8 ); hr = IRandomAccessStream_get_Size( in_memory_stream, &value64 ); ok( hr == S_OK, "got hr %#lx.\n", hr ); - todo_wine ok( value64 == 17, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); ok( hr == S_OK, "got hr %#lx.\n", hr ); - todo_wine ok( value64 == 8, "got pos %I64u.\n", value64 ); hr = IOutputStream_WriteAsync( output_stream, NULL, &write_op ); @@ -412,7 +405,6 @@ static void test_InMemoryRandomAccessStream(void) memset( data, 0xcd, 17 ); hr = IRandomAccessStream_Seek( in_memory_stream, 0 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); res_buffer = NULL; input_stream_read( input_stream, buffer, 0, Completed, S_OK, &res_buffer ); @@ -452,7 +444,6 @@ static void test_InMemoryRandomAccessStream(void) IBuffer_Release( res_buffer ); hr = IRandomAccessStream_Seek( in_memory_stream, 18 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); res_buffer = NULL; input_stream_read( input_stream, buffer, 1, Completed, S_OK, &res_buffer ); @@ -492,11 +483,9 @@ static void test_InMemoryRandomAccessStream(void) ok( value64 == 0x100000, "got size %I64u.\n", value64 ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); ok( hr == S_OK, "got hr %#lx.\n", hr ); - todo_wine ok( value64 == 18, "got pos %I64u.\n", value64 ); hr = IRandomAccessStream_Seek( in_memory_stream, 0xffff8 ); - todo_wine ok( hr == S_OK, "got hr %#lx.\n", hr ); res_buffer = NULL; input_stream_read( input_stream, buffer, 8, Completed, S_OK, &res_buffer ); -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/11297
From: Conor McCarthy <cmccarthy@codeweavers.com> --- dlls/windows.storage/async.c | 12 +++++ dlls/windows.storage/private.h | 45 +++++++++++++++++ dlls/windows.storage/streams.c | 72 +++++++++++++++++++++++++++- dlls/windows.storage/tests/storage.c | 20 -------- 4 files changed, 127 insertions(+), 22 deletions(-) diff --git a/dlls/windows.storage/async.c b/dlls/windows.storage/async.c index 090ae84fac9..17bfa462e81 100644 --- a/dlls/windows.storage/async.c +++ b/dlls/windows.storage/async.c @@ -518,6 +518,18 @@ HRESULT creation_fn( IUnknown *invoker, IUnknown *param, return S_OK; \ } \ +struct async_buffer_uint32 +{ + IAsyncOperationWithProgress_IBuffer_UINT32 IAsyncOperationWithProgress_IBuffer_UINT32_iface; + IAsyncInfoImpl *IAsyncInfoImpl_inner; + LONG ref; +}; + +DEFINE_IASYNCOPERATIONWITHPROGRESS( IAsyncOperationWithProgress_IBuffer_UINT32, \ + IAsyncOperationProgressHandler_IBuffer_UINT32, \ + IAsyncOperationWithProgressCompletedHandler_IBuffer_UINT32, \ + "IBuffer,UInt32", async_buffer_uint32, IBuffer*, VT_UNKNOWN, punkVal, async_operation_buffer_uint32_create ) + struct async_uint32_uint32 { IAsyncOperationWithProgress_UINT32_UINT32 IAsyncOperationWithProgress_UINT32_UINT32_iface; diff --git a/dlls/windows.storage/private.h b/dlls/windows.storage/private.h index fdf7ad48769..21ef87124e0 100644 --- a/dlls/windows.storage/private.h +++ b/dlls/windows.storage/private.h @@ -41,6 +41,8 @@ #include "robuffer.h" #include "async_private.h" +HRESULT async_operation_buffer_uint32_create( IUnknown *invoker, IUnknown *param, async_operation_callback callback, + IAsyncOperationWithProgress_IBuffer_UINT32 **out ); HRESULT async_operation_uint32_uint32_create( IUnknown *invoker, IUnknown *param, async_operation_callback callback, IAsyncOperationWithProgress_UINT32_UINT32 **out ); @@ -87,4 +89,47 @@ extern IActivationFactory *memory_stream_activation_factory; #define DEFINE_IINSPECTABLE_OUTER( pfx, iface_type, impl_type, outer_iface ) \ DEFINE_IINSPECTABLE_( pfx, iface_type, impl_type, impl_from_##iface_type, iface_type##_iface, impl->outer_iface ) +#define DEFINE_ASYNC_PARAMS( type ) \ + static struct type *type##_from_IUnknown( IUnknown *iface ) \ + { \ + return CONTAINING_RECORD( iface, struct type, IUnknown_iface ); \ + } \ + static HRESULT WINAPI type##_QueryInterface( IUnknown *iface, REFIID iid, void **out ) \ + { \ + if (IsEqualIID( iid, &IID_IUnknown )) \ + { \ + IUnknown_AddRef( iface ); \ + *out = iface; \ + return S_OK; \ + } \ + *out = NULL; \ + return E_NOINTERFACE; \ + } \ + static ULONG WINAPI type##_AddRef( IUnknown *iface ) \ + { \ + struct type *object = type##_from_IUnknown( iface ); \ + return InterlockedIncrement( &object->ref ); \ + } \ + static ULONG WINAPI type##_Release( IUnknown *iface ) \ + { \ + struct type *object = type##_from_IUnknown( iface ); \ + ULONG ref = InterlockedDecrement( &object->ref ); \ + if (!ref) type##_##destroy( object ); \ + return ref; \ + } \ + static const IUnknownVtbl type##_vtbl = \ + { \ + type##_QueryInterface, \ + type##_AddRef, \ + type##_Release, \ + }; \ + static struct type *type##_alloc(void) \ + { \ + struct type *object; \ + if (!(object = calloc( 1, sizeof(*object) ))) return NULL; \ + object->IUnknown_iface.lpVtbl = &type##_vtbl; \ + object->ref = 1; \ + return object; \ + } + #endif diff --git a/dlls/windows.storage/streams.c b/dlls/windows.storage/streams.c index 15e13d1436e..f321ee55280 100644 --- a/dlls/windows.storage/streams.c +++ b/dlls/windows.storage/streams.c @@ -186,6 +186,21 @@ struct memory_stream BOOL closed; }; +static void memory_stream_read( struct memory_stream *impl, IBuffer *buffer, UINT32 count ) +{ + IBufferByteAccess *access; + BYTE *data; + + IBuffer_QueryInterface( buffer, &IID_IBufferByteAccess, (void **)&access ); + IBufferByteAccess_Buffer( access, &data ); + IBufferByteAccess_Release( access ); + + count = (impl->size >= impl->pos) ? min( count, impl->size - impl->pos ) : 0; + memcpy( data, &impl->buffer[impl->pos], count ); + impl->pos += count; + IBuffer_put_Length( buffer, count ); +} + static HRESULT memory_stream_require_capacity( struct memory_stream *impl, size_t capacity ) { BYTE *new_buffer; @@ -434,6 +449,22 @@ static const struct IClosableVtbl memory_stream_closable_vtbl = memory_stream_closable_Close, }; +struct read_async_params +{ + IUnknown IUnknown_iface; + LONG ref; + + IBuffer *buffer; + UINT32 count; +}; + +static void read_async_params_destroy( struct read_async_params *params ) +{ + IBuffer_Release( params->buffer ); +} + +DEFINE_ASYNC_PARAMS( read_async_params ) + static inline struct memory_stream *impl_from_IInputStream( IInputStream *iface ) { return CONTAINING_RECORD( iface, struct memory_stream, IInputStream_iface ); @@ -478,19 +509,56 @@ static HRESULT WINAPI memory_stream_input_GetTrustLevel( IInputStream *iface, Tr return E_NOTIMPL; } +static HRESULT memory_stream_input_async( IUnknown *invoker, IUnknown *param, PROPVARIANT *result, BOOL called_async ) +{ + struct memory_stream *impl = impl_from_IInputStream( (IInputStream *)invoker ); + struct read_async_params *params = read_async_params_from_IUnknown( param ); + UINT32 capacity; + HRESULT hr; + + assert( !called_async ); + + if (FAILED(hr = IBuffer_get_Capacity( params->buffer, &capacity ))) + return hr; + + if (capacity < params->count) + return E_INVALIDARG; + + memory_stream_read( impl, params->buffer, params->count ); + result->vt = VT_UNKNOWN; + IBuffer_AddRef( params->buffer ); + result->punkVal = (IUnknown *)params->buffer; + + return S_OK; +} + static HRESULT WINAPI memory_stream_input_ReadAsync( IInputStream *iface, IBuffer *buffer, UINT32 count, InputStreamOptions options, IAsyncOperationWithProgress_IBuffer_UINT32 **operation ) { struct memory_stream *impl = impl_from_IInputStream( iface ); + struct read_async_params *params; - FIXME( "iface %p, buffer %p, count %u, options %d, operation %p stub!\n", iface, buffer, count, options, operation ); + TRACE( "iface %p, buffer %p, count %u, options %d, operation %p.\n", iface, buffer, count, options, operation ); *operation = NULL; + if (!buffer) + return E_POINTER; + if (impl->closed) return RO_E_CLOSED; - return E_NOTIMPL; + if (!(params = read_async_params_alloc())) + return E_OUTOFMEMORY; + + IBuffer_AddRef( buffer ); + params->buffer = buffer; + params->count = count; + async_operation_buffer_uint32_create( (IUnknown *)iface, ¶ms->IUnknown_iface, memory_stream_input_async, operation ); + + IUnknown_Release( ¶ms->IUnknown_iface ); + + return S_OK; } static const struct IInputStreamVtbl memory_stream_input_vtbl = diff --git a/dlls/windows.storage/tests/storage.c b/dlls/windows.storage/tests/storage.c index 7781f089388..0f5e1c0d0d1 100644 --- a/dlls/windows.storage/tests/storage.c +++ b/dlls/windows.storage/tests/storage.c @@ -262,9 +262,7 @@ static void input_stream_read_( unsigned int line, IInputStream *input_stream, I UINT res; hr = IInputStream_ReadAsync( input_stream, buffer, count, 0, &operation ); - todo_wine ok_(__FILE__, line)( hr == S_OK, "got hr %#lx.\n", hr ); - if (FAILED(hr)) return; res = await_IAsyncOperationWithProgress_IBuffer_UINT32( operation, 1000 ); ok_(__FILE__, line)( res == 0, "await_IAsyncOperationWithProgress_IBuffer_UINT32 returned %#x\n", res ); check_async_info( operation, expect_status, expect_hr ); @@ -408,38 +406,28 @@ static void test_InMemoryRandomAccessStream(void) ok( hr == S_OK, "got hr %#lx.\n", hr ); res_buffer = NULL; input_stream_read( input_stream, buffer, 0, Completed, S_OK, &res_buffer ); - todo_wine ok( res_buffer == buffer, "got different buffer.\n" ); - if (!res_buffer) IBuffer_AddRef( res_buffer = buffer ); hr = IBuffer_get_Length( res_buffer, &value ); ok( hr == S_OK, "got hr %#lx.\n", hr ); - todo_wine ok( value == 0, "got read %u.\n", value ); IBuffer_Release( res_buffer ); res_buffer = (void *)0xdeadbeef; input_stream_read( input_stream, buffer, 20, Error, E_INVALIDARG, &res_buffer ); - todo_wine ok( !res_buffer, "got res_buffer %p.\n", res_buffer ); res_buffer = NULL; input_stream_read( input_stream, buffer, 17, Completed, S_OK, &res_buffer ); - todo_wine ok( res_buffer == buffer, "got different buffer.\n" ); - if (!res_buffer) IBuffer_AddRef( res_buffer = buffer ); hr = IBuffer_get_Length( res_buffer, &value ); ok( hr == S_OK, "got hr %#lx.\n", hr ); - todo_wine ok( value == 17, "got read %u.\n", value ); hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); ok( hr == S_OK, "got hr %#lx.\n", hr ); - todo_wine ok( value64 == 17, "got pos %I64u.\n", value64 ); data = buffer_get_data( res_buffer ); memcpy( &value64, data, sizeof(value64 ) ); - todo_wine ok( value64 == uint64_value, "got value64 %#I64x.\n", value64 ); - todo_wine ok ( data[16] == byte_value, "got byte value %#x.\n", data[16]); IBuffer_Release( res_buffer ); @@ -447,18 +435,14 @@ static void test_InMemoryRandomAccessStream(void) ok( hr == S_OK, "got hr %#lx.\n", hr ); res_buffer = NULL; input_stream_read( input_stream, buffer, 1, Completed, S_OK, &res_buffer ); - todo_wine ok( res_buffer == buffer, "got different buffer.\n" ); - if (!res_buffer) IBuffer_AddRef( res_buffer = buffer ); hr = IBuffer_get_Length( res_buffer, &value ); ok( hr == S_OK, "got hr %#lx.\n", hr ); - todo_wine ok( value == 0, "got read %u.\n", value ); IBuffer_Release( res_buffer ); operation = (void *)0xdeadbeef; hr = IInputStream_ReadAsync( input_stream, NULL, 1, 0, &operation ); - todo_wine ok( hr == E_POINTER, "got hr %#lx.\n", hr ); ok( !operation, "got operation %p.\n", operation ); /* Crashes on Windows if the op pointer is null @@ -489,15 +473,12 @@ static void test_InMemoryRandomAccessStream(void) ok( hr == S_OK, "got hr %#lx.\n", hr ); res_buffer = NULL; input_stream_read( input_stream, buffer, 8, Completed, S_OK, &res_buffer ); - todo_wine ok( res_buffer == buffer, "got different buffer.\n" ); - if (!res_buffer) IBuffer_AddRef( res_buffer = buffer ); hr = IBuffer_get_Length( res_buffer, &value ); ok( hr == S_OK, "got hr %#lx.\n", hr ); ok( value == 8, "got read %u.\n", value ); data = buffer_get_data( res_buffer ); memcpy( &value64, data, sizeof(value64) ); - todo_wine ok( value64 == 0, "got value64 %#I64x.\n", value64 ); IBuffer_Release( res_buffer ); @@ -527,7 +508,6 @@ static void test_InMemoryRandomAccessStream(void) value64 = uint64_value; hr = IRandomAccessStream_get_Position( in_memory_stream, &value64 ); ok( hr == RO_E_CLOSED, "got hr %#lx.\n", hr ); - todo_wine ok( value64 == 0x100000, "got pos %I64u.\n", value64 ); value_bool = 0; hr = IRandomAccessStream_get_CanRead( in_memory_stream, &value_bool ); -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/11297
participants (2)
-
Conor McCarthy -
Conor McCarthy (@cmccarthy)