Exemple #1
0
 public static extern bool UpdateResource(ResourceUpdateHandle hUpdate, IntPtr type, IntPtr name, ushort wLanguage, byte[] lpData, int cbData);
Exemple #2
0
        public bool Execute()
        {
            if (NativeMethods.GetFileVersionInfoSize(Source.ItemSpec) > 0)
            {
                return(true); // Nothing to do
            }
            try
            {
                if (Directory.Exists(TempDir))
                {
                    Directory.Delete(TempDir, true);
                }

                Directory.CreateDirectory(TempDir);
                string srcFile = Source.ItemSpec;
                string tmpFile = Path.Combine(TempDir, Path.GetFileName(Source.ItemSpec));

                Assembly       myAssembly = typeof(UpdateNativeVersionResource).Assembly;
                AppDomainSetup setup      = new AppDomainSetup();
                setup.ApplicationName               = "GenerateVersionInfoViaAssembly";
                setup.ApplicationBase               = Path.GetDirectoryName(new Uri(myAssembly.CodeBase).LocalPath);
                setup.AppDomainInitializer          = new AppDomainInitializer(ResourceRefresh.OnRefreshVersionInfo);
                setup.AppDomainInitializerArguments = new string[] { Source.ItemSpec, TempDir, tmpFile };

                AppDomain dom = AppDomain.CreateDomain("AttributeRefresher", myAssembly.Evidence, setup);
                AppDomain.Unload(dom); // Remove locks

                byte[] versionInfo;
                int    size = NativeMethods.GetFileVersionInfoSize(tmpFile);
                if (size >= 0 && NativeMethods.GetFileVersionInfo(tmpFile, size, out versionInfo))
                {
                    File.Delete(tmpFile);

                    BuildEngine.LogMessageEvent(new BuildMessageEventArgs("Updating version resources", null, "UpdateNativeVersionResouce", MessageImportance.Normal));
                    using (ResourceUpdateHandle resHandle = NativeMethods.BeginUpdateResource(srcFile, false))
                    {
                        if (resHandle != null)
                        {
                            bool ok = NativeMethods.UpdateResource(resHandle, (IntPtr)16, (IntPtr)1, 0, versionInfo, size);
                            ok = resHandle.Commit() && ok;

                            if (ok)
                            {
                                if (!string.IsNullOrEmpty(KeyContainer) || (KeyFile != null && !string.IsNullOrEmpty(KeyFile.ItemSpec)))
                                {
                                    BuildEngine.LogMessageEvent(new BuildMessageEventArgs("Resigning assembly", null, "UpdateNativeVersionResouce", MessageImportance.Normal));

                                    if (!ResignAssemblyWithFileOrContainer(srcFile, (KeyFile != null) ? KeyFile.ItemSpec : null, KeyContainer))
                                    {
                                        BuildEngine.LogMessageEvent(new BuildMessageEventArgs("Resigning assembly failed", null, "UpdateNativeVersionResouce", MessageImportance.High));
                                        return(false);
                                    }
                                }

                                SourceUpdated = true;
                                return(true);
                            }
                        }
                    }
                }
                else
                {
                    File.Delete(tmpFile);
                }

                BuildEngine.LogMessageEvent(new BuildMessageEventArgs("Updating version resources failed", null, "UpdateNativeVersionResouce", MessageImportance.Normal));

                return(false);
            }
            finally
            {
                if (!SourceUpdated && File.Exists(Source.ItemSpec))
                {
                    File.Delete(Source.ItemSpec); // Ensure relinking in next build
                }
            }
        }