Skip to content

Instantly share code, notes, and snippets.

@tkouba
Created August 29, 2017 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkouba/bcf0ddd5736e3b53987358ab1515bb19 to your computer and use it in GitHub Desktop.
Save tkouba/bcf0ddd5736e3b53987358ab1515bb19 to your computer and use it in GitHub Desktop.
COM interface IEnumString to string array
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