Exemple #1
0
        void ComTypes.ITypeLib.GetDocumentation(int memid, out string strName, out string strDocString, out int dwHelpContext, out string strHelpFile)
        {
            // initialize out parameters
            strName       = default;
            strDocString  = default;
            dwHelpContext = default;
            strHelpFile   = default;

            using (var _name = AddressableVariables.CreateBSTR())
                using (var _docString = AddressableVariables.CreateBSTR())
                    using (var _helpContext = AddressableVariables.Create <int>())
                        using (var _Helpfile = AddressableVariables.CreateBSTR())
                        {
                            int hr = _this_Internal.GetDocumentation(memid, _name.Address, _docString.Address, _helpContext.Address, _Helpfile.Address);
                            if (ComHelper.HRESULT_FAILED(hr))
                            {
                                HandleBadHRESULT(hr);
                            }

                            strName       = _name.Value;
                            strDocString  = _docString.Value;
                            dwHelpContext = _helpContext.Value;
                            strHelpFile   = _Helpfile.Value;
                        }
        }
        /// <summary>
        /// Converts an unmanaged pointer into a <see cref="ComPointer{TComInterface}"/>, using
        /// aggregation rather than type-casting which may fail due to strict rules enforced by
        /// managed code.
        /// </summary>
        /// <param name="pUnk">
        /// Unmanaged IUnknown pointer
        /// </param>
        /// <param name="addRef">
        /// If true, an IUnknown::AddRef will be applied on the pointer. This is required when
        /// the pointer is provided via other methods besides the methods provided in the
        /// <see cref="System.Runtime.InteropServices.Marshal"/>.
        /// </param>
        /// <param name="queryType">
        /// Indicate if IUnknown::QueryInterface should be invoked to obtain the interface.
        /// Refer to <see cref="ComHelper.ComCastViaAggregation{T}"/> for details.
        /// </param>
        /// <returns>
        /// A <see cref="ComPointer{TComInterface}"/> encapsulating the IUnknown pointer and the
        /// managed interface. Use <see cref="Interface"/> to obtain the managed interface.
        /// </returns>
        internal static ComPointer <TComInterface> GetObjectViaAggregation(IntPtr pUnk, bool addRef, bool queryType)
        {
            var comInterface = ComHelper.ComCastViaAggregation <TComInterface>(pUnk, queryType);

            return(comInterface == null
                ? null
                : new ComPointer <TComInterface>(pUnk, comInterface));
        }
Exemple #3
0
        void ComTypes.ITypeInfo.GetDllEntry(int memid, ComTypes.INVOKEKIND invKind, IntPtr pBstrDllName, IntPtr pBstrName, IntPtr pwOrdinal)
        {
            // for some reason, the ComTypes.ITypeInfo definition for GetDllEntry uses the raw pointers for strings here,
            // just like our unfriendly version.  This makes it much easier for us to forward on
            var hr = _this_Internal.GetDllEntry(memid, invKind, pBstrDllName, pBstrName, pwOrdinal);

            if (ComHelper.HRESULT_FAILED(hr))
            {
                HandleBadHRESULT(hr);
            }
        }
Exemple #4
0
        // simply check if a COM object supports a particular COM interface
        // (without doing any casting by the CLR, which does much more than this under the covers)
        public static bool DoesComObjPtrSupportInterface <T>(IntPtr comObjPtr)
        {
            var iid = typeof(T).GUID;
            var hr  = RdMarshal.QueryInterface(comObjPtr, ref iid, out var outInterfacePtr);

            if (!ComHelper.HRESULT_FAILED(hr))
            {
                RdMarshal.Release(outInterfacePtr);
                return(true);
            }
            return(false);
        }
Exemple #5
0
        bool ComTypes.ITypeLib.IsName(string szNameBuf, int lHashVal)
        {
            using (var _pfName = AddressableVariables.Create <int>())
            {
                var hr = _this_Internal.IsName(szNameBuf, lHashVal, _pfName.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }

                return(_pfName.Value != 0);
            }
        }
Exemple #6
0
        void ComTypes.ITypeInfo.AddressOfMember(int memid, ComTypes.INVOKEKIND invKind, out IntPtr ppv)
        {
            // initialize out parameters
            ppv = default;

            using (var outPpv = AddressableVariables.Create <IntPtr>())
            {
                var hr = _this_Internal.AddressOfMember(memid, invKind, outPpv.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }
                ppv = outPpv.Value;
            }
        }
Exemple #7
0
        void ComTypes.ITypeInfo.GetRefTypeInfo(int hRef, out ComTypes.ITypeInfo ppTI)
        {
            // initialize out parameters
            ppTI = default;

            using (var typeInfoPtr = AddressableVariables.CreateObjectPtr <ComTypes.ITypeInfo>())
            {
                var hr = _this_Internal.GetRefTypeInfo(hRef, typeInfoPtr.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }
                ppTI = typeInfoPtr.Value;
            }
        }
Exemple #8
0
        void ComTypes.ITypeLib.GetTypeInfoOfGuid(ref Guid guid, out ComTypes.ITypeInfo ppTInfo)
        {
            // initialize out parameters
            ppTInfo = default;

            using (var typeInfoPtr = AddressableVariables.CreateObjectPtr <ComTypes.ITypeInfo>())
            {
                var hr = _this_Internal.GetTypeInfoOfGuid(guid, typeInfoPtr.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }
                ppTInfo = typeInfoPtr.Value;
            }
        }
Exemple #9
0
        void ComTypes.ITypeLib.GetTypeInfoType(int index, out ComTypes.TYPEKIND pTKind)
        {
            // initialize out parameters
            pTKind = default;

            using (var typeKindPtr = AddressableVariables.Create <ComTypes.TYPEKIND>())
            {
                var hr = _this_Internal.GetTypeInfoType(index, typeKindPtr.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }
                pTKind = typeKindPtr.Value;
            }
        }
Exemple #10
0
        void ComTypes.ITypeLib.GetTypeComp(out ComTypes.ITypeComp ppTComp)
        {
            // initialize out parameters
            ppTComp = default;

            using (var typeCompPtr = AddressableVariables.CreateObjectPtr <ComTypes.ITypeComp>())
            {
                var hr = _this_Internal.GetTypeComp(typeCompPtr.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }
                ppTComp = typeCompPtr.Value;
            }
        }
Exemple #11
0
        void ComTypes.ITypeLib.GetLibAttr(out IntPtr ppTLibAttr)
        {
            // initialize out parameters
            ppTLibAttr = default;

            using (var typeLibAttrPtr = AddressableVariables.CreatePtrTo <ComTypes.TYPELIBATTR>())
            {
                var hr = _this_Internal.GetLibAttr(typeLibAttrPtr.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }
                ppTLibAttr = typeLibAttrPtr.Value.Address;
            }
        }
Exemple #12
0
        void ComTypes.ITypeInfo.GetVarDesc(int index, out IntPtr ppVarDesc)
        {
            // initialize out parameters
            ppVarDesc = default;

            using (var varDescPtr = AddressableVariables.CreatePtrTo <ComTypes.VARDESC>())
            {
                var hr = _this_Internal.GetVarDesc(index, varDescPtr.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }
                ppVarDesc = varDescPtr.Value.Address;          // dereference the ptr, and take the contents address
            }
        }
Exemple #13
0
        void ComTypes.ITypeInfo.GetRefTypeOfImplType(int index, out int href)
        {
            // initialize out parameters
            href = default;

            using (var outHref = AddressableVariables.Create <int>())
            {
                var hr = _this_Internal.GetRefTypeOfImplType(index, outHref.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }
                href = outHref.Value;
            }
        }
Exemple #14
0
        void ComTypes.ITypeInfo.GetImplTypeFlags(int index, out ComTypes.IMPLTYPEFLAGS pImplTypeFlags)
        {
            // initialize out parameters
            pImplTypeFlags = default;

            using (var implTypeFlags = AddressableVariables.Create <ComTypes.IMPLTYPEFLAGS>())
            {
                var hr = _this_Internal.GetImplTypeFlags(index, implTypeFlags.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }

                pImplTypeFlags = implTypeFlags.Value;
            }
        }
Exemple #15
0
 void ComTypes.ITypeInfo.GetIDsOfNames(string[] rgszNames, int cNames, int[] pMemId)
 {
     // We can't use the managed arrays as passed in.  We create our own unmanaged arrays,
     // and copy them into the managed ones on completion
     using (var names = AddressableVariables.CreateBSTR(cNames))
         using (var memberIds = AddressableVariables.Create <int>(cNames))
         {
             var hr = _this_Internal.GetIDsOfNames(names.Address, cNames, memberIds.Address);
             if (ComHelper.HRESULT_FAILED(hr))
             {
                 HandleBadHRESULT(hr);
             }
             names.CopyArrayTo(rgszNames);
             memberIds.CopyArrayTo(pMemId);
         }
 }
Exemple #16
0
        void ComTypes.ITypeInfo.GetMops(int memid, out string pBstrMops)
        {
            // initialize out parameters
            pBstrMops = default;

            using (var strMops = AddressableVariables.CreateBSTR())
            {
                var hr = _this_Internal.GetMops(memid, strMops.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }

                pBstrMops = strMops.Value;
            }
        }
Exemple #17
0
        void ComTypes.ITypeInfo.GetTypeAttr(out IntPtr ppTypeAttr)
        {
            // initialize out parameters
            ppTypeAttr = default;

            using (var typeAttrPtr = AddressableVariables.CreatePtrTo <ComTypes.TYPEATTR>())
            {
                var hr = _this_Internal.GetTypeAttr(typeAttrPtr.Address);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }

                ppTypeAttr = typeAttrPtr.Value.Address;        // dereference the ptr, and take the contents address
            }
        }
Exemple #18
0
        void ComTypes.ITypeInfo.GetNames(int memid, string[] rgBstrNames, int cMaxNames, out int pcNames)
        {
            // initialize out parameters
            pcNames = default;

            using (var names = AddressableVariables.CreateBSTR(cMaxNames))
                using (var namesCount = AddressableVariables.Create <int>())
                {
                    var hr = _this_Internal.GetNames(memid, names.Address, cMaxNames, namesCount.Address);
                    if (ComHelper.HRESULT_FAILED(hr))
                    {
                        HandleBadHRESULT(hr);
                    }
                    names.CopyArrayTo(rgBstrNames);
                    pcNames = namesCount.Value;
                }
        }
Exemple #19
0
        void ComTypes.ITypeInfo.CreateInstance(object pUnkOuter, ref Guid riid, out object ppvObj)
        {
            // initialize out parameters
            ppvObj = default;

            using (var outPpvObj = AddressableVariables.CreateObjectPtr <object>())
            {
                var unkOuter = RdMarshal.GetIUnknownForObject(pUnkOuter);
                var hr       = _this_Internal.CreateInstance(unkOuter, riid, outPpvObj.Address);
                RdMarshal.Release(unkOuter);
                if (ComHelper.HRESULT_FAILED(hr))
                {
                    HandleBadHRESULT(hr);
                }

                ppvObj = outPpvObj.Value;
            }
        }
Exemple #20
0
        void ComTypes.ITypeInfo.GetContainingTypeLib(out ComTypes.ITypeLib ppTLB, out int pIndex)
        {
            // initialize out parameters
            ppTLB  = default;
            pIndex = default;

            using (var typeLibPtr = AddressableVariables.CreateObjectPtr <ComTypes.ITypeLib>())
                using (var indexPtr = AddressableVariables.Create <int>())
                {
                    var hr = _this_Internal.GetContainingTypeLib(typeLibPtr.Address, indexPtr.Address);
                    if (ComHelper.HRESULT_FAILED(hr))
                    {
                        HandleBadHRESULT(hr);
                    }

                    ppTLB  = typeLibPtr.Value;
                    pIndex = indexPtr.Value;
                }
        }
Exemple #21
0
        void ComTypes.ITypeLib.FindName(string szNameBuf, int lHashVal, ComTypes.ITypeInfo[] ppTInfo, int[] rgMemId, ref short pcFound)
        {
            // We can't use the managed arrays as passed in.  We create our own unmanaged arrays,
            // and copy them into the managed ones on completion
            using (var _ppTInfo = AddressableVariables.CreateObjectPtr <ComTypes.ITypeInfo>(pcFound))
                using (var _MemIds = AddressableVariables.Create <int>(pcFound))
                    using (var _pcFound = AddressableVariables.Create <short>())
                    {
                        var hr = _this_Internal.FindName(szNameBuf, lHashVal, _ppTInfo.Address, _MemIds.Address, _pcFound.Address);
                        if (ComHelper.HRESULT_FAILED(hr))
                        {
                            HandleBadHRESULT(hr);
                        }

                        _ppTInfo.CopyArrayTo(ppTInfo);
                        _MemIds.CopyArrayTo(rgMemId);
                        pcFound = _pcFound.Value;
                    }
        }
        /// <summary>
        /// A basic helper for IDispatch::Invoke
        /// </summary>
        /// <param name="obj">The IDispatch object of which you want to invoke a member on</param>
        /// <param name="memberId">The dispatch ID of the member to invoke</param>
        /// <param name="invokeKind">See InvokeKind enumeration</param>
        /// <param name="args">Array of arguments to pass to the call, or null for no args</param>
        /// <remarks>TODO support DISPATCH_PROPERTYPUTREF (property-set) which requires special handling</remarks>
        /// <returns>An object representing the return value from the called routine</returns>
        public static object Invoke(IDispatch obj, int memberId, InvokeKind invokeKind, object[] args = null)
        {
            var pDispParams = PrepareDispatchArgs(args);
            var pExcepInfo  = new ComTypes.EXCEPINFO();

            var hr = obj.Invoke(memberId, ref _guid_null, 0, (uint)invokeKind,
                                ref pDispParams, out var pVarResult, ref pExcepInfo, out var pErrArg);

            UnprepareDispatchArgs(pDispParams);

            if (ComHelper.HRESULT_FAILED(hr))
            {
                if ((hr == (int)KnownComHResults.DISP_E_EXCEPTION) && (ComHelper.HRESULT_FAILED(pExcepInfo.scode)))
                {
                    throw RdMarshal.GetExceptionForHR(pExcepInfo.scode);
                }
                throw RdMarshal.GetExceptionForHR(hr);
            }

            return(pVarResult);
        }
Exemple #23
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="outerObject">The object that needs interface requests filtered</param>
        /// <param name="queryForType">determines whether we call QueryInterface for the interface or not</param>
        /// <remarks>if the passed in outerObject is known to point to the correct vtable for the interface, then queryForType can be false</remarks>
        /// <returns>if outerObject is IntPtr.Zero, then a null wrapper, else an aggregated wrapper</returns>
        public RestrictComInterfaceByAggregation(IntPtr outerObject, bool queryForType = true)
        {
            if (queryForType)
            {
                var iid = typeof(T).GUID;
                if (ComHelper.HRESULT_FAILED(RdMarshal.QueryInterface(outerObject, ref iid, out _outerObject)))
                {
                    // allow null wrapping here
                    return;
                }
            }
            else
            {
                _outerObject = outerObject;
                RdMarshal.AddRef(_outerObject);
            }

            var clrAggregator = RdMarshal.CreateAggregatedObject(_outerObject, this);

            WrappedObject = (T)RdMarshal.GetObjectForIUnknown(clrAggregator); // when this CCW object gets released, it will free the aggObjInner (well, after GC)
            RdMarshal.Release(clrAggregator);                                 // _wrappedObject holds a reference to this now
        }