コード例 #1
0
        static bool TryGetResourceValue(System.Runtime.InteropServices.ComTypes.IDataObject dataObject, string resourceName, out string value)
        {
            value = string.Empty;

            FORMATETC formatEtc = new FORMATETC();

            formatEtc.cfFormat = (short)System.Windows.Forms.DataFormats.GetFormat(resourceName).Id;
            formatEtc.ptd      = IntPtr.Zero;
            formatEtc.dwAspect = DVASPECT.DVASPECT_CONTENT;
            formatEtc.lindex   = -1;
            formatEtc.tymed    = TYMED.TYMED_HGLOBAL;

            STGMEDIUM stg = new STGMEDIUM();

            stg.pUnkForRelease = null;
            stg.tymed          = TYMED.TYMED_HGLOBAL;
#pragma warning suppress 56523
            stg.unionmember = SafeNativeMethods.GlobalAlloc(SafeNativeMethods.GMEM_SHARE, 255);
            if (stg.unionmember == IntPtr.Zero)
            {
                return(false);
            }
            try
            {
                dataObject.GetDataHere(ref formatEtc, ref stg);
            }
            catch (Exception ex)
            {
                if (Utilities.IsCriticalException(ex))
                {
                    throw;
                }
                return(false);
            }

            value = Marshal.PtrToStringUni(stg.unionmember);

            SafeNativeMethods.ReleaseStgMedium(ref stg);
            return(true);
        }