Beispiel #1
0
            public IntPtr MarshalManagedToNative(object obj)
            {
                Font font = (Font)obj;

                User32.LOGFONTW logFont  = User32.LOGFONTW.FromFont(font);
                var             fontDesc = new Oleaut32.FONTDESC
                {
                    cbSizeOfStruct = (uint)Marshal.SizeOf <Oleaut32.FONTDESC>(),
                    lpstrName      = font.Name,
                    cySize         = (long)(font.SizeInPoints * 10000),
                    sWeight        = (short)logFont.lfWeight,
                    sCharset       = logFont.lfCharSet,
                    fItalic        = font.Italic.ToBOOL(),
                    fUnderline     = font.Underline.ToBOOL(),
                    fStrikethrough = font.Strikeout.ToBOOL(),
                };
                Guid iid = typeof(Ole32.IFont).GUID;

                Ole32.IFont oleFont = Oleaut32.OleCreateFontIndirect(ref fontDesc, ref iid);
                IntPtr      pFont   = Marshal.GetIUnknownForObject(oleFont);

                int hr = Marshal.QueryInterface(pFont, ref iid, out IntPtr pIFont);

                Marshal.Release(pFont);

                if (((HRESULT)hr).Failed())
                {
                    Marshal.ThrowExceptionForHR(hr);
                }

                return(pIFont);
            }
Beispiel #2
0
            unsafe HRESULT Ole32.IVBFormat.Format(
                IntPtr vData,
                IntPtr bstrFormat,
                IntPtr lpBuffer,
                ushort cb,
                int lcid,
                Ole32.VarFormatFirstDayOfWeek sFirstDayOfWeek,
                Ole32.VarFormatFirstWeekOfYear sFirstWeekOfYear,
                ushort *rcb)
            {
                Debug.WriteLineIf(s_axHTraceSwitch.TraceVerbose, "in Format");
                if (rcb is null)
                {
                    return(HRESULT.E_INVALIDARG);
                }

                *rcb = 0;
                if (lpBuffer == IntPtr.Zero || cb < 2)
                {
                    return(HRESULT.E_INVALIDARG);
                }

                IntPtr  pbstr = IntPtr.Zero;
                HRESULT hr    = Oleaut32.VarFormat(
                    vData,
                    bstrFormat,
                    sFirstDayOfWeek,
                    sFirstWeekOfYear,
                    Oleaut32.VarFormatOptions.FORMAT_NOSUBSTITUTE,
                    ref pbstr);

                try
                {
                    ushort i = 0;
                    if (pbstr != IntPtr.Zero)
                    {
                        short ch = 0;
                        cb--;
                        for (; i < cb && (ch = Marshal.ReadInt16(pbstr, i * 2)) != 0; i++)
                        {
                            Marshal.WriteInt16(lpBuffer, i * 2, ch);
                        }
                    }

                    Marshal.WriteInt16(lpBuffer, i * 2, (short)0);
                    *rcb = i;
                }
                finally
                {
                    Oleaut32.SysFreeString(pbstr);
                }

                return(HRESULT.S_OK);
            }
Beispiel #3
0
            public void Clear()
            {
                if ((vt == VARENUM.UNKNOWN || vt == VARENUM.DISPATCH) && data1 != IntPtr.Zero)
                {
                    Marshal.Release(data1);
                }

                if (vt == VARENUM.BSTR && data1 != IntPtr.Zero)
                {
                    Oleaut32.SysFreeString(data1);
                }

                data1 = data2 = IntPtr.Zero;
                vt    = VARENUM.EMPTY;
            }
        private static object[] GetVariantsFromPtr(IntPtr ptr, uint cVariants)
        {
            if (ptr != IntPtr.Zero)
            {
                object[] objects = new object[cVariants];
                IntPtr   curVariant;

                for (int i = 0; i < cVariants; i++)
                {
                    try
                    {
                        curVariant = (IntPtr)((long)ptr + (i * 16 /*sizeof(VARIANT)*/));
                        if (curVariant != IntPtr.Zero)
                        {
                            objects[i] = Marshal.GetObjectForNativeVariant(curVariant);
                            Oleaut32.VariantClear(curVariant);
                        }
                        else
                        {
                            objects[i] = Convert.DBNull;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.Fail("Failed to marshal component attribute VARIANT " + i.ToString(CultureInfo.InvariantCulture), ex.ToString());
                    }
                }
                try
                {
                    Marshal.FreeCoTaskMem(ptr);
                }
                catch (Exception ex)
                {
                    Debug.Fail("Failed to free VARIANT array memory", ex.ToString());
                }
                return(objects);
            }
            else
            {
                return(new object[cVariants]);
            }
        }
Beispiel #5
0
 private static string[] GetStringsFromPtr(IntPtr ptr, int cStrings)
 {
     if (ptr != IntPtr.Zero)
     {
         string[] strs = new string[cStrings];
         IntPtr   bstr;
         for (int i = 0; i < cStrings; i++)
         {
             try
             {
                 bstr = Marshal.ReadIntPtr(ptr, i * 4);
                 if (bstr != IntPtr.Zero)
                 {
                     strs[i] = Marshal.PtrToStringUni(bstr);
                     Oleaut32.SysFreeString(bstr);
                 }
                 else
                 {
                     strs[i] = string.Empty;
                 }
             }
             catch (Exception ex)
             {
                 Debug.Fail("Failed to marshal component attribute BSTR " + i.ToString(CultureInfo.InvariantCulture), ex.ToString());
             }
         }
         try
         {
             Marshal.FreeCoTaskMem(ptr);
         }
         catch (Exception ex)
         {
             Debug.Fail("Failed to free BSTR array memory", ex.ToString());
         }
         return(strs);
     }
     else
     {
         return(Array.Empty <string>());
     }
 }
        public unsafe void ShowPropertyPage(string title, object component, int dispid, Guid pageGuid, IntPtr parentHandle)
        {
            object[] objs     = component.GetType().IsArray ? (object[])component : new object[] { component };
            IntPtr[] objAddrs = new IntPtr[objs.Length];

            try
            {
                for (int i = 0; i < objAddrs.Length; i++)
                {
                    objAddrs[i] = Marshal.GetIUnknownForObject(objs[i]);
                }

                fixed(IntPtr *pObjAddrs = objAddrs)
                {
                    Oleaut32.OleCreatePropertyFrame(
                        parentHandle,
                        0,
                        0,
                        title,
                        (uint)objAddrs.Length,
                        pObjAddrs,
                        1,
                        &pageGuid,
                        Kernel32.GetThreadLocale(),
                        0,
                        IntPtr.Zero);
                }
            }
            finally
            {
                for (int i = 0; i < objAddrs.Length; i++)
                {
                    if (objAddrs[i] != IntPtr.Zero)
                    {
                        Marshal.Release(objAddrs[i]);
                    }
                }
            }
        }
Beispiel #7
0
        public unsafe override bool EditComponent(ITypeDescriptorContext context, object obj, IWin32Window parent)
        {
            IntPtr handle = (parent == null ? IntPtr.Zero : parent.Handle);

            // try to get the page guid
            if (obj is Oleaut32.IPerPropertyBrowsing)
            {
                // check for a property page
                Guid    guid = Guid.Empty;
                HRESULT hr   = ((Oleaut32.IPerPropertyBrowsing)obj).MapPropertyToPage(Ole32.DispatchID.MEMBERID_NIL, &guid);
                if (hr == HRESULT.S_OK & !guid.Equals(Guid.Empty))
                {
                    IntPtr pUnk = Marshal.GetIUnknownForObject(obj);
                    try
                    {
                        Oleaut32.OleCreatePropertyFrame(
                            new HandleRef(parent, handle),
                            0,
                            0,
                            "PropertyPages",
                            1,
                            &pUnk,
                            1,
                            &guid,
                            (uint)Application.CurrentCulture.LCID,
                            0,
                            IntPtr.Zero);
                        return(true);
                    }
                    finally
                    {
                        Marshal.Release(pUnk);
                    }
                }
            }

            if (obj is ISpecifyPropertyPages ispp)
            {
                try
                {
                    var     uuids = new CAUUID();
                    HRESULT hr    = ispp.GetPages(&uuids);
                    if (!hr.Succeeded() || uuids.cElems == 0)
                    {
                        return(false);
                    }

                    IntPtr pUnk = Marshal.GetIUnknownForObject(obj);
                    try
                    {
                        Oleaut32.OleCreatePropertyFrame(
                            new HandleRef(parent, handle),
                            0,
                            0,
                            "PropertyPages",
                            1,
                            &pUnk,
                            uuids.cElems,
                            uuids.pElems,
                            (uint)Application.CurrentCulture.LCID,
                            0,
                            IntPtr.Zero);
                        return(true);
                    }
                    finally
                    {
                        Marshal.Release(pUnk);
                        if (uuids.pElems != null)
                        {
                            Marshal.FreeCoTaskMem((IntPtr)uuids.pElems);
                        }
                    }
                }
                catch (Exception ex)
                {
                    string errString = SR.ErrorPropertyPageFailed;

                    IUIService uiSvc = (context != null) ? ((IUIService)context.GetService(typeof(IUIService))) : null;

                    if (uiSvc == null)
                    {
                        RTLAwareMessageBox.Show(null, errString, SR.PropertyGridTitle,
                                                MessageBoxButtons.OK, MessageBoxIcon.Error,
                                                MessageBoxDefaultButton.Button1, 0);
                    }
                    else if (ex != null)
                    {
                        uiSvc.ShowError(ex, errString);
                    }
                    else
                    {
                        uiSvc.ShowError(errString);
                    }
                }
            }

            return(false);
        }
Beispiel #8
0
            /// <summary>
            /// <para>Gets the TypeLibAttr corresponding to the TLB containing our ActiveX control.</para>
            /// </summary>
            private TYPELIBATTR GetTypeLibAttr()
            {
                string      controlKey = "CLSID\\" + clsid;
                RegistryKey key        = Registry.ClassesRoot.OpenSubKey(controlKey);

                if (key == null)
                {
                    if (AxToolSwitch.TraceVerbose)
                    {
                        Debug.WriteLine("No registry key found for: " + controlKey);
                    }
                    throw new ArgumentException(string.Format(SR.AXNotRegistered, controlKey.ToString()));
                }

                // Load the typelib into memory.
                //
                ITypeLib pTLB = null;

                // Open the key for the TypeLib
                //
                RegistryKey tlbKey = key.OpenSubKey("TypeLib");

                if (tlbKey != null)
                {
                    // Get the major and minor version numbers.
                    //
                    RegistryKey verKey = key.OpenSubKey("Version");
                    Debug.Assert(verKey != null, "No version registry key found for: " + controlKey);

                    string ver = (string)verKey.GetValue("");
                    int    dot = ver.IndexOf('.');

                    short majorVer;

                    short minorVer;
                    if (dot == -1)
                    {
                        majorVer = short.Parse(ver, CultureInfo.InvariantCulture);
                        minorVer = 0;
                    }
                    else
                    {
                        majorVer = short.Parse(ver.Substring(0, dot), CultureInfo.InvariantCulture);
                        minorVer = short.Parse(ver.Substring(dot + 1, ver.Length - dot - 1), CultureInfo.InvariantCulture);
                    }

                    Debug.Assert(majorVer > 0 && minorVer >= 0, "No Major version number found for: " + controlKey);
                    verKey.Close();

                    object o = tlbKey.GetValue("");

                    // Try to get the TypeLib's Guid.
                    //
                    var tlbGuid = new Guid((string)o);
                    Debug.Assert(!tlbGuid.Equals(Guid.Empty), "No valid Guid found for: " + controlKey);
                    tlbKey.Close();

                    try
                    {
                        pTLB = Oleaut32.LoadRegTypeLib(ref tlbGuid, majorVer, minorVer, Application.CurrentCulture.LCID);
                    }
                    catch (Exception e)
                    {
                        if (ClientUtils.IsCriticalException(e))
                        {
                            throw;
                        }
                    }
                }

                // Try to load the TLB directly from the InprocServer32.
                //
                // If that fails, try to load the TLB based on the TypeLib guid key.
                //
                if (pTLB == null)
                {
                    RegistryKey inprocServerKey = key.OpenSubKey("InprocServer32");
                    if (inprocServerKey != null)
                    {
                        string inprocServer = (string)inprocServerKey.GetValue("");
                        Debug.Assert(inprocServer != null, "No valid InprocServer32 found for: " + controlKey);
                        inprocServerKey.Close();

                        pTLB = Oleaut32.LoadTypeLib(inprocServer);
                    }
                }

                key.Close();

                if (pTLB != null)
                {
                    try
                    {
                        IntPtr pTlibAttr = NativeMethods.InvalidIntPtr;
                        pTLB.GetLibAttr(out pTlibAttr);
                        if (pTlibAttr == NativeMethods.InvalidIntPtr)
                        {
                            throw new ArgumentException(string.Format(SR.AXNotRegistered, controlKey.ToString()));
                        }
                        else
                        {
                            // Marshal the returned int as a TLibAttr structure
                            //
                            TYPELIBATTR typeLibraryAttributes = (TYPELIBATTR)Marshal.PtrToStructure(pTlibAttr, typeof(TYPELIBATTR));
                            pTLB.ReleaseTLibAttr(pTlibAttr);

                            return(typeLibraryAttributes);
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(pTLB);
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format(SR.AXNotRegistered, controlKey.ToString()));
                }
            }