/ / メモ
2008-06-20

2008-06-14-1 の C# 版
CoInitialize を書かない。
CoCreateInstance の代わりに new を使う。
BSTR は string
UPnPDevices を foreach でくるくる。
IUnknown.Release の代わりに Marshal.ReleaseComObject を使う。

using System;
using System.Collections;
using System.Runtime.InteropServices;
using UPNPLib;

namespace upnp_enum_dev
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList rcws = new ArrayList();
            try
            {
                UPnPDeviceFinder df = new UPnPDeviceFinder();
                rcws.Add(df);
                UPnPDevices ds = df.FindByType("upnp:rootdevice", 0);
                rcws.Add(ds);
                foreach (UPnPDevice d in ds)
                {
                    rcws.Add(d);
                    Console.WriteLine(d.FriendlyName);
                }
            }
            finally
            {
                foreach(object obj in rcws)
                {
                    try
                    {
                        Marshal.ReleaseComObject(obj);
                    }
                    catch
                    {
                    }
                }
            }
        }
    }
}

リソースの解放は D言語の scope(exit) のほうが見やすい。

参考
COM Interop, Marshal.ReleaseComObject

トラックバック http://mikanya.dip.jp/memo/2008-06-20-1