private UInt64 SubtractTimes(ComTypes.FILETIME a, ComTypes.FILETIME b) { UInt64 aInt = ((UInt64) (a.dwHighDateTime << 32)) | (UInt64) a.dwLowDateTime; UInt64 bInt = ((UInt64) (b.dwHighDateTime << 32)) | (UInt64) b.dwLowDateTime; return aInt - bInt; }
internal ComTypeEnumDesc(ComTypes.ITypeInfo typeInfo, ComTypeLibDesc typeLibDesc) : base(typeInfo, ComType.Enum, typeLibDesc) { ComTypes.TYPEATTR typeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(typeInfo); string[] memberNames = new string[typeAttr.cVars]; object[] memberValues = new object[typeAttr.cVars]; IntPtr p = IntPtr.Zero; // For each enum member get name and value. for (int i = 0; i < typeAttr.cVars; i++) { typeInfo.GetVarDesc(i, out p); // Get the enum member value (as object). ComTypes.VARDESC varDesc; try { varDesc = (ComTypes.VARDESC)Marshal.PtrToStructure(p, typeof(ComTypes.VARDESC)); if (varDesc.varkind == ComTypes.VARKIND.VAR_CONST) { memberValues[i] = Marshal.GetObjectForNativeVariant(varDesc.desc.lpvarValue); } } finally { typeInfo.ReleaseVarDesc(p); } // Get the enum member name memberNames[i] = ComRuntimeHelpers.GetNameOfMethod(typeInfo, varDesc.memid); } _memberNames = memberNames; _memberValues = memberValues; }
internal ComTypeClassDesc(ComTypes.ITypeInfo typeInfo, ComTypeLibDesc typeLibDesc) : base(typeInfo, ComType.Class, typeLibDesc) { ComTypes.TYPEATTR typeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(typeInfo); Guid = typeAttr.guid; for (int i = 0; i < typeAttr.cImplTypes; i++) { int hRefType; typeInfo.GetRefTypeOfImplType(i, out hRefType); ComTypes.ITypeInfo currentTypeInfo; typeInfo.GetRefTypeInfo(hRefType, out currentTypeInfo); ComTypes.IMPLTYPEFLAGS implTypeFlags; typeInfo.GetImplTypeFlags(i, out implTypeFlags); bool isSourceItf = (implTypeFlags & ComTypes.IMPLTYPEFLAGS.IMPLTYPEFLAG_FSOURCE) != 0; AddInterface(currentTypeInfo, isSourceItf); } }
internal static ComTypeLibDesc GetFromTypeLib(ComTypes.ITypeLib typeLib) { // check whether we have already loaded this type library ComTypes.TYPELIBATTR typeLibAttr = ComRuntimeHelpers.GetTypeAttrForTypeLib(typeLib); ComTypeLibDesc typeLibDesc; lock (_CachedTypeLibDesc) { if (_CachedTypeLibDesc.TryGetValue(typeLibAttr.guid, out typeLibDesc)) { return typeLibDesc; } } typeLibDesc = new ComTypeLibDesc(); typeLibDesc._typeLibName = ComRuntimeHelpers.GetNameOfLib(typeLib); typeLibDesc._typeLibAttributes = typeLibAttr; int countTypes = typeLib.GetTypeInfoCount(); for (int i = 0; i < countTypes; i++) { ComTypes.TYPEKIND typeKind; typeLib.GetTypeInfoType(i, out typeKind); ComTypes.ITypeInfo typeInfo; typeLib.GetTypeInfo(i, out typeInfo); if (typeKind == ComTypes.TYPEKIND.TKIND_COCLASS) { ComTypeClassDesc classDesc = new ComTypeClassDesc(typeInfo, typeLibDesc); typeLibDesc._classes.AddLast(classDesc); } else if (typeKind == ComTypes.TYPEKIND.TKIND_ENUM) { ComTypeEnumDesc enumDesc = new ComTypeEnumDesc(typeInfo, typeLibDesc); typeLibDesc._enums.Add(enumDesc.TypeName, enumDesc); } else if (typeKind == ComTypes.TYPEKIND.TKIND_ALIAS) { ComTypes.TYPEATTR typeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(typeInfo); if (typeAttr.tdescAlias.vt == (short)VarEnum.VT_USERDEFINED) { string aliasName, documentation; ComRuntimeHelpers.GetInfoFromType(typeInfo, out aliasName, out documentation); ComTypes.ITypeInfo referencedTypeInfo; typeInfo.GetRefTypeInfo(typeAttr.tdescAlias.lpValue.ToInt32(), out referencedTypeInfo); ComTypes.TYPEATTR referencedTypeAttr = ComRuntimeHelpers.GetTypeAttrForTypeInfo(referencedTypeInfo); ComTypes.TYPEKIND referencedTypeKind = referencedTypeAttr.typekind; if (referencedTypeKind == ComTypes.TYPEKIND.TKIND_ENUM) { ComTypeEnumDesc enumDesc = new ComTypeEnumDesc(referencedTypeInfo, typeLibDesc); typeLibDesc._enums.Add(aliasName, enumDesc); } } } } // cached the typelib using the guid as the dictionary key lock (_CachedTypeLibDesc) { _CachedTypeLibDesc.Add(typeLibAttr.guid, typeLibDesc); } return typeLibDesc; }
unsafe void NativeMethods.IDispatch.Invoke( int dispid, ref Guid riid, int lcid, ComTypes.INVOKEKIND wFlags, ref ComTypes.DISPPARAMS pDispParams, IntPtr pvarResult, IntPtr pExcepInfo, IntPtr puArgErr) { ComEventsMethod method = FindMethod(dispid); if (method == null) return; // notice the unsafe pointers we are using. This is to avoid unnecessary // arguments marshalling. see code:ComEventsHelper#ComEventsArgsMarshalling object [] args = new object[pDispParams.cArgs]; int [] byrefsMap = new int[pDispParams.cArgs]; bool [] usedArgs = new bool[pDispParams.cArgs]; Variant* pvars = (Variant*)pDispParams.rgvarg; int* pNamedArgs = (int*)pDispParams.rgdispidNamedArgs; // copy the named args (positional) as specified int i; int pos; for (i = 0; i < pDispParams.cNamedArgs; i++) { pos = pNamedArgs[i]; Variant* pvar = GetVariant(&pvars[i]); args[pos] = pvar->ToObject(); usedArgs[pos] = true; if (pvar->IsByRef) { byrefsMap[pos] = i; } else { byrefsMap[pos] = -1; } } // copy the rest of the arguments in the reverse order pos = 0; for (; i < pDispParams.cArgs; i++) { // find the next unassigned argument while (usedArgs[pos]) { ++pos; } Variant* pvar = GetVariant(&pvars[pDispParams.cArgs - 1 - i]); args[pos] = pvar->ToObject(); if (pvar->IsByRef) byrefsMap[pos] = pDispParams.cArgs - 1 - i; else byrefsMap[pos] = -1; pos++; } // Do the actual delegate invocation object result; result = method.Invoke(args); // convert result to VARIANT if (pvarResult != IntPtr.Zero) { Marshal.GetNativeVariantForObject(result, pvarResult); } // Now we need to marshal all the byrefs back for (i = 0; i < pDispParams.cArgs; i++) { int idxToPos = byrefsMap[i]; if (idxToPos == -1) continue; GetVariant(&pvars[idxToPos])->CopyFromIndirect(args[i]); } }
internal static unsafe int Stat(System.IntPtr pComThis, out ComTypes.STATSTG pstatstg, int grfStatFlag) { __com_IStream* pIStream = (__com_IStream*)pComThis; pstatstg = new ComTypes.STATSTG(); pstatstg.cbSize = pIStream->m_cbSize; return Interop.COM.S_OK; }
private static extern void ReleaseStgMedium(ref ComTypes.STGMEDIUM pmedium);
private static void ScanSourceInterface(ComTypes.ITypeInfo sourceTypeInfo, ref Dictionary<string, ComEventDesc> events) { ComTypes.TYPEATTR sourceTypeAttribute = ComRuntimeHelpers.GetTypeAttrForTypeInfo(sourceTypeInfo); for (int index = 0; index < sourceTypeAttribute.cFuncs; index++) { IntPtr funcDescHandleToRelease = IntPtr.Zero; try { ComTypes.FUNCDESC funcDesc; GetFuncDescForDescIndex(sourceTypeInfo, index, out funcDesc, out funcDescHandleToRelease); // we are not interested in hidden or restricted functions for now. if ((funcDesc.wFuncFlags & (int)ComTypes.FUNCFLAGS.FUNCFLAG_FHIDDEN) != 0) { continue; } if ((funcDesc.wFuncFlags & (int)ComTypes.FUNCFLAGS.FUNCFLAG_FRESTRICTED) != 0) { continue; } string name = ComRuntimeHelpers.GetNameOfMethod(sourceTypeInfo, funcDesc.memid); name = name.ToUpper(System.Globalization.CultureInfo.InvariantCulture); // Sometimes coclass has multiple source interfaces. Usually this is caused by // adding new events and putting them on new interfaces while keeping the // old interfaces around. This may cause name collisioning which we are // resolving by keeping only the first event with the same name. if (events.ContainsKey(name) == false) { ComEventDesc eventDesc = new ComEventDesc(); eventDesc.dispid = funcDesc.memid; eventDesc.sourceIID = sourceTypeAttribute.guid; events.Add(name, eventDesc); } } finally { if (funcDescHandleToRelease != IntPtr.Zero) { sourceTypeInfo.ReleaseFuncDesc(funcDescHandleToRelease); } } } }
private static void GetFuncDescForDescIndex(ComTypes.ITypeInfo typeInfo, int funcIndex, out ComTypes.FUNCDESC funcDesc, out IntPtr funcDescHandle) { IntPtr pFuncDesc = IntPtr.Zero; typeInfo.GetFuncDesc(funcIndex, out pFuncDesc); // GetFuncDesc should never return null, this is just to be safe if (pFuncDesc == IntPtr.Zero) { throw Error.CannotRetrieveTypeInformation(); } funcDesc = (ComTypes.FUNCDESC)Marshal.PtrToStructure(pFuncDesc, typeof(ComTypes.FUNCDESC)); funcDescHandle = pFuncDesc; }
private static ComTypes.ITypeInfo GetCoClassTypeInfo(object rcw, ComTypes.ITypeInfo typeInfo) { Debug.Assert(typeInfo != null); IProvideClassInfo provideClassInfo = rcw as IProvideClassInfo; if (provideClassInfo != null) { IntPtr typeInfoPtr = IntPtr.Zero; try { provideClassInfo.GetClassInfo(out typeInfoPtr); if (typeInfoPtr != IntPtr.Zero) { return Marshal.GetObjectForIUnknown(typeInfoPtr) as ComTypes.ITypeInfo; } } finally { if (typeInfoPtr != IntPtr.Zero) { Marshal.Release(typeInfoPtr); } } } // retrieving class information through IPCI has failed - // we can try scanning the typelib to find the coclass ComTypes.ITypeLib typeLib; int typeInfoIndex; typeInfo.GetContainingTypeLib(out typeLib, out typeInfoIndex); string typeName = ComRuntimeHelpers.GetNameOfType(typeInfo); ComTypeLibDesc typeLibDesc = ComTypeLibDesc.GetFromTypeLib(typeLib); ComTypeClassDesc coclassDesc = typeLibDesc.GetCoClassForInterface(typeName); if (coclassDesc == null) { return null; } ComTypes.ITypeInfo typeInfoCoClass; Guid coclassGuid = coclassDesc.Guid; typeLib.GetTypeInfoOfGuid(ref coclassGuid, out typeInfoCoClass); return typeInfoCoClass; }
private void AddInterface(ComTypes.ITypeInfo itfTypeInfo, bool isSourceItf) { string itfName = ComRuntimeHelpers.GetNameOfType(itfTypeInfo); if (isSourceItf) { if (_sourceItfs == null) { _sourceItfs = new LinkedList<string>(); } _sourceItfs.AddLast(itfName); } else { if (_itfs == null) { _itfs = new LinkedList<string>(); } _itfs.AddLast(itfName); } }
private static extern bool GetSystemTimes( out ComTypes.FILETIME lpIdleTime, out ComTypes.FILETIME lpKernelTime, out ComTypes.FILETIME lpUserTime );
/// <summary> /// Handles DataChanged events from a COM IDataObject. /// </summary> /// <param name="format">The data format that had a change.</param> /// <param name="stgmedium">The data value.</param> public void OnDataChange(ref ComTypes.FORMATETC format, ref ComTypes.STGMEDIUM stgmedium) { // We listen to DropDescription changes, so that we can unset the IsDefault // drop description flag. object odd = ComTypes.ComDataObjectExtensions.GetDropDescription((ComTypes.IDataObject)data); if (odd != null) DragSourceHelper.SetDropDescriptionIsDefault(data, false); }