private Assembly CreateActiveXWrapper(string ocxFileName)
        {
            bool hasOutputDirectory = !string.IsNullOrEmpty(OutputDirectory);

            if (hasOutputDirectory && !Directory.Exists(OutputDirectory))
            {
                Directory.CreateDirectory(OutputDirectory);
            }

            if (_options == null)
            {
                _options = new AxImporter.Options();
                _options.outputDirectory = OutputDirectory;
                _options.keyPair = KeyPair;
                _options.references = new AxImporterResolver(ImportedFileNames);
            }
            else
            {
                _options.outputName = null;
            }

            AxImporter importer = new AxImporter(_options);
            importer.GenerateFromFile(new FileInfo(ocxFileName));

            foreach (string fileName in importer.GeneratedAssemblies)
            {
                _existingWrappers.Add(Path.GetFileNameWithoutExtension(fileName), fileName);
                Assembly libraryAssembly = Assembly.LoadFrom(fileName);
                ImportedFileNames[libraryAssembly.FullName] = Path.GetFileName(fileName);
            }

            return Assembly.LoadFrom(hasOutputDirectory
                                         ? Path.Combine(OutputDirectory, _options.outputName)
                                         : _options.outputName);
        }
 internal static string GenerateWrappers(AxImporter importer, Guid axClsid, Assembly rcwAssem, string[] refAssemblies, DateTime tlbTimeStamp, out string assem)
 {
     assem = null;
     bool flag = false;
     CodeNamespace ns = null;
     string axctl = null;
     try
     {
         System.Type[] types = rcwAssem.GetTypes();
         for (int i = 0; i < types.Length; i++)
         {
             if (IsTypeActiveXControl(types[i]))
             {
                 flag = true;
                 if (ns == null)
                 {
                     axctlNS = "Ax" + types[i].Namespace;
                     ns = new CodeNamespace(axctlNS);
                 }
                 AxWrapperGen gen = new AxWrapperGen(types[i]);
                 gen.GenerateAxHost(ns, refAssemblies);
                 if (!axClsid.Equals(Guid.Empty) && axClsid.Equals(types[i].GUID))
                 {
                     axctl = gen.axctl;
                 }
                 else if (axClsid.Equals(Guid.Empty) && (axctl == null))
                 {
                     axctl = gen.axctl;
                 }
             }
         }
     }
     finally
     {
         if (classesInNamespace != null)
         {
             classesInNamespace.Clear();
             classesInNamespace = null;
         }
     }
     AssemblyName name = rcwAssem.GetName();
     if (flag)
     {
         Version version = name.Version;
         assem = Compile(importer, ns, refAssemblies, tlbTimeStamp, version);
         if (assem != null)
         {
             if (axctl == null)
             {
                 throw new Exception(System.Design.SR.GetString("AXNotValidControl", new object[] { "{" + axClsid + "}" }));
             }
             return (axctlNS + "." + axctl + "," + axctlNS);
         }
     }
     return null;
 }
 private static string Compile(AxImporter importer, CodeNamespace ns, string[] refAssemblies, DateTime tlbTimeStamp, Version version)
 {
     CompilerResults results;
     ICodeGenerator codegen = new CSharpCodeProvider().CreateGenerator();
     string outputName = importer.options.outputName;
     string path = Path.Combine(importer.options.outputDirectory, outputName);
     string fileName = Path.ChangeExtension(path, ".cs");
     CompilerParameters options = new CompilerParameters(refAssemblies, path) {
         IncludeDebugInformation = importer.options.genSources
     };
     CodeCompileUnit cu = new CodeCompileUnit();
     cu.Namespaces.Add(ns);
     CodeAttributeDeclarationCollection assemblyCustomAttributes = cu.AssemblyCustomAttributes;
     assemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyVersion", new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression(version.ToString())) }));
     assemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Windows.Forms.AxHost.TypeLibraryTimeStamp", new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression(tlbTimeStamp.ToString())) }));
     if (importer.options.delaySign)
     {
         assemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyDelaySign", new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression(true)) }));
     }
     if ((importer.options.keyFile != null) && (importer.options.keyFile.Length > 0))
     {
         assemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyKeyFile", new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression(importer.options.keyFile)) }));
     }
     if ((importer.options.keyContainer != null) && (importer.options.keyContainer.Length > 0))
     {
         assemblyCustomAttributes.Add(new CodeAttributeDeclaration("System.Reflection.AssemblyKeyName", new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression(importer.options.keyContainer)) }));
     }
     if (importer.options.genSources)
     {
         SaveCompileUnit(codegen, cu, fileName);
         results = ((ICodeCompiler) codegen).CompileAssemblyFromFile(options, fileName);
     }
     else
     {
         results = ((ICodeCompiler) codegen).CompileAssemblyFromDom(options, cu);
     }
     if ((results.Errors != null) && (results.Errors.Count > 0))
     {
         string str4 = null;
         foreach (CompilerError error in results.Errors)
         {
             if (!error.IsWarning)
             {
                 str4 = str4 + error.ToString() + "\r\n";
             }
         }
         if (str4 != null)
         {
             SaveCompileUnit(codegen, cu, fileName);
             throw new Exception(System.Design.SR.GetString("AXCompilerError", new object[] { ns.Name, fileName }) + "\r\n" + str4);
         }
     }
     return path;
 }
예제 #4
0
		public AxImporter (AxImporter.Options options)
		{
			this.options = options;
		}
예제 #5
0
		// This translates the type library into an assembly
		internal void TranslateTypeLib()
		{
			TraceUtil.WriteLineInfo(this, "TranslateTypeLib");
			// Already associated with an assembly
			if (_assy != null)
				return;
			ReadTypeLibFile();
			if (_iTypeLib == null)
				throw new Exception("Failed to open file: " + _fileName);
			// If this is present, we can't translate the assembly,
			// we just have to try and load it using this name.  This 
			// is the case for things like mscorlib.tlb 
			if (_translatedAssyFullName != null) {
				Assembly assy = null;
				try {
					String fileName;
					// We will have the file name if we are coming through
					// here after the type library has been opened previously
					// (when it is being restored from the registry).
					if (_assyInfo._fileName != null)
						fileName = _assyInfo._fileName;
					else
						fileName = _translatedAssyFullName;
					TraceUtil.WriteLineInfo
						(null,
						"TypeLib - translated assy looking for: " 
						+ fileName);
					assy = Assembly.Load(fileName);
				} catch (FileNotFoundException) {
					// Don't ask the user if we have already asked
					// the user
					if (!_dontFindAssy)
						assy = FindAssemblyFile();
				}
			
				TraceUtil.WriteLineInfo(null,
										"TypeLib - source assy is: "
										+ assy
										+ " for translated lib "
										+ _translatedAssyFullName);
				RecordTranslatedAssy(assy);
				return;
			}
			CheckForPrimaryInteropAssy();
			if (_primaryInteropAssyName != null)
				return;
			// Only create the versioned names if the primary interop assy
			// is not used, since that's not versioned.
			CreateAssyNames();
			TraceUtil.WriteLineIf(null, TraceLevel.Info,
								 "TypeLib - converting - url: " 
								 + _assyInfo._url
								 + " file: " + _assyInfo._fileName);
			String dir = ComponentInspectorProperties.ConvertedAssemblyDirectory;
			Directory.CreateDirectory(dir);
			Directory.SetCurrentDirectory(dir);
			if (IsAssyCurrent(_assyInfo._fileName, _fileName)) {
				// See if there is a translated version already present.
				// We don't know if there should be an Ax version or not,
				// try the normal one and if it succeeds, try the Ax version.
				// If the normal version succeeds however, we can consider
				// that we are done.
				RecordTranslatedAssy(Assembly.LoadFrom(_assyInfo._url));
				TraceUtil.WriteLineInfo(null,
										"TypeLib - found previous xlated "
										+ _assyInfo._name);
				try {
					RecordTranslatedAssy(Assembly.LoadFrom(_axAssyInfo._url));
				} catch {
					// Ignore
				}
				return;
			}
			// Convert the type library to an assembly
				
			// The assembly load event happens in here, it is going to
			// be unable to add the assembly to the registry since there
			// is no CodeBase associated with a dynamically created 
			// assembly, but that's not a problem since we add it below
			// when adding the GUID for the typelib
			ProgressDialog progress = new ProgressDialog();
			progress.Setup("Converting " + GetName() 
						   + " to .NET Assembly",
						  "Please wait while I convert " 
						  + GetName()
						   + " to a .NET Assembly",
						  ProgressDialog.NO_PROGRESS_BAR,
						   !ProgressDialog.HAS_PROGRESS_TEXT,
						  ProgressDialog.FINAL);
			progress.ShowIfNotDone();
			try {
				TypeLibConverter converter = new TypeLibConverter();
				AssemblyBuilder asm = converter.ConvertTypeLibToAssembly
					(_iTypeLib, 
					_assyInfo._fileName,
					// Can't do this because we can't strong name these
					 //TypeLibImporterFlags.PrimaryInteropAssembly, 
					0,
					this,
					null, null, null, null);
				// For the problem associated with Q316653
				//FixEventSinkHelpers(asm);
				asm.Save(_assyInfo._fileName);
				RecordTranslatedAssy(Assembly.LoadFrom(_assyInfo._url));
				// Generate the ActiveX wrapper for the typelib, 
				// if that's necessary
				AxImporter.Options impOptions = new AxImporter.Options();
				impOptions.outputDirectory = dir;
				impOptions.outputName = _axAssyInfo._fileName;
				impOptions.references = this;
				impOptions.genSources = true;
				AxImporter axImport = new AxImporter(impOptions);
				try {
					// This converts the type library and generates the
					// wrapper for any ActiveX controls.  It produces 
					// a list of the assemblies that it created.  If there
					// are no ActiveX controls this will throw.
					String wrapper = axImport.
						GenerateFromTypeLibrary(_iTypeLib);
				} catch (Exception ex) {
					if (ex.Message.StartsWith("Did not find a")) {
						TraceUtil.WriteLineInfo(this, 
												"TypeLib - no controls found");
						return;
					}
					TraceUtil.WriteLineWarning(this, 
											  "TypeLib - AxImporter error "
											  + _assyInfo._url + " " 
											  + ex);
					throw new Exception("Error in AxImporter for: "
										+ _assyInfo._url, ex);
				}
				foreach (String aname in axImport.GeneratedAssemblies) {
					TraceUtil.WriteLineInfo(this, "TypeLib - AX gen assy: " 
											+ aname);
				}
				foreach (String sname in axImport.GeneratedSources) {
					TraceUtil.WriteLineInfo(this, "TypeLib - AX gen source: " 
											+ sname);
				}
				RecordTranslatedAssy(Assembly.LoadFrom(_axAssyInfo._url));
				TraceUtil.WriteLineIf(null, TraceLevel.Info,
									 "TypeLib - conversion done: "
									 + _assyInfo._url);
			} finally {
				progress.Finished();
			}
		}
예제 #6
0
	public 	AxImporter(AxImporter.Options options)
			{
				throw new NotImplementedException ();
			}
예제 #7
0
 public ImporterCallback(AxImporter importer)
 {
     this.importer = importer;
     this.options  = importer.options;
 }
 public ImporterCallback(AxImporter importer)
 {
     this.importer = importer;
     this.options = importer.options;
 }