Example #1
0
        public void TestSaveStringResource()
        {
            Uri    uri            = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string uriPath        = Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath));
            string sourceFilename = Path.Combine(uriPath, @"Binaries\gutils.dll");
            string targetFilename = Path.Combine(Path.GetTempPath(), "testSaveStringResource.dll");

            File.Copy(sourceFilename, targetFilename, true);
            // a new resource
            StringResource sr = new StringResource();

            sr.Name  = new ResourceId(StringResource.GetBlockId(1256));
            sr[1256] = Guid.NewGuid().ToString();
            sr.SaveTo(targetFilename);
            using (ResourceInfo ri = new ResourceInfo())
            {
                ri.Load(targetFilename);
                Assert.AreEqual(2, ri[Kernel32.ResourceTypes.RT_STRING].Count);
                Assert.AreEqual(StringResource.GetBlockId(1256), ri[Kernel32.ResourceTypes.RT_STRING][1].Name.Id.ToInt64());
                Assert.AreEqual(sr[1256], ((StringResource)ri[Kernel32.ResourceTypes.RT_STRING][1])[1256]);
                foreach (StringResource rc in ri[Kernel32.ResourceTypes.RT_STRING])
                {
                    Console.WriteLine("StringResource: {0}, {1}", rc.Name, rc.TypeName);
                    DumpResource.Dump(rc);
                }
            }
        }
        public void TestAddDialogResource()
        {
            Uri    uri           = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string uriPath       = Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath));
            string gutilsdll     = Path.Combine(uriPath, @"Binaries\gutils.dll");
            int    dialogsBefore = 0;

            using (ResourceInfo ri = new ResourceInfo())
            {
                ri.Load(gutilsdll);
                dialogsBefore = ri.Resources[new ResourceId(Kernel32.ResourceTypes.RT_DIALOG)].Count;
            }
            string targetFilename = Path.Combine(Path.GetTempPath(), "testAddDialogResource.dll");

            File.Copy(gutilsdll, targetFilename, true);
            // copy an existing dialog inside gutils.dll
            DialogResource sourceDialog = new DialogResource();

            sourceDialog.Name = new ResourceId("GABRTDLG");
            sourceDialog.LoadFrom(gutilsdll);
            sourceDialog.Name = new ResourceId("NEWGABRTDLG");
            Console.WriteLine(targetFilename);
            sourceDialog.SaveTo(targetFilename);
            // check that the dialog was written
            sourceDialog.LoadFrom(targetFilename);
            DumpResource.Dump(sourceDialog);
            using (ResourceInfo ri = new ResourceInfo())
            {
                ri.Load(targetFilename);
                int dialogsAfter = ri.Resources[new ResourceId(Kernel32.ResourceTypes.RT_DIALOG)].Count;
                DumpResource.Dump(ri);
                Assert.AreEqual(dialogsBefore + 1, dialogsAfter);
            }
        }
Example #3
0
        public void TestDeepCopyBytes()
        {
            string filename = Path.Combine(Environment.SystemDirectory, "atl.dll");

            Assert.IsTrue(File.Exists(filename));
            VersionResource existingVersionResource = new VersionResource();

            existingVersionResource.Language = ResourceUtil.USENGLISHLANGID;
            Console.WriteLine("Loading {0}", filename);
            existingVersionResource.LoadFrom(filename);
            DumpResource.Dump(existingVersionResource);

            VersionResource versionResource = new VersionResource();

            versionResource.FileVersion    = existingVersionResource.FileVersion;
            versionResource.ProductVersion = existingVersionResource.ProductVersion;

            StringFileInfo existingVersionResourceStringFileInfo = (StringFileInfo)existingVersionResource["StringFileInfo"];
            VarFileInfo    existingVersionResourceVarFileInfo    = (VarFileInfo)existingVersionResource["VarFileInfo"];

            // copy string resources, data only
            StringFileInfo stringFileInfo = new StringFileInfo();

            versionResource["StringFileInfo"] = stringFileInfo;
            {
                Dictionary <string, StringTable> .Enumerator enumerator = existingVersionResourceStringFileInfo.Strings.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    StringTable stringTable = new StringTable(enumerator.Current.Key);
                    stringFileInfo.Strings.Add(enumerator.Current.Key, stringTable);
                    Dictionary <string, StringTableEntry> .Enumerator resourceEnumerator = enumerator.Current.Value.Strings.GetEnumerator();
                    while (resourceEnumerator.MoveNext())
                    {
                        StringTableEntry stringResource = new StringTableEntry(resourceEnumerator.Current.Key);
                        stringResource.Value = resourceEnumerator.Current.Value.Value;
                        stringTable.Strings.Add(resourceEnumerator.Current.Key, stringResource);
                    }
                }
            }

            // copy var resources, data only
            VarFileInfo varFileInfo = new VarFileInfo();

            versionResource["VarFileInfo"] = varFileInfo;
            {
                Dictionary <string, VarTable> .Enumerator enumerator = existingVersionResourceVarFileInfo.Vars.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    VarTable varTable = new VarTable(enumerator.Current.Key);
                    varFileInfo.Vars.Add(enumerator.Current.Key, varTable);
                    Dictionary <UInt16, UInt16> .Enumerator translationEnumerator = enumerator.Current.Value.Languages.GetEnumerator();
                    while (translationEnumerator.MoveNext())
                    {
                        varTable.Languages.Add(translationEnumerator.Current.Key, translationEnumerator.Current.Value);
                    }
                }
            }

            ByteUtils.CompareBytes(existingVersionResource.WriteAndGetBytes(), versionResource.WriteAndGetBytes());
        }
Example #4
0
        public void TestLoad()
        {
            Uri    uri     = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string uriPath = Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath));

            string[] files =
            {
                // Path.Combine(Environment.SystemDirectory, "regedt32.exe"),
                // Path.Combine(Environment.GetEnvironmentVariable("WINDIR"), "explorer.exe"),
                Path.Combine(uriPath, "Binaries\\gutils.dll"),
                Path.Combine(uriPath, "Binaries\\6to4svc.dll"),
                Path.Combine(uriPath, "Binaries\\custom.exe"),
            };

            foreach (string filename in files)
            {
                Console.WriteLine(filename);
                Assert.IsTrue(File.Exists(filename));
                using (ResourceInfo vi = new ResourceInfo())
                {
                    vi.Load(filename);
                    DumpResource.Dump(vi);
                }
            }
        }
Example #5
0
        public void TestDeleteVersionResource()
        {
            string filename = Path.Combine(Environment.SystemDirectory, "atl.dll");

            Assert.IsTrue(File.Exists(filename));
            string targetFilename = Path.Combine(Path.GetTempPath(), "testDeleteVersionResource.dll");

            File.Copy(filename, targetFilename, true);
            Console.WriteLine(targetFilename);
            VersionResource versionResource = new VersionResource();

            versionResource.Language = ResourceUtil.USENGLISHLANGID;
            versionResource.LoadFrom(targetFilename);
            Console.WriteLine("Name: {0}", versionResource.Name);
            Console.WriteLine("Type: {0}", versionResource.Type);
            Console.WriteLine("Language: {0}", versionResource.Language);
            versionResource.DeleteFrom(targetFilename);
            try
            {
                versionResource.LoadFrom(targetFilename);
                Assert.Fail("Expected that the deleted resource cannot be found");
            }
            catch (Win32Exception ex)
            {
                // expected exception
                Console.WriteLine("Expected exception: {0}", ex.Message);
            }
            using (ResourceInfo ri = new ResourceInfo())
            {
                ri.Load(targetFilename);
                DumpResource.Dump(ri);
            }
        }
        public void TestLoadAndSaveCursorResource()
        {
            Uri    uri      = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string filename = HttpUtility.UrlDecode(uri.AbsolutePath);

            Assert.IsTrue(File.Exists(filename));
            string targetFilename = Path.Combine(Path.GetTempPath(), "testLoadAndSaveCursorResource.exe");

            File.Copy(filename, targetFilename, true);
            Console.WriteLine(targetFilename);

            string gutilsfilename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\gutils.dll");

            Assert.IsTrue(File.Exists(gutilsfilename));
            string[] cursorNames = { "HORZLINE", "VERTLINE" };
            foreach (string cursorName in cursorNames)
            {
                CursorDirectoryResource cursorDirectoryResource = new CursorDirectoryResource();
                cursorDirectoryResource.Name     = new ResourceId(cursorName);
                cursorDirectoryResource.Language = ResourceUtil.USENGLISHLANGID;
                cursorDirectoryResource.LoadFrom(gutilsfilename);
                cursorDirectoryResource.SaveTo(targetFilename);
            }

            Console.WriteLine("Written CursorDirectoryResource:");
            foreach (string cursorName in cursorNames)
            {
                CursorDirectoryResource newCursorDirectoryResource = new CursorDirectoryResource();
                newCursorDirectoryResource.Name     = new ResourceId(cursorName);
                newCursorDirectoryResource.Language = ResourceUtil.USENGLISHLANGID;
                newCursorDirectoryResource.LoadFrom(targetFilename);
                DumpResource.Dump(newCursorDirectoryResource);
            }
        }
Example #7
0
        public void TestLoadAndSaveIconResource()
        {
            Uri    uri           = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string icon1filename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Icons\\Icon1.ico");

            Assert.IsTrue(File.Exists(icon1filename));
            IconFile iconFile = new IconFile(icon1filename);

            Console.WriteLine("{0}: {1}", Path.GetFileName(icon1filename), iconFile.Type);
            foreach (IconFileIcon icon in iconFile.Icons)
            {
                Console.WriteLine(" {0}", icon.ToString());
            }

            Console.WriteLine("Converted IconDirectoryResource:");
            IconDirectoryResource iconDirectoryResource = new IconDirectoryResource(iconFile);

            DumpResource.Dump(iconDirectoryResource);
            Assert.AreEqual(iconFile.Icons.Count, iconDirectoryResource.Icons.Count);

            string filename = HttpUtility.UrlDecode(uri.AbsolutePath);

            Assert.IsTrue(File.Exists(filename));
            string targetFilename = Path.Combine(Path.GetTempPath(), "testLoadAndSaveIconResource.exe");

            File.Copy(filename, targetFilename, true);
            Console.WriteLine(targetFilename);
            iconDirectoryResource.SaveTo(targetFilename);

            Console.WriteLine("Written IconDirectoryResource:");
            IconDirectoryResource newIconDirectoryResource = new IconDirectoryResource();

            newIconDirectoryResource.LoadFrom(targetFilename);
            DumpResource.Dump(newIconDirectoryResource);
        }
        public void TestLoadAndSaveVersionResource(string binaryName)
        {
            Uri    uri      = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string filename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\" + binaryName);

            Assert.IsTrue(File.Exists(filename));

            VersionResource versionResource = new VersionResource();

            versionResource.Language = ResourceUtil.USENGLISHLANGID;
            versionResource.LoadFrom(filename);
            DumpResource.Dump(versionResource);

            versionResource.FileVersion    = "1.2.3.4";
            versionResource.ProductVersion = "5.6.7.8";
            versionResource.FileFlags      = 0x2 | 0x8; // private and prerelease

            StringFileInfo stringFileInfo = (StringFileInfo)versionResource["StringFileInfo"];

            stringFileInfo["Comments"] = string.Format("{0}\0", Guid.NewGuid());
            stringFileInfo["NewValue"] = string.Format("{0}\0", Guid.NewGuid());

            VarFileInfo varFileInfo = (VarFileInfo)versionResource["VarFileInfo"];

            varFileInfo[ResourceUtil.USENGLISHLANGID] = 1300;

            string targetFilename = Path.Combine(Path.GetTempPath(), "test.dll");

            File.Copy(filename, targetFilename, true);
            Console.WriteLine(targetFilename);
            versionResource.SaveTo(targetFilename);

            VersionResource newVersionResource = new VersionResource();

            newVersionResource.Language = ResourceUtil.USENGLISHLANGID;
            newVersionResource.LoadFrom(targetFilename);
            DumpResource.Dump(versionResource);

            AssertOneVersionResource(targetFilename);
            Assert.AreEqual(newVersionResource.FileVersion, versionResource.FileVersion);
            Assert.AreEqual(newVersionResource.ProductVersion, versionResource.ProductVersion);
            Assert.AreEqual(newVersionResource.FileFlags, versionResource.FileFlags);

            StringFileInfo testedStringFileInfo = (StringFileInfo)newVersionResource["StringFileInfo"];

            foreach (KeyValuePair <string, StringTableEntry> stringResource in testedStringFileInfo.Default.Strings)
            {
                Console.WriteLine("{0} = {1}", stringResource.Value.Key, stringResource.Value.StringValue);
                Assert.AreEqual(stringResource.Value.Value, stringFileInfo[stringResource.Key]);
            }

            VarFileInfo newVarFileInfo = (VarFileInfo)newVersionResource["VarFileInfo"];

            foreach (KeyValuePair <UInt16, UInt16> varResource in newVarFileInfo.Default.Languages)
            {
                Console.WriteLine("{0} = {1}", varResource.Key, varResource.Value);
                Assert.AreEqual(varResource.Value, varFileInfo[varResource.Key]);
            }
        }
        public void TestDeleteAndSaveVersionResource(string binaryName, int language)
        {
            Uri    uri      = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string filename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\" + binaryName);

            Assert.IsTrue(File.Exists(filename));
            string targetFilename = Path.Combine(Path.GetTempPath(), "testDeleteAndSaveVersionResource.dll");

            File.Copy(filename, targetFilename, true);
            Console.WriteLine(targetFilename);
            VersionResource existingVersionResource = new VersionResource();

            existingVersionResource.Language = (ushort)language;
            existingVersionResource.DeleteFrom(targetFilename);

            VersionResource versionResource = new VersionResource();

            versionResource.FileVersion    = "1.2.3.4";
            versionResource.ProductVersion = "4.5.6.7";

            StringFileInfo stringFileInfo = new StringFileInfo();

            versionResource[stringFileInfo.Key] = stringFileInfo;
            StringTable stringFileInfoStrings = new StringTable(); // "040904b0"

            stringFileInfoStrings.LanguageID = (ushort)language;
            stringFileInfoStrings.CodePage   = 1200;
            Assert.AreEqual(language, stringFileInfoStrings.LanguageID);
            Assert.AreEqual(1200, stringFileInfoStrings.CodePage);
            stringFileInfo.Strings.Add(stringFileInfoStrings.Key, stringFileInfoStrings);
            stringFileInfoStrings["ProductName"]     = "ResourceLib";
            stringFileInfoStrings["FileDescription"] = "File updated by ResourceLib\0";
            stringFileInfoStrings["CompanyName"]     = "Vestris Inc.";
            stringFileInfoStrings["LegalCopyright"]  = "All Rights Reserved\0";
            stringFileInfoStrings["EmptyValue"]      = "";
            stringFileInfoStrings["Comments"]        = string.Format("{0}\0", Guid.NewGuid());
            stringFileInfoStrings["ProductVersion"]  = string.Format("{0}\0", versionResource.ProductVersion);

            VarFileInfo varFileInfo = new VarFileInfo();

            versionResource[varFileInfo.Key] = varFileInfo;
            VarTable varFileInfoTranslation = new VarTable("Translation");

            varFileInfo.Vars.Add(varFileInfoTranslation.Key, varFileInfoTranslation);
            varFileInfoTranslation[ResourceUtil.USENGLISHLANGID] = 1300;

            versionResource.SaveTo(targetFilename);
            Console.WriteLine("Reloading {0}", targetFilename);

            VersionResource newVersionResource = new VersionResource();

            newVersionResource.LoadFrom(targetFilename);
            DumpResource.Dump(newVersionResource);

            AssertOneVersionResource(targetFilename);
            Assert.AreEqual(newVersionResource.FileVersion, versionResource.FileVersion);
            Assert.AreEqual(newVersionResource.ProductVersion, versionResource.ProductVersion);
        }
Example #10
0
        public void TestLoadIconResources()
        {
            string filename = Path.Combine(Environment.SystemDirectory, "regedt32.exe");

            Assert.IsTrue(File.Exists(filename));
            IconDirectoryResource iconDirectoryResource = new IconDirectoryResource();

            iconDirectoryResource.LoadFrom(filename);
            DumpResource.Dump(iconDirectoryResource);
        }
Example #11
0
 public void TestLoad(string filename)
 {
     Console.WriteLine(filename);
     Assert.IsTrue(File.Exists(filename));
     using (ResourceInfo vi = new ResourceInfo())
     {
         vi.Load(filename);
         DumpResource.Dump(vi);
     }
 }
        public void TestLoadManifestResources()
        {
            ManifestResource manifestResource = new ManifestResource();
            Uri    uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string dll = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\write.exe");

            manifestResource.LoadFrom(dll, Kernel32.ManifestType.CreateProcess);
            DumpResource.Dump(manifestResource);
            Assert.AreEqual(Kernel32.ManifestType.CreateProcess, manifestResource.ManifestType);
        }
Example #13
0
        public void TestDeleteAndSaveVersionResource()
        {
            string filename = Path.Combine(Environment.SystemDirectory, "atl.dll");

            Assert.IsTrue(File.Exists(filename));
            string targetFilename = Path.Combine(Path.GetTempPath(), "testDeleteAndSaveVersionResource.dll");

            File.Copy(filename, targetFilename, true);
            Console.WriteLine(targetFilename);
            VersionResource existingVersionResource = new VersionResource();

            existingVersionResource.DeleteFrom(targetFilename);

            VersionResource versionResource = new VersionResource();

            versionResource.FileVersion    = "1.2.3.4";
            versionResource.ProductVersion = "4.5.6.7";

            StringFileInfo stringFileInfo = new StringFileInfo();

            versionResource[stringFileInfo.Key] = stringFileInfo;
            StringTable stringFileInfoStrings = new StringTable(); // "040904b0"

            stringFileInfoStrings.LanguageID = 1033;
            stringFileInfoStrings.CodePage   = 1200;
            Assert.AreEqual(1033, stringFileInfoStrings.LanguageID);
            Assert.AreEqual(1200, stringFileInfoStrings.CodePage);
            stringFileInfo.Strings.Add(stringFileInfoStrings.Key, stringFileInfoStrings);
            stringFileInfoStrings["ProductName"]     = "ResourceLib";
            stringFileInfoStrings["FileDescription"] = "File updated by ResourceLib\0";
            stringFileInfoStrings["CompanyName"]     = "Vestris Inc.";
            stringFileInfoStrings["LegalCopyright"]  = "All Rights Reserved\0";
            stringFileInfoStrings["EmptyValue"]      = "";
            stringFileInfoStrings["Comments"]        = string.Format("{0}\0", Guid.NewGuid());
            stringFileInfoStrings["ProductVersion"]  = string.Format("{0}\0", versionResource.ProductVersion);

            VarFileInfo varFileInfo = new VarFileInfo();

            versionResource[varFileInfo.Key] = varFileInfo;
            VarTable varFileInfoTranslation = new VarTable("Translation");

            varFileInfo.Vars.Add(varFileInfoTranslation.Key, varFileInfoTranslation);
            varFileInfoTranslation[ResourceUtil.USENGLISHLANGID] = 1300;

            versionResource.SaveTo(targetFilename);
            Console.WriteLine("Reloading {0}", targetFilename);

            VersionResource newVersionResource = new VersionResource();

            newVersionResource.LoadFrom(targetFilename);
            DumpResource.Dump(newVersionResource);

            Assert.AreEqual(newVersionResource.FileVersion, versionResource.FileVersion);
            Assert.AreEqual(newVersionResource.ProductVersion, versionResource.ProductVersion);
        }
        public void TestAddCursorResource()
        {
            Uri    uri             = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string cursor1filename = Path.Combine(Path.GetDirectoryName(
                                                      HttpUtility.UrlDecode(uri.AbsolutePath)), "Cursors\\Cursor1.cur");

            Assert.IsTrue(File.Exists(cursor1filename));
            IconFile cursorFile = new IconFile(cursor1filename);

            Console.WriteLine("{0}: {1}", Path.GetFileName(cursor1filename), cursorFile.Type);
            foreach (IconFileIcon cursor in cursorFile.Icons)
            {
                Console.WriteLine(" {0}", cursor.ToString());
            }

            Console.WriteLine("Converted CursorDirectoryResource:");
            CursorDirectoryResource cursorDirectoryResource = new CursorDirectoryResource(cursorFile);

            Assert.AreEqual(16, cursorDirectoryResource.Icons[0].HotspotX);
            Assert.AreEqual(16, cursorDirectoryResource.Icons[0].HotspotY);
            cursorDirectoryResource.Name              = new ResourceId("RESOURCELIB");
            cursorDirectoryResource.Language          = ResourceUtil.USENGLISHLANGID;
            cursorDirectoryResource.Icons[0].HotspotX = 12;
            cursorDirectoryResource.Icons[0].HotspotY = 12;
            cursorDirectoryResource.Icons[0].Id       = 3;
            cursorDirectoryResource.Icons[0].Language = ResourceUtil.USENGLISHLANGID;
            DumpResource.Dump(cursorDirectoryResource);
            Assert.AreEqual(cursorFile.Icons.Count, cursorDirectoryResource.Icons.Count);

            string filename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\gutils.dll");

            Assert.IsTrue(File.Exists(filename));
            string targetFilename = Path.Combine(Path.GetTempPath(), "testReplaceCursorResource.dll");

            File.Copy(filename, targetFilename, true);
            Console.WriteLine(targetFilename);
            cursorDirectoryResource.SaveTo(targetFilename);

            Console.WriteLine("Written CursorDirectoryResource:");
            CursorDirectoryResource newCursorDirectoryResource = new CursorDirectoryResource();

            newCursorDirectoryResource.Name     = new ResourceId("RESOURCELIB");
            newCursorDirectoryResource.Language = ResourceUtil.USENGLISHLANGID;
            newCursorDirectoryResource.LoadFrom(targetFilename);
            Assert.AreEqual(1, newCursorDirectoryResource.Icons.Count);
            Assert.AreEqual(cursorFile.Icons[0].Image.Size, newCursorDirectoryResource.Icons[0].Image.Size);
            DumpResource.Dump(newCursorDirectoryResource);

            using (ResourceInfo ri = new ResourceInfo())
            {
                ri.Load(targetFilename);
                DumpResource.Dump(ri);
            }
        }
Example #15
0
        public void TestLoadVersionResourceStrings()
        {
            string filename = Path.Combine(Environment.SystemDirectory, "atl.dll");

            Assert.IsTrue(File.Exists(filename));
            VersionResource versionResource = new VersionResource();

            versionResource.Language = ResourceUtil.USENGLISHLANGID;
            versionResource.LoadFrom(filename);
            DumpResource.Dump(versionResource);
        }
        public void TestLoadVersionResourceStrings(string binaryName)
        {
            Uri    uri      = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string filename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\" + binaryName);

            Assert.IsTrue(File.Exists(filename));
            VersionResource versionResource = new VersionResource();

            versionResource.Language = ResourceUtil.USENGLISHLANGID;
            versionResource.LoadFrom(filename);
            DumpResource.Dump(versionResource);
            AssertOneVersionResource(filename);
        }
Example #17
0
 public void TestLoadFontDirectoryResources()
 {
     Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
     string uriPath = Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath));
     string filename = Path.Combine(uriPath, @"Binaries\custom.exe");
     using (ResourceInfo ri = new ResourceInfo())
     {
         ri.Load(filename);
         Assert.AreEqual(1, ri[Kernel32.ResourceTypes.RT_FONTDIR].Count);
         foreach (FontDirectoryResource rc in ri[Kernel32.ResourceTypes.RT_FONTDIR])
         {
             Console.WriteLine("FontDirectoryResource: {0}, {1}", rc.Name, rc.TypeName);
             DumpResource.Dump(rc);
         }
     }
 }
Example #18
0
        public void TestReplaceBitmapResource()
        {
            Uri    uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string bitmapsdirectory = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Bitmaps");

            foreach (string bitmapfilename in Directory.GetFiles(bitmapsdirectory))
            {
                if (!bitmapfilename.EndsWith(".bmp"))
                {
                    continue;
                }

                Image imageFile = Bitmap.FromFile(bitmapfilename);
                Console.WriteLine("{0}: {1}x{2}", Path.GetFileName(bitmapfilename), imageFile.Width, imageFile.Height);

                string filename = Path.Combine(Environment.SystemDirectory, "msftedit.dll");
                Assert.IsTrue(File.Exists(filename));
                string targetFilename = Path.Combine(Path.GetTempPath(), "testLoadAndSaveBitmapResource.exe");
                File.Copy(filename, targetFilename, true);
                Console.WriteLine(targetFilename);

                BitmapResource bitmapResource = new BitmapResource();
                using (ResourceInfo targetFilenameResources = new ResourceInfo())
                {
                    targetFilenameResources.Load(targetFilename);
                    Resource existingBitmapResource = targetFilenameResources[Kernel32.ResourceTypes.RT_BITMAP][0];
                    bitmapResource.Name     = existingBitmapResource.Name;
                    bitmapResource.Language = existingBitmapResource.Language;
                    Console.WriteLine("Replacing {0}", bitmapResource.Name);
                }

                BitmapFile bitmapFile = new BitmapFile(bitmapfilename);
                bitmapResource.Bitmap = bitmapFile.Bitmap;
                Console.WriteLine("DIB: {0}x{1}", bitmapResource.Bitmap.Image.Width, bitmapResource.Bitmap.Image.Height);
                bitmapResource.SaveTo(targetFilename);

                Console.WriteLine("Written BitmapResource:");
                BitmapResource newBitmapResource = new BitmapResource();
                newBitmapResource.Name = bitmapResource.Name;
                newBitmapResource.LoadFrom(targetFilename);
                DumpResource.Dump(newBitmapResource);

                Image newBitmapResourceImage = newBitmapResource.Bitmap.Image;
                Assert.AreEqual(imageFile.Width, newBitmapResourceImage.Width);
                Assert.AreEqual(imageFile.Height, newBitmapResourceImage.Height);
            }
        }
        public void TestLoadDeleteGreekResource()
        {
            // the 6to4svcgreek.dll has a Greek version info strings resource
            VersionResource vr = new VersionResource();

            vr.Language = 1032;
            string testDll = Path.Combine(Path.GetTempPath(), "testLoadDeleteGreekResource.dll");

            Console.WriteLine(testDll);
            Uri    uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string dll = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\6to4svcgreek.dll");

            File.Copy(dll, testDll, true);
            vr.LoadFrom(testDll);
            DumpResource.Dump(vr);
            Assert.AreEqual(1032, vr.Language);
            vr.DeleteFrom(testDll);
        }
        public void TestLoadAndSaveCreateProcessManifestResource()
        {
            Uri    uri            = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string filename       = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\write.exe");
            string targetFilename = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".exe");

            File.Copy(filename, targetFilename, true);
            Console.WriteLine(targetFilename);
            ManifestResource manifestResource = new ManifestResource(Kernel32.ManifestType.CreateProcess);

            manifestResource.SaveTo(targetFilename);
            Console.WriteLine("Written manifest:");
            ManifestResource newManifestResource = new ManifestResource();

            newManifestResource.LoadFrom(targetFilename);
            DumpResource.Dump(newManifestResource);
            File.Delete(targetFilename);
        }
Example #21
0
        public void TestLoadAcceleratorResources()
        {
            Uri    uri      = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string uriPath  = Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath));
            string filename = Path.Combine(uriPath, @"Binaries\custom.exe");

            using (ResourceInfo ri = new ResourceInfo())
            {
                ri.Load(filename);
                Assert.AreEqual(2, ri[Kernel32.ResourceTypes.RT_ACCELERATOR].Count);
                foreach (AcceleratorResource rc in ri[Kernel32.ResourceTypes.RT_ACCELERATOR])
                {
                    Console.WriteLine("AcceleratorResource: {0}, {1}", rc.Name, rc.TypeName);
                    DumpResource.Dump(rc);
                }
                Assert.AreEqual(109, ri[Kernel32.ResourceTypes.RT_ACCELERATOR][0].Name.Id.ToInt64());
                Assert.AreEqual(110, ri[Kernel32.ResourceTypes.RT_ACCELERATOR][1].Name.Id.ToInt64());
            }
        }
        public void TestLoadCursorResources()
        {
            // gutils.dll has two cursors
            Uri    uri      = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string filename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\gutils.dll");

            Assert.IsTrue(File.Exists(filename));
            string[] cursorNames = { "HORZLINE", "VERTLINE" };
            foreach (string cursorName in cursorNames)
            {
                CursorDirectoryResource cursorDirectoryResource = new CursorDirectoryResource();
                cursorDirectoryResource.Name = new ResourceId(cursorName);
                cursorDirectoryResource.LoadFrom(filename);
                Assert.AreEqual(cursorDirectoryResource.Icons.Count, 1);
                Assert.AreEqual(16, cursorDirectoryResource.Icons[0].HotspotX);
                Assert.AreEqual(16, cursorDirectoryResource.Icons[0].HotspotY);
                DumpResource.Dump(cursorDirectoryResource);
            }
        }
Example #23
0
        public void TestLoadAndSaveBitmapResource()
        {
            Uri    uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string bitmapsdirectory = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Bitmaps");

            foreach (string bitmapfilename in Directory.GetFiles(bitmapsdirectory))
            {
                if (!bitmapfilename.EndsWith(".bmp"))
                {
                    continue;
                }

                Assert.IsTrue(File.Exists(bitmapfilename));
                Image imageFile = Bitmap.FromFile(bitmapfilename);
                Console.WriteLine("{0}: {1}x{2}", Path.GetFileName(bitmapfilename), imageFile.Width, imageFile.Height);

                string filename = HttpUtility.UrlDecode(uri.AbsolutePath);
                Assert.IsTrue(File.Exists(filename));
                string targetFilename = Path.Combine(Path.GetTempPath(), "testLoadAndSaveBitmapResource.exe");
                File.Copy(filename, targetFilename, true);
                Console.WriteLine(targetFilename);

                BitmapResource bitmapResource = new BitmapResource();
                bitmapResource.Name = new ResourceId("RESOURCELIB");
                BitmapFile bitmapFile = new BitmapFile(bitmapfilename);
                bitmapResource.Bitmap = bitmapFile.Bitmap;
                Console.WriteLine("DIB: {0}x{1}", bitmapResource.Bitmap.Image.Width, bitmapResource.Bitmap.Image.Height);
                bitmapResource.SaveTo(targetFilename);

                Console.WriteLine("Written BitmapResource:");
                BitmapResource newBitmapResource = new BitmapResource();
                newBitmapResource.Name = new ResourceId("RESOURCELIB");
                newBitmapResource.LoadFrom(targetFilename);
                DumpResource.Dump(newBitmapResource);

                Image newBitmapResourceImage = newBitmapResource.Bitmap.Image;
                Assert.AreEqual(imageFile.Width, newBitmapResourceImage.Width);
                Assert.AreEqual(imageFile.Height, newBitmapResourceImage.Height);
            }
        }
Example #24
0
        public void TestBatchUpdate()
        {
            Uri    uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string originalFilename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\gutils.dll");
            string filename         = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\gutils_changed.dll");

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
            File.Copy(originalFilename, filename);
            Assert.IsTrue(File.Exists(filename));
            Console.WriteLine("Filename: {0}", filename);

            // read existing string table
            var sr = new StringResource();

            sr.Name     = new ResourceId(StringResource.GetBlockId(402));
            sr.Language = 1033;
            sr.LoadFrom(filename);
            Console.WriteLine("StringResource before patching: {0}, {1}, {2}", sr.Name, sr.TypeName, sr.Language);
            DumpResource.Dump(sr);
            Assert.IsNotNull(sr);
            Assert.AreEqual("Out Of Memory", sr[402]);

            // change string and save it
            sr[402] = "OOM";
            Resource.Save(filename, new[] { sr });

            // re-read string table and verify the changed string table entry
            sr          = new StringResource();
            sr.Name     = new ResourceId(StringResource.GetBlockId(402));
            sr.Language = 1033;
            sr.LoadFrom(filename);
            Console.WriteLine("StringResource after patching: {0}, {1}, {2}", sr.Name, sr.TypeName, sr.Language);
            DumpResource.Dump(sr);
            Assert.AreEqual("OOM", sr[402]);
        }
        public void TestDeleteVersionResource(string binaryName)
        {
            Uri    uri      = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string filename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\" + binaryName);

            Assert.IsTrue(File.Exists(filename));
            string targetFilename = Path.Combine(Path.GetTempPath(), "testDeleteVersionResource.dll");

            File.Copy(filename, targetFilename, true);
            Console.WriteLine(targetFilename);
            VersionResource versionResource = new VersionResource();

            versionResource.Language = ResourceUtil.USENGLISHLANGID;
            versionResource.LoadFrom(targetFilename);
            Console.WriteLine("Name: {0}", versionResource.Name);
            Console.WriteLine("Type: {0}", versionResource.Type);
            Console.WriteLine("Language: {0}", versionResource.Language);
            versionResource.DeleteFrom(targetFilename);
            try
            {
                versionResource.LoadFrom(targetFilename);
                Assert.Fail("Expected that the deleted resource cannot be found");
            }
            catch (Win32Exception ex)
            {
                // expected exception
                Console.WriteLine("Expected exception: {0}", ex.Message);
            }

            AssertNoVersionResource(targetFilename);

            using (ResourceInfo ri = new ResourceInfo())
            {
                ri.Load(targetFilename);
                DumpResource.Dump(ri);
            }
        }
Example #26
0
        public void NoExceptionsOnNonAnsiPaths(string pefile, string widepath)
        {
            string widedir = Path.Combine(Path.GetTempPath(), widepath);

            try
            {
                if (Directory.Exists(widedir))
                {
                    Directory.Delete(widedir, true);
                }
                Directory.CreateDirectory(widedir);

                // Some binary file
                string pefileSrc = Path.Combine(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase, UriKind.Absolute).LocalPath), "Binaries\\" + pefile);
                Assert.That(File.Exists(pefileSrc), Is.True);
                string pefileDest = Path.Combine(widedir, pefile);
                File.Copy(pefileSrc, pefileDest);

                // Check it works — the operation is not important, LoadLibraryEx failed any of them
                var versionResource = new VersionResource();
                versionResource.Language = ResourceUtil.USENGLISHLANGID;
                versionResource.LoadFrom(pefileDest); // “System.ComponentModel.Win32Exception : The system cannot find the file specified” if fails
                DumpResource.Dump(versionResource);
            }
            finally
            {
                try
                {
                    Directory.Delete(widedir, true);
                }
                catch
                {
                    // Ignore, that's post-test cleanup
                }
            }
        }
        public void TestDeleteDeepCopyAndSaveVersionResource(string binaryName)
        {
            Uri    uri      = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string filename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\" + binaryName);

            Assert.IsTrue(File.Exists(filename));
            string targetFilename = Path.Combine(Path.GetTempPath(), binaryName);

            File.Copy(filename, targetFilename, true);
            Console.WriteLine(targetFilename);
            VersionResource existingVersionResource = new VersionResource();

            existingVersionResource.Language = ResourceUtil.USENGLISHLANGID;
            existingVersionResource.LoadFrom(targetFilename);
            DumpResource.Dump(existingVersionResource);
            existingVersionResource.DeleteFrom(targetFilename);

            VersionResource versionResource = new VersionResource();

            versionResource.FileVersion    = existingVersionResource.FileVersion;
            versionResource.ProductVersion = existingVersionResource.ProductVersion;

            StringFileInfo existingVersionResourceStringFileInfo = (StringFileInfo)existingVersionResource["StringFileInfo"];
            VarFileInfo    existingVersionResourceVarFileInfo    = (VarFileInfo)existingVersionResource["VarFileInfo"];

            // copy string resources, data only
            StringFileInfo stringFileInfo = new StringFileInfo();

            versionResource["StringFileInfo"] = stringFileInfo;
            {
                Dictionary <string, StringTable> .Enumerator enumerator = existingVersionResourceStringFileInfo.Strings.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    StringTable stringTable = new StringTable(enumerator.Current.Key);
                    stringFileInfo.Strings.Add(enumerator.Current.Key, stringTable);
                    Dictionary <string, StringTableEntry> .Enumerator resourceEnumerator = enumerator.Current.Value.Strings.GetEnumerator();
                    while (resourceEnumerator.MoveNext())
                    {
                        StringTableEntry stringResource = new StringTableEntry(resourceEnumerator.Current.Key);
                        stringResource.Value = resourceEnumerator.Current.Value.Value;
                        stringTable.Strings.Add(resourceEnumerator.Current.Key, stringResource);
                    }
                }
            }

            // copy var resources, data only
            VarFileInfo varFileInfo = new VarFileInfo();

            versionResource["VarFileInfo"] = varFileInfo;
            {
                Dictionary <string, VarTable> .Enumerator enumerator = existingVersionResourceVarFileInfo.Vars.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    VarTable varTable = new VarTable(enumerator.Current.Key);
                    varFileInfo.Vars.Add(enumerator.Current.Key, varTable);
                    Dictionary <UInt16, UInt16> .Enumerator translationEnumerator = enumerator.Current.Value.Languages.GetEnumerator();
                    while (translationEnumerator.MoveNext())
                    {
                        varTable.Languages.Add(translationEnumerator.Current.Key, translationEnumerator.Current.Value);
                    }
                }
            }

            versionResource.SaveTo(targetFilename);
            Console.WriteLine("Reloading {0}", targetFilename);

            VersionResource newVersionResource = new VersionResource();

            newVersionResource.LoadFrom(targetFilename);
            DumpResource.Dump(newVersionResource);

            AssertOneVersionResource(targetFilename);
            Assert.AreEqual(newVersionResource.FileVersion, versionResource.FileVersion);
            Assert.AreEqual(newVersionResource.ProductVersion, versionResource.ProductVersion);

            StringFileInfo testedStringFileInfo = (StringFileInfo)newVersionResource["StringFileInfo"];

            foreach (KeyValuePair <string, StringTableEntry> stringResource in testedStringFileInfo.Default.Strings)
            {
                Assert.AreEqual(stringResource.Value.Value, stringFileInfo[stringResource.Key]);
            }

            VarFileInfo newVarFileInfo = (VarFileInfo)newVersionResource["VarFileInfo"];

            foreach (KeyValuePair <UInt16, UInt16> varResource in newVarFileInfo.Default.Languages)
            {
                Assert.AreEqual(varResource.Value, varFileInfo[varResource.Key]);
            }
        }
        public void TestDeepCopyBytes(string binaryName)
        {
            Uri    uri      = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            string filename = Path.Combine(Path.GetDirectoryName(HttpUtility.UrlDecode(uri.AbsolutePath)), "Binaries\\" + binaryName);

            Assert.IsTrue(File.Exists(filename));
            VersionResource existingVersionResource = new VersionResource();

            existingVersionResource.Language = ResourceUtil.USENGLISHLANGID;
            Console.WriteLine("Loading {0}", filename);
            existingVersionResource.LoadFrom(filename);
            DumpResource.Dump(existingVersionResource);

            VersionResource versionResource = new VersionResource();

            versionResource.FileVersion    = existingVersionResource.FileVersion;
            versionResource.ProductVersion = existingVersionResource.ProductVersion;

            StringFileInfo existingVersionResourceStringFileInfo = (StringFileInfo)existingVersionResource["StringFileInfo"];
            VarFileInfo    existingVersionResourceVarFileInfo    = (VarFileInfo)existingVersionResource["VarFileInfo"];

            // copy string resources, data only
            StringFileInfo stringFileInfo = new StringFileInfo();

            versionResource["StringFileInfo"] = stringFileInfo;
            {
                Dictionary <string, StringTable> .Enumerator enumerator = existingVersionResourceStringFileInfo.Strings.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    StringTable stringTable = new StringTable(enumerator.Current.Key);
                    stringFileInfo.Strings.Add(enumerator.Current.Key, stringTable);
                    Dictionary <string, StringTableEntry> .Enumerator resourceEnumerator = enumerator.Current.Value.Strings.GetEnumerator();
                    while (resourceEnumerator.MoveNext())
                    {
                        StringTableEntry stringResource = new StringTableEntry(resourceEnumerator.Current.Key);
                        stringResource.Value = resourceEnumerator.Current.Value.Value;
                        stringTable.Strings.Add(resourceEnumerator.Current.Key, stringResource);
                    }
                }
            }

            // copy var resources, data only
            VarFileInfo varFileInfo = new VarFileInfo();

            versionResource["VarFileInfo"] = varFileInfo;
            {
                Dictionary <string, VarTable> .Enumerator enumerator = existingVersionResourceVarFileInfo.Vars.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    VarTable varTable = new VarTable(enumerator.Current.Key);
                    varFileInfo.Vars.Add(enumerator.Current.Key, varTable);
                    Dictionary <UInt16, UInt16> .Enumerator translationEnumerator = enumerator.Current.Value.Languages.GetEnumerator();
                    while (translationEnumerator.MoveNext())
                    {
                        varTable.Languages.Add(translationEnumerator.Current.Key, translationEnumerator.Current.Value);
                    }
                }
            }

            ByteUtils.CompareBytes(existingVersionResource.WriteAndGetBytes(), versionResource.WriteAndGetBytes());
        }