Exemplo n.º 1
0
        // Converts a string to a PropVariant with type LPWSTR instead of BSTR
        // The resulting variant must be cleared using PropVariantClear and freed using Marshal.FreeCoTaskMem
        public static IntPtr PropVariantFromString(string value)
        {
            IntPtr pstr = IntPtr.Zero;
            IntPtr pv   = IntPtr.Zero;

            try
            {
                // In managed code, new automatically zeros the contents.
                PropVariant.PROPVARIANT propvariant = new PropVariant.PROPVARIANT();

                // Allocate the string
                pstr = Marshal.StringToCoTaskMemUni(value);

                // Allocate the PropVariant
                pv = Marshal.AllocCoTaskMem(16);

                // Transfer ownership of the string
                propvariant.vt         = 31; // VT_LPWSTR - not documented but this is to be allocated using CoTaskMemAlloc.
                propvariant.dataIntPtr = pstr;
                Marshal.StructureToPtr(propvariant, pv, false);
                pstr = IntPtr.Zero;

                // Transfer ownership to the result
                IntPtr result = pv;
                pv = IntPtr.Zero;
                return(result);
            }
            finally
            {
                if (pstr != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pstr);
                    pstr = IntPtr.Zero;
                }
                if (pv != IntPtr.Zero)
                {
                    try
                    {
                        NativeMethods.PropVariantClear(pv);
                    }
                    catch
                    {
                        Debug.Fail("VariantClear failure");
                    }
                    Marshal.FreeCoTaskMem(pv);
                    pv = IntPtr.Zero;
                }
            }
        }
Exemplo n.º 2
0
        // Converts a string to a PropVariant with type LPWSTR instead of BSTR
        // The resulting variant must be cleared using PropVariantClear and freed using Marshal.FreeCoTaskMem
        public static IntPtr PropVariantFromString(string value)
        {
            IntPtr pstr = IntPtr.Zero;
            IntPtr pv = IntPtr.Zero;
            try
            {
                // In managed code, new automatically zeros the contents.
                PropVariant.PROPVARIANT propvariant = new PropVariant.PROPVARIANT();

                // Allocate the string
                pstr = Marshal.StringToCoTaskMemUni(value);

                // Allocate the PropVariant
                pv = Marshal.AllocCoTaskMem(16);

                // Transfer ownership of the string
                propvariant.vt = 31; // VT_LPWSTR - not documented but this is to be allocated using CoTaskMemAlloc.
                propvariant.dataIntPtr = pstr;
                Marshal.StructureToPtr(propvariant, pv, false);
                pstr = IntPtr.Zero;

                // Transfer ownership to the result
                IntPtr result = pv;
                pv = IntPtr.Zero;
                return result;
            }
            finally
            {
                if (pstr != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pstr);
                    pstr = IntPtr.Zero;
                }
                if (pv != IntPtr.Zero)
                {
                    try
                    {
                        NativeMethods.PropVariantClear(pv);
                    }
                    catch
                    {
                        Debug.Fail("VariantClear failure");
                    }
                    Marshal.FreeCoTaskMem(pv);
                    pv = IntPtr.Zero;
                }
            }
        }