|
| 1 | +package winbt |
| 2 | + |
| 3 | +import ( |
| 4 | + "syscall" |
| 5 | + "unsafe" |
| 6 | + |
| 7 | + "github.com/go-ole/go-ole" |
| 8 | +) |
| 9 | + |
| 10 | +// IBuffer Represents a referenced array of bytes used by |
| 11 | +// byte stream read and write interfaces. Buffer is the class |
| 12 | +// implementation of this interface. |
| 13 | +type IBuffer struct { |
| 14 | + ole.IInspectable |
| 15 | +} |
| 16 | + |
| 17 | +type IBufferVtbl struct { |
| 18 | + ole.IInspectableVtbl |
| 19 | + |
| 20 | + // These methods have been obtained from windows.storage.streams.h in the WinRT API. |
| 21 | + |
| 22 | + // read methods |
| 23 | + GetCapacity uintptr // ([out] [retval] UINT32* value) |
| 24 | + GetLength uintptr // ([out] [retval] UINT32* value) |
| 25 | + |
| 26 | + // write methods |
| 27 | + SetLength uintptr // ([in] UINT32 value); |
| 28 | +} |
| 29 | + |
| 30 | +func (v *IBuffer) VTable() *IBufferVtbl { |
| 31 | + return (*IBufferVtbl)(unsafe.Pointer(v.RawVTable)) |
| 32 | +} |
| 33 | + |
| 34 | +func (v *IBuffer) Length() int { |
| 35 | + var n int |
| 36 | + hr, _, _ := syscall.SyscallN( |
| 37 | + v.VTable().GetLength, |
| 38 | + uintptr(unsafe.Pointer(v)), |
| 39 | + uintptr(unsafe.Pointer(&n)), |
| 40 | + ) |
| 41 | + mustSucceed(hr) |
| 42 | + return n |
| 43 | +} |
| 44 | + |
| 45 | +func (v *IBuffer) Bytes() ([]byte, error) { |
| 46 | + // Get DataReaderStatics: we need to pass the class name, and the iid of the interface |
| 47 | + // GUID: https://github.com/tpn/winsdk-10/blob/9b69fd26ac0c7d0b83d378dba01080e93349c2ed/Include/10.0.14393.0/winrt/windows.storage.streams.idl#L311 |
| 48 | + inspectable, err := ole.RoGetActivationFactory("Windows.Storage.Streams.DataReader", ole.NewGUID("11FCBFC8-F93A-471B-B121-F379E349313C")) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + drStatics := (*IDataReaderStatics)(unsafe.Pointer(inspectable)) |
| 54 | + |
| 55 | + // Call FromBuffer to create new DataReader |
| 56 | + var dr *IDataReader |
| 57 | + hr, _, _ := syscall.SyscallN( |
| 58 | + drStatics.VTable().FromBuffer, |
| 59 | + 0, // this is a static func, so there's no this |
| 60 | + uintptr(unsafe.Pointer(v)), // in buffer |
| 61 | + uintptr(unsafe.Pointer(&dr)), // out DataReader |
| 62 | + ) |
| 63 | + err = makeError(hr) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + data := make([]byte, v.Length()) |
| 69 | + err = dr.Bytes(data) |
| 70 | + return data, err |
| 71 | +} |
| 72 | + |
| 73 | +type IDataReaderStatics struct { |
| 74 | + ole.IInspectable |
| 75 | +} |
| 76 | + |
| 77 | +type IDataReaderStaticsVtbl struct { |
| 78 | + ole.IInspectableVtbl |
| 79 | + |
| 80 | + FromBuffer uintptr // ([in] Windows.Storage.Streams.IBuffer* buffer, [out] [retval] Windows.Storage.Streams.DataReader** dataReader); |
| 81 | +} |
| 82 | + |
| 83 | +func (v *IDataReaderStatics) VTable() *IDataReaderStaticsVtbl { |
| 84 | + return (*IDataReaderStaticsVtbl)(unsafe.Pointer(v.RawVTable)) |
| 85 | +} |
| 86 | + |
| 87 | +type IDataReader struct { |
| 88 | + ole.IInspectable |
| 89 | +} |
| 90 | + |
| 91 | +type IDataReaderVtbl struct { |
| 92 | + ole.IInspectableVtbl |
| 93 | + |
| 94 | + GetUnconsumedBufferLength uintptr // ([out] [retval] UINT32* value); |
| 95 | + GetUnicodeEncoding uintptr // ([out] [retval] Windows.Storage.Streams.UnicodeEncoding* value); |
| 96 | + PutUnicodeEncoding uintptr // ([in] Windows.Storage.Streams.UnicodeEncoding value); |
| 97 | + GetByteOrder uintptr // ([out] [retval] Windows.Storage.Streams.ByteOrder* value); |
| 98 | + PutByteOrder uintptr // ([in] Windows.Storage.Streams.ByteOrder value); |
| 99 | + GetInputStreamOptions uintptr // ([out] [retval] Windows.Storage.Streams.InputStreamOptions* value); |
| 100 | + PutInputStreamOptions uintptr // ([in] Windows.Storage.Streams.InputStreamOptions value); |
| 101 | + ReadByte uintptr // ([out] [retval] BYTE* value); |
| 102 | + ReadBytes uintptr // ([in] UINT32 __valueSize, [out] [size_is(__valueSize)] BYTE* value); |
| 103 | + ReadBuffer uintptr // ([in] UINT32 length, [out] [retval] Windows.Storage.Streams.IBuffer** buffer); |
| 104 | + ReadBoolean uintptr // ([out] [retval] boolean* value); |
| 105 | + ReadGuid uintptr // ([out] [retval] GUID* value); |
| 106 | + ReadInt16 uintptr // ([out] [retval] INT16* value); |
| 107 | + ReadInt32 uintptr // ([out] [retval] INT32* value); |
| 108 | + ReadInt64 uintptr // ([out] [retval] INT64* value); |
| 109 | + ReadUInt16 uintptr // ([out] [retval] UINT16* value); |
| 110 | + ReadUInt32 uintptr // ([out] [retval] UINT32* value); |
| 111 | + ReadUInt64 uintptr // ([out] [retval] UINT64* value); |
| 112 | + ReadSingle uintptr // ([out] [retval] FLOAT* value); |
| 113 | + ReadDouble uintptr // ([out] [retval] DOUBLE* value); |
| 114 | + ReadString uintptr // ([in] UINT32 codeUnitCount, [out] [retval] HSTRING* value); |
| 115 | + ReadDateTime uintptr // ([out] [retval] Windows.Foundation.DateTime* value); |
| 116 | + ReadTimeSpan uintptr // ([out] [retval] Windows.Foundation.TimeSpan* value); |
| 117 | + LoadAsync uintptr // ([in] UINT32 count, [out] [retval] Windows.Storage.Streams.DataReaderLoadOperation** operation); |
| 118 | + DetachBuffer uintptr // ([out] [retval] Windows.Storage.Streams.IBuffer** buffer); |
| 119 | + DetachStream uintptr // ([out] [retval] Windows.Storage.Streams.IInputStream** stream);*/ |
| 120 | +} |
| 121 | + |
| 122 | +func (v *IDataReader) VTable() *IDataReaderVtbl { |
| 123 | + return (*IDataReaderVtbl)(unsafe.Pointer(v.RawVTable)) |
| 124 | +} |
| 125 | + |
| 126 | +// Bytes fills the incoming array with the data from the buffer |
| 127 | +func (v *IDataReader) Bytes(b []byte) error { |
| 128 | + // ([in] UINT32 __valueSize, [out] [size_is(__valueSize)] BYTE* value); |
| 129 | + size := len(b) |
| 130 | + hr, _, _ := syscall.SyscallN( |
| 131 | + v.VTable().ReadBytes, |
| 132 | + uintptr(unsafe.Pointer(v)), |
| 133 | + uintptr(size), |
| 134 | + uintptr(unsafe.Pointer(&b[0])), |
| 135 | + ) |
| 136 | + return makeError(hr) |
| 137 | +} |
0 commit comments