Created
August 29, 2017 17:31
-
-
Save tkouba/bcf0ddd5736e3b53987358ab1515bb19 to your computer and use it in GitHub Desktop.
COM interface IEnumString to string array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private string[] IEnumStringToArray(IEnumString enumerator) | |
{ | |
const int S_OK = 0x00000000; | |
List<string> lst = new List<string>(); | |
if (enumerator != null) | |
{ | |
int cft; | |
string[] strF = new string[100]; | |
int hresult; | |
IntPtr intPtr = Marshal.AllocCoTaskMem(sizeof(int)); | |
try | |
{ | |
do | |
{ | |
hresult = enumerator.Next(100, strF, intPtr); | |
if (hresult < 0) | |
Marshal.ThrowExceptionForHR(hresult); | |
cft = Marshal.ReadInt32(intPtr); | |
if (cft > 0) | |
{ | |
for (int i = 0; i < cft; i++) | |
lst.Add(strF[i]); | |
} | |
} | |
while (hresult == S_OK); | |
} | |
finally | |
{ | |
if (intPtr != IntPtr.Zero) | |
Marshal.FreeCoTaskMem(intPtr); | |
} | |
} | |
return lst.ToArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment