static void ExtractNames(string typelib) { UCOMITypeLib tl = null; OleAut.LoadTypeLibEx(typelib, OleAut.REGKIND_NONE, out tl); // Get the names from ITypeLib string name; string docstring; Int32 ctxt; string helpfile; tl.GetDocumentation(-1, out name, out docstring, out ctxt, out helpfile); IntPtr pa; tl.GetLibAttr(out pa); TYPELIBATTR ta = (TYPELIBATTR)Marshal.PtrToStructure(pa, typeof(TYPELIBATTR)); DealWithName(name, ta.guid); // Enumerate TypeInfos int nTypeInfos = tl.GetTypeInfoCount(); for (int i = 0; i < nTypeInfos; i++) { UCOMITypeInfo it = null; tl.GetTypeInfo(i, out it); DealWithTypeInfo(it); } }
protected void GetDocumentation(int index) { String docString; _iTypeLib.GetDocumentation(index, out _name, out docString, out _helpContext, out _helpFile); // Set doc string using the property so that it is checked // properly for null DocString = docString; }
private static void SetVersionInformation(AssemblyBuilder asmBldr, Object typeLib, AssemblyName asmName) { // Extract the name of the typelib. String strTypeLibName = null; String strDocString = null; int dwHelpContext = 0; String strHelpFile = null; UCOMITypeLib pTLB = (UCOMITypeLib)typeLib; pTLB.GetDocumentation(-1, out strTypeLibName, out strDocString, out dwHelpContext, out strHelpFile); // Generate the product name string from the named of the typelib. String strProductName = String.Format(Environment.GetResourceString("TypeLibConverter_ImportedTypeLibProductName"), strTypeLibName); // Set the OS version information. asmBldr.DefineVersionInfoResource(strProductName, asmName.Version.ToString(), null, null, null); }
/// <summary> /// This method creates (and saves) a .NET assembly given a COM /// type library. /// The name of the assembly is: "interop.[ComLibName].dll" /// The version of the assembly is: [ComLib.Major], [ComLib.Minor], 0, 0 /// </summary> public static UCOMITypeLib LoadCOMTypeInfo(string pathToComServer) { // Load type information for COM server. UCOMITypeLib typLib = null; try { LoadTypeLibEx(pathToComServer, REGKIND.REGKIND_NONE, out typLib); string strName, strDoc, strHelpFile; int helpCtx; Console.WriteLine("COM Library Description:"); typLib.GetDocumentation(-1, out strName, out strDoc, out helpCtx, out strHelpFile); Console.WriteLine("->Name: {0}", strName); Console.WriteLine("->Doc: {0}", strDoc); Console.WriteLine("->Help Context: {0}", helpCtx.ToString()); Console.WriteLine("->Help File: {0}", strHelpFile); } catch { Console.WriteLine("ugh...can't load COM type info!"); } return(typLib); }
public ComImporter(string path, OutputMessageCollection outputMessages, string outputDisplayName) { _outputMessages = outputMessages; _outputDisplayName = outputDisplayName; if (NativeMethods.SfcIsFileProtected(IntPtr.Zero, path) != 0) { outputMessages.AddWarningMessage("GenerateManifest.ComImport", outputDisplayName, _resources.GetString("ComImporter.ProtectedFile")); } object obj = null; try { NativeMethods.LoadTypeLibEx(path, NativeMethods.RegKind.RegKind_None, out obj); } catch (COMException) { } #pragma warning disable 618 UCOMITypeLib tlib = (UCOMITypeLib)obj; if (tlib != null) { IntPtr typeLibAttrPtr = IntPtr.Zero; tlib.GetLibAttr(out typeLibAttrPtr); TYPELIBATTR typeLibAttr = (TYPELIBATTR)Marshal.PtrToStructure(typeLibAttrPtr, typeof(TYPELIBATTR)); tlib.ReleaseTLibAttr(typeLibAttrPtr); Guid tlbid = typeLibAttr.guid; string name, docString, helpFile; int helpContext; tlib.GetDocumentation(-1, out name, out docString, out helpContext, out helpFile); string helpdir = Util.FilterNonprintableChars(helpFile); //Path.GetDirectoryName(helpFile); _typeLib = new TypeLib(tlbid, new Version(typeLibAttr.wMajorVerNum, typeLibAttr.wMinorVerNum), helpdir, typeLibAttr.lcid, Convert.ToInt32(typeLibAttr.wLibFlags, CultureInfo.InvariantCulture)); List <ComClass> comClassList = new List <ComClass>(); int count = tlib.GetTypeInfoCount(); for (int i = 0; i < count; ++i) { TYPEKIND tkind; tlib.GetTypeInfoType(i, out tkind); if (tkind == TYPEKIND.TKIND_COCLASS) { UCOMITypeInfo tinfo; tlib.GetTypeInfo(i, out tinfo); IntPtr tinfoAttrPtr = IntPtr.Zero; tinfo.GetTypeAttr(out tinfoAttrPtr); TYPEATTR tinfoAttr = (TYPEATTR)Marshal.PtrToStructure(tinfoAttrPtr, typeof(TYPEATTR)); tinfo.ReleaseTypeAttr(tinfoAttrPtr); Guid clsid = tinfoAttr.guid; string sclsid = clsid.ToString("B"); tlib.GetDocumentation(i, out name, out docString, out helpContext, out helpFile); string description = Util.FilterNonprintableChars(docString); ClassInfo info = GetRegisteredClassInfo(clsid); if (info == null) { continue; } comClassList.Add(new ComClass(tlbid, clsid, info.Progid, info.ThreadingModel, description)); } } if (comClassList.Count > 0) { _comClasses = comClassList.ToArray(); _success = true; } else { outputMessages.AddErrorMessage("GenerateManifest.ComImport", outputDisplayName, _resources.GetString("ComImporter.NoRegisteredClasses")); _success = false; } } else { outputMessages.AddErrorMessage("GenerateManifest.ComImport", outputDisplayName, _resources.GetString("ComImporter.TypeLibraryLoadFailure")); _success = false; } #pragma warning restore 618 }
public ComImporter(string path, OutputMessageCollection outputMessages, string outputDisplayName) { this.outputMessages = outputMessages; this.outputDisplayName = outputDisplayName; if (Microsoft.Build.Tasks.Deployment.ManifestUtilities.NativeMethods.SfcIsFileProtected(IntPtr.Zero, path) != 0) { outputMessages.AddWarningMessage("GenerateManifest.ComImport", new string[] { outputDisplayName, this.resources.GetString("ComImporter.ProtectedFile") }); } object typeLib = null; try { Microsoft.Build.Tasks.Deployment.ManifestUtilities.NativeMethods.LoadTypeLibEx(path, Microsoft.Build.Tasks.Deployment.ManifestUtilities.NativeMethods.RegKind.RegKind_None, out typeLib); } catch (COMException) { } UCOMITypeLib lib = (UCOMITypeLib)typeLib; if (lib != null) { string str; string str2; string str3; int num; IntPtr zero = IntPtr.Zero; lib.GetLibAttr(out zero); TYPELIBATTR typelibattr = (TYPELIBATTR)Marshal.PtrToStructure(zero, typeof(TYPELIBATTR)); lib.ReleaseTLibAttr(zero); Guid tlbId = typelibattr.guid; lib.GetDocumentation(-1, out str, out str2, out num, out str3); string helpDirectory = Microsoft.Build.Tasks.Deployment.ManifestUtilities.Util.FilterNonprintableChars(str3); this.typeLib = new Microsoft.Build.Tasks.Deployment.ManifestUtilities.TypeLib(tlbId, new Version(typelibattr.wMajorVerNum, typelibattr.wMinorVerNum), helpDirectory, typelibattr.lcid, Convert.ToInt32(typelibattr.wLibFlags, CultureInfo.InvariantCulture)); List <ComClass> list = new List <ComClass>(); int typeInfoCount = lib.GetTypeInfoCount(); for (int i = 0; i < typeInfoCount; i++) { TYPEKIND typekind; lib.GetTypeInfoType(i, out typekind); if (typekind == TYPEKIND.TKIND_COCLASS) { UCOMITypeInfo info; lib.GetTypeInfo(i, out info); IntPtr ppTypeAttr = IntPtr.Zero; info.GetTypeAttr(out ppTypeAttr); TYPEATTR typeattr = (TYPEATTR)Marshal.PtrToStructure(ppTypeAttr, typeof(TYPEATTR)); info.ReleaseTypeAttr(ppTypeAttr); Guid guid = typeattr.guid; guid.ToString("B"); lib.GetDocumentation(i, out str, out str2, out num, out str3); string description = Microsoft.Build.Tasks.Deployment.ManifestUtilities.Util.FilterNonprintableChars(str2); ClassInfo registeredClassInfo = this.GetRegisteredClassInfo(guid); if (registeredClassInfo != null) { list.Add(new ComClass(tlbId, guid, registeredClassInfo.Progid, registeredClassInfo.ThreadingModel, description)); } } } if (list.Count > 0) { this.comClasses = list.ToArray(); this.success = true; } else { outputMessages.AddErrorMessage("GenerateManifest.ComImport", new string[] { outputDisplayName, this.resources.GetString("ComImporter.NoRegisteredClasses") }); this.success = false; } } else { outputMessages.AddErrorMessage("GenerateManifest.ComImport", new string[] { outputDisplayName, this.resources.GetString("ComImporter.TypeLibraryLoadFailure") }); this.success = false; } }
internal static AssemblyName GetAssemblyNameFromTypelib(Object typeLib, String asmFileName, byte[] publicKey, StrongNameKeyPair keyPair, Version asmVersion) { // Extract the name of the typelib. TYPELIBATTR Attr; String strTypeLibName = null; String strDocString = null; int dwHelpContext = 0; String strHelpFile = null; UCOMITypeLib pTLB = (UCOMITypeLib)typeLib; pTLB.GetDocumentation(-1, out strTypeLibName, out strDocString, out dwHelpContext, out strHelpFile); // Retrieve the name to use for the assembly. if (asmFileName == null) { asmFileName = strTypeLibName; } else { BCLDebug.Assert(!String.Empty.Equals(asmFileName), "The assembly file name cannot be an empty string!"); String strFileNameNoPath = Path.GetFileName(asmFileName); String strExtension = Path.GetExtension(asmFileName); // Validate that the extension is valid. bool bExtensionValid = ".dll".Equals(strExtension.ToLower(CultureInfo.InvariantCulture)); // If the extension is not valid then tell the user and quit. if (!bExtensionValid) { throw new ArgumentException(Environment.GetResourceString("Arg_InvalidFileExtension")); } // The assembly cannot contain the path nor the extension. asmFileName = strFileNameNoPath.Substring(0, strFileNameNoPath.Length - ".dll".Length); } // If the version information was not specified, then retrieve it from the typelib. if (asmVersion == null) { IntPtr pAttr = Win32Native.NULL; try { pTLB.GetLibAttr(out pAttr); Attr = (TYPELIBATTR)Marshal.PtrToStructure(pAttr, typeof(TYPELIBATTR)); asmVersion = new Version(Attr.wMajorVerNum, Attr.wMinorVerNum, 0, 0); } finally { if (pAttr != Win32Native.NULL) { pTLB.ReleaseTLibAttr(pAttr); } } } // Create the assembly name for the imported typelib's assembly. AssemblyName AsmName = new AssemblyName(); AsmName.Init( asmFileName, publicKey, null, asmVersion, null, AssemblyHashAlgorithm.None, AssemblyVersionCompatibility.SameMachine, null, AssemblyNameFlags.None, keyPair, null); return(AsmName); }