Exemple #1
0
        /// <include file='doc\AxImporter.uex' path='docs/doc[@for="AxImporter.GenerateFromTypeLibrary1"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Generates a wrapper for an ActiveX control for use in the design-time
        ///       environment.
        ///    </para>
        /// </devdoc>
        public string GenerateFromTypeLibrary(UCOMITypeLib typeLib, Guid clsid)
        {
            string   axWFW     = null;
            string   axctlType = null;
            Assembly rcw       = null;

            // Look to see if we can find the AxWrapper also for this typeLib.
            //
            axWFW = GetAxReference(typeLib);

            if (axWFW != null && clsid != Guid.Empty)
            {
                axctlType = GetAxTypeFromAssembly(axWFW, clsid);
            }

            if (axWFW == null)
            {
                string tlbName = Marshal.GetTypeLibName(typeLib);
                string rcwName = Path.Combine(options.outputDirectory, tlbName + ".dll");
                Debug.WriteLineIf(AxWrapperGen.AxWrapper.Enabled, "Converting TypeLib Name: " + tlbName + " in " + rcwName);

                AddReferencedAssembly(GetManagedReference("System.Windows.Forms"));
                AddReferencedAssembly(GetManagedReference("System.Drawing"));
                AddReferencedAssembly(GetManagedReference("System"));

                string rcwAssem = GetComReference(typeLib);
                if (rcwAssem != null)
                {
                    AddReferencedAssembly(rcwAssem);
                    rcw = GetCopiedAssembly(rcwAssem, false, false);
                    AddDependentAssemblies(rcw);
                }
                else
                {
                    TypeLibConverter tlbConverter = new TypeLibConverter();

                    // Try to locate the primary interop assembly first.
                    //
                    rcw = GetPrimaryInteropAssembly(typeLib, tlbConverter);

                    if (rcw != null)
                    {
                        rcwAssem = GetLocalPath(rcw.EscapedCodeBase);
                        AddDependentAssemblies(rcw);
                    }
                    else
                    {
                        AssemblyBuilder asmBldr = tlbConverter.ConvertTypeLibToAssembly(typeLib,
                                                                                        rcwName,
                                                                                        (TypeLibImporterFlags)0,
                                                                                        new ImporterCallback(this),
                                                                                        options.publicKey,
                                                                                        options.keyPair,
                                                                                        null,
                                                                                        null);

                        if (rcwAssem == null)
                        {
                            // Save the assembly to the disk only if we did not find it already on the reference list.
                            //
                            rcwAssem = SaveAssemblyBuilder(typeLib, asmBldr, rcwName);
                            rcw      = GetCopiedAssembly(rcwAssem, false, false);
                        }
                    }
                }
                Debug.Assert(rcw != null, "No assembly obtained from: " + rcwAssem);

                // Create a list of the referenced assemblies and create the WFW Wrapper for the AxControl.
                //
                int      i         = 0;
                string[] refAssems = new string[this.refAssems.Count];
                foreach (string assem in this.refAssems)
                {
                    string name = assem;

                    name = name.Replace("%20", " ");
                    Debug.WriteLineIf(AxWrapperGen.AxWrapper.Enabled, "Adding " + name + " to the wrapper reference list...");
                    refAssems[i++] = name;
                }

                if (axctlType == null)
                {
                    string   file         = GetFileOfTypeLib(typeLib);
                    DateTime tlbTimeStamp = (file == null) ? DateTime.Now : File.GetLastWriteTime(file);

                    // Hook up the type resolution events for the appdomain so we can delay load
                    // any assemblies/types users gave us in the /i option.
                    //
                    ResolveEventHandler assemblyResolveEventHandler = new ResolveEventHandler(OnAssemblyResolve);
                    AppDomain.CurrentDomain.AssemblyResolve += assemblyResolveEventHandler;

                    try {
                        if (options.genSources)
                        {
                            AxWrapperGen.GeneratedSources = new ArrayList();
                        }

                        if (options.outputName == null)
                        {
                            options.outputName = "Ax" + tlbName + ".dll";
                        }

                        axctlType = AxWrapperGen.GenerateWrappers(this, clsid, rcw, refAssems, tlbTimeStamp, out axWFW);

                        if (options.genSources)
                        {
                            generatedSources = AxWrapperGen.GeneratedSources;
                        }
                    }
                    finally {
                        AppDomain.CurrentDomain.AssemblyResolve -= assemblyResolveEventHandler;
                    }

                    if (axctlType == null)
                    {
                        throw new Exception(SR.GetString(SR.AXNoActiveXControls, ((typeLibName != null) ? typeLibName : tlbName)));
                    }
                }

                if (axctlType != null)
                {
                    // Add the WFW assembly to the references list.
                    //
                    Debug.Assert(axWFW != null && axWFW.Length > 0, "Invalid output assembly name");
                    AddReferencedAssembly(axWFW);
                    AddTypeLibAttr(typeLib);
                    AddGeneratedAssembly(axWFW);
                }
            }

            return(axctlType);
        }
        public string GenerateFromTypeLibrary(UCOMITypeLib typeLib, Guid clsid)
        {
            string   fileName           = null;
            string   axTypeFromAssembly = null;
            Assembly assem = null;

            fileName = this.GetAxReference((System.Runtime.InteropServices.ComTypes.ITypeLib)typeLib);
            if ((fileName != null) && (clsid != Guid.Empty))
            {
                axTypeFromAssembly = this.GetAxTypeFromAssembly(fileName, clsid);
            }
            if (fileName == null)
            {
                string typeLibName = Marshal.GetTypeLibName((System.Runtime.InteropServices.ComTypes.ITypeLib)typeLib);
                string asmFileName = Path.Combine(this.options.outputDirectory, typeLibName + ".dll");
                this.AddReferencedAssembly(this.GetManagedReference("System.Windows.Forms"));
                this.AddReferencedAssembly(this.GetManagedReference("System.Drawing"));
                this.AddReferencedAssembly(this.GetManagedReference("System"));
                string comReference = this.GetComReference((System.Runtime.InteropServices.ComTypes.ITypeLib)typeLib);
                if (comReference != null)
                {
                    this.AddReferencedAssembly(comReference);
                    assem = this.GetCopiedAssembly(comReference, false, false);
                    this.AddDependentAssemblies(assem, comReference);
                }
                else
                {
                    TypeLibConverter tlbConverter = new TypeLibConverter();
                    assem = this.GetPrimaryInteropAssembly((System.Runtime.InteropServices.ComTypes.ITypeLib)typeLib, tlbConverter);
                    if (assem != null)
                    {
                        comReference = this.GetLocalPath(assem.EscapedCodeBase);
                        this.AddDependentAssemblies(assem, comReference);
                    }
                    else
                    {
                        AssemblyBuilder asmBldr = tlbConverter.ConvertTypeLibToAssembly((System.Runtime.InteropServices.ComTypes.ITypeLib)typeLib, asmFileName, TypeLibImporterFlags.None, new ImporterCallback(this), this.options.publicKey, this.options.keyPair, null, null);
                        if (comReference == null)
                        {
                            comReference = this.SaveAssemblyBuilder((System.Runtime.InteropServices.ComTypes.ITypeLib)typeLib, asmBldr, asmFileName);
                            assem        = asmBldr;
                        }
                    }
                }
                int      num           = 0;
                string[] refAssemblies = new string[this.refAssems.Count];
                foreach (string str6 in this.refAssems)
                {
                    string str7 = str6;
                    str7 = str7.Replace("%20", " ");
                    refAssemblies[num++] = str7;
                }
                if (axTypeFromAssembly == null)
                {
                    string              fileOfTypeLib = GetFileOfTypeLib((System.Runtime.InteropServices.ComTypes.ITypeLib)typeLib);
                    DateTime            tlbTimeStamp  = (fileOfTypeLib == null) ? DateTime.Now : File.GetLastWriteTime(fileOfTypeLib);
                    ResolveEventHandler handler       = new ResolveEventHandler(this.OnAssemblyResolve);
                    AppDomain.CurrentDomain.AssemblyResolve += handler;
                    AppDomain.CurrentDomain.TypeResolve     += new ResolveEventHandler(this.OnTypeResolve);
                    try
                    {
                        if (this.options.genSources)
                        {
                            AxWrapperGen.GeneratedSources = new ArrayList();
                        }
                        if (this.options.outputName == null)
                        {
                            this.options.outputName = "Ax" + typeLibName + ".dll";
                        }
                        axTypeFromAssembly = AxWrapperGen.GenerateWrappers(this, clsid, assem, refAssemblies, tlbTimeStamp, out fileName);
                        if (this.options.genSources)
                        {
                            this.generatedSources = AxWrapperGen.GeneratedSources;
                        }
                    }
                    finally
                    {
                        AppDomain.CurrentDomain.AssemblyResolve -= handler;
                        AppDomain.CurrentDomain.TypeResolve     -= new ResolveEventHandler(this.OnTypeResolve);
                    }
                    if (axTypeFromAssembly == null)
                    {
                        string message = System.Design.SR.GetString("AXNoActiveXControls", new object[] { (this.typeLibName != null) ? this.typeLibName : typeLibName });
                        if (this.options.msBuildErrors)
                        {
                            message = "AxImp: error aximp000: " + message;
                        }
                        throw new Exception(message);
                    }
                }
                if (axTypeFromAssembly != null)
                {
                    this.AddReferencedAssembly(fileName);
                    this.AddTypeLibAttr((System.Runtime.InteropServices.ComTypes.ITypeLib)typeLib);
                    this.AddGeneratedAssembly(fileName);
                }
            }
            return(axTypeFromAssembly);
        }