forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructHelpers.cs
More file actions
32 lines (27 loc) · 1008 Bytes
/
Copy pathStructHelpers.cs
File metadata and controls
32 lines (27 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Microsoft.ClearScript.Util.COM
{
internal static class StructHelpers
{
public delegate void GetStruct(out IntPtr pStruct);
public delegate void ReleaseStruct(IntPtr pStruct);
public static IScope<T> CreateScope<T>(GetStruct get, ReleaseStruct release)
{
get(out var pStruct);
return Scope.Create(() => (T)Marshal.PtrToStructure(pStruct, typeof(T)), _ => release(pStruct));
}
public static IEnumerable<T> GetStructsFromArray<T>(IntPtr pStructs, int count)
{
var size = Marshal.SizeOf(typeof(T));
for (var pStruct = pStructs; count > 0; count--)
{
yield return (T)Marshal.PtrToStructure(pStruct, typeof(T));
pStruct += size;
}
}
}
}