-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIEnumStringExtensions.cs
68 lines (55 loc) · 1.78 KB
/
IEnumStringExtensions.cs
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#if (!WindowsCE) && (!WINDOWS_PHONE) && (!PCL)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenNETCF;
namespace System.Runtime.InteropServices.ComTypes
{
public static class IEnumStringExtensions
{
private const int S_OK = 1;
private const int S_NOTDONE = 0;
private const int BufferSize = 100;
public static List<string> GetStringList(this IEnumString enumerator)
{
Validate.Begin()
.ParameterIsNotNull(enumerator, "enumerator")
.Check();
var browserNames = new List<string>();
var stringNames = new List<string>();
var count = 0;
var countHandle = GCHandle.Alloc(count, GCHandleType.Pinned);
try
{
var countPtr = countHandle.AddrOfPinnedObject();
enumerator.Reset();
var buffer = new string[BufferSize];
var hresult = enumerator.Next(buffer.Length, buffer, countPtr);
stringNames.AddRange(buffer);
while (hresult == S_NOTDONE)
{
hresult = enumerator.Next(buffer.Length, buffer, countPtr);
stringNames.AddRange(buffer);
}
if (hresult != S_OK)
{
throw Marshal.GetExceptionForHR(hresult);
}
foreach (var item in stringNames)
{
if (item != null)
{
browserNames.Add(item);
}
}
}
finally
{
countHandle.Free();
}
return browserNames;
}
}
}
#endif