コード例 #1
0
ファイル: ResourceTests.cs プロジェクト: belav/roslyn
        public void ResourcesInCoff()
        {
            //this is to test that resources coming from a COFF can be added to a binary.
            string source =
                @"
class C
{
}
";
            var c1 = CreateCompilation(
                source,
                assemblyName: "Win32WithCoff",
                options: TestOptions.ReleaseDll
                );
            var exe = Temp.CreateFile();

            using (FileStream output = exe.Open())
            {
                var memStream = new MemoryStream(TestResources.General.nativeCOFFResources);
                c1.Emit(output, win32Resources: memStream);
            }

            c1 = null;

            //Open as data
            IntPtr lib = IntPtr.Zero;
            string versionData;

            try
            {
                lib = LoadLibraryEx(exe.Path, IntPtr.Zero, 0x00000002);
                if (lib == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                //the manifest and version primitives are tested elsewhere. This is to test that the resources
                //we expect are present. Also need to check that the actual contents of at least one of the resources
                //is good. That tests our processing of the relocations.

                uint   size;
                IntPtr versionRsrc = Win32Res.GetResource(lib, "#1", "#16", out size);
                versionData = Win32Res.VersionResourceToXml(versionRsrc);

                uint   stringTableSize;
                IntPtr stringTable = Win32Res.GetResource(lib, "#1", "#6", out stringTableSize);
                Assert.NotEqual(default, stringTable);
コード例 #2
0
ファイル: ResourceTests.cs プロジェクト: khm1600/CJing
        public void ResourceWithAttrSettings()
        {
            string source  = @"
[assembly: System.Reflection.AssemblyVersion(""1.2.3.4"")]
[assembly: System.Reflection.AssemblyFileVersion(""5.6.7.8"")]
[assembly: System.Reflection.AssemblyTitle(""One Hundred Years of Solitude"")] 
[assembly: System.Reflection.AssemblyDescription(""A classic of magical realist literature"")]
[assembly: System.Reflection.AssemblyCompany(""MossBrain"")]
[assembly: System.Reflection.AssemblyProduct(""Sound Cannon"")]
[assembly: System.Reflection.AssemblyCopyright(""circle C"")]
[assembly: System.Reflection.AssemblyTrademark(""circle R"")]
[assembly: System.Reflection.AssemblyInformationalVersion(""1.2.3garbage"")]

public class Maine
{
    public static void Main()
    {
    }
}
";
            var    c1      = CreateCompilation(source, assemblyName: "Win32VerAttrs", options: TestOptions.ReleaseExe);
            var    exeFile = Temp.CreateFile();

            using (FileStream output = exeFile.Open())
            {
                c1.Emit(output, win32Resources: c1.CreateDefaultWin32Resources(true, false, null, null));
            }

            c1 = null;
            string versionData;

            //Open as data
            IntPtr lib = IntPtr.Zero;

            try
            {
                lib = LoadLibraryEx(exeFile.Path, IntPtr.Zero, 0x00000002);
                Assert.True(lib != IntPtr.Zero, String.Format("LoadLibrary failed with HResult: {0:X}", +Marshal.GetLastWin32Error()));

                //the manifest and version primitives are tested elsewhere. This is to test that the default
                //values are passed to the primitives that assemble the resources.

                uint   size;
                IntPtr versionRsrc = Win32Res.GetResource(lib, "#1", "#16", out size);
                versionData = Win32Res.VersionResourceToXml(versionRsrc);
            }
            finally
            {
                if (lib != IntPtr.Zero)
                {
                    FreeLibrary(lib);
                }
            }

            string expected =
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<VersionResource Size=""964"">
  <VS_FIXEDFILEINFO FileVersionMS=""00050006"" FileVersionLS=""00070008"" ProductVersionMS=""00010002"" ProductVersionLS=""00030000"" />
  <KeyValuePair Key=""Comments"" Value=""A classic of magical realist literature"" />
  <KeyValuePair Key=""CompanyName"" Value=""MossBrain"" />
  <KeyValuePair Key=""FileDescription"" Value=""One Hundred Years of Solitude"" />
  <KeyValuePair Key=""FileVersion"" Value=""5.6.7.8"" />
  <KeyValuePair Key=""InternalName"" Value=""Win32VerAttrs.exe"" />
  <KeyValuePair Key=""LegalCopyright"" Value=""circle C"" />
  <KeyValuePair Key=""LegalTrademarks"" Value=""circle R"" />
  <KeyValuePair Key=""OriginalFilename"" Value=""Win32VerAttrs.exe"" />
  <KeyValuePair Key=""ProductName"" Value=""Sound Cannon"" />
  <KeyValuePair Key=""ProductVersion"" Value=""1.2.3garbage"" />
  <KeyValuePair Key=""Assembly Version"" Value=""1.2.3.4"" />
</VersionResource>";

            Assert.Equal(expected, versionData);
        }
コード例 #3
0
ファイル: ResourceTests.cs プロジェクト: khm1600/CJing
        public void DefaultVersionResource()
        {
            string source = @"
public class Maine
{
    public static void Main()
    {
    }
}
";
            var    c1     = CreateCompilation(source, assemblyName: "Win32VerNoAttrs", options: TestOptions.ReleaseExe);
            var    exe    = Temp.CreateFile();

            using (FileStream output = exe.Open())
            {
                c1.Emit(output, win32Resources: c1.CreateDefaultWin32Resources(true, false, null, null));
            }

            c1 = null;

            //Open as data
            IntPtr lib = IntPtr.Zero;
            string versionData;
            string mftData;

            try
            {
                lib = LoadLibraryEx(exe.Path, IntPtr.Zero, 0x00000002);
                if (lib == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                //the manifest and version primitives are tested elsewhere. This is to test that the default
                //values are passed to the primitives that assemble the resources.

                uint   size;
                IntPtr versionRsrc = Win32Res.GetResource(lib, "#1", "#16", out size);
                versionData = Win32Res.VersionResourceToXml(versionRsrc);

                uint   mftSize;
                IntPtr mftRsrc = Win32Res.GetResource(lib, "#1", "#24", out mftSize);
                mftData = Win32Res.ManifestResourceToXml(mftRsrc, mftSize);
            }
            finally
            {
                if (lib != IntPtr.Zero)
                {
                    FreeLibrary(lib);
                }
            }

            string expected =
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<VersionResource Size=""612"">
  <VS_FIXEDFILEINFO FileVersionMS=""00000000"" FileVersionLS=""00000000"" ProductVersionMS=""00000000"" ProductVersionLS=""00000000"" />
  <KeyValuePair Key=""FileDescription"" Value="" "" />
  <KeyValuePair Key=""FileVersion"" Value=""0.0.0.0"" />
  <KeyValuePair Key=""InternalName"" Value=""Win32VerNoAttrs.exe"" />
  <KeyValuePair Key=""LegalCopyright"" Value="" "" />
  <KeyValuePair Key=""OriginalFilename"" Value=""Win32VerNoAttrs.exe"" />
  <KeyValuePair Key=""ProductVersion"" Value=""0.0.0.0"" />
  <KeyValuePair Key=""Assembly Version"" Value=""0.0.0.0"" />
</VersionResource>";

            Assert.Equal(expected, versionData);

            expected = @"<?xml version=""1.0"" encoding=""utf-16""?>
<ManifestResource Size=""490"">
  <Contents><![CDATA[<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>

<assembly xmlns=""urn:schemas-microsoft-com:asm.v1"" manifestVersion=""1.0"">
  <assemblyIdentity version=""1.0.0.0"" name=""MyApplication.app""/>
  <trustInfo xmlns=""urn:schemas-microsoft-com:asm.v2"">
    <security>
      <requestedPrivileges xmlns=""urn:schemas-microsoft-com:asm.v3"">
        <requestedExecutionLevel level=""asInvoker"" uiAccess=""false""/>
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>]]></Contents>
</ManifestResource>";

            Assert.Equal(expected, mftData);

            //look at the same data through the FileVersion API.
            //If the codepage and resource language information is not
            //written correctly into the internal resource directory of
            //the PE, then GetVersionInfo will fail to find the FileVersionInfo.
            //Once upon a time in Roslyn, the codepage and lang info was not written correctly.
            var fileVer = FileVersionInfo.GetVersionInfo(exe.Path);

            Assert.Equal(" ", fileVer.LegalCopyright);
        }
コード例 #4
0
ファイル: ResourceTests.cs プロジェクト: khm1600/CJing
        public void ResourcesInCoff()
        {
            //this is to test that resources coming from a COFF can be added to a binary.
            string source = @"
class C
{
}
";
            var    c1     = CreateCompilation(source, assemblyName: "Win32WithCoff", options: TestOptions.ReleaseDll);
            var    exe    = Temp.CreateFile();

            using (FileStream output = exe.Open())
            {
                var memStream = new MemoryStream(TestResources.General.nativeCOFFResources);
                c1.Emit(output, win32Resources: memStream);
            }

            c1 = null;

            //Open as data
            IntPtr lib = IntPtr.Zero;
            string versionData;

            try
            {
                lib = LoadLibraryEx(exe.Path, IntPtr.Zero, 0x00000002);
                if (lib == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }

                //the manifest and version primitives are tested elsewhere. This is to test that the resources
                //we expect are present. Also need to check that the actual contents of at least one of the resources
                //is good. That tests our processing of the relocations.

                uint   size;
                IntPtr versionRsrc = Win32Res.GetResource(lib, "#1", "#16", out size);
                versionData = Win32Res.VersionResourceToXml(versionRsrc);

                uint   stringTableSize;
                IntPtr stringTable = Win32Res.GetResource(lib, "#1", "#6", out stringTableSize);
                Assert.NotNull(stringTable);

                uint   elevenSize;
                IntPtr elevenRsrc = Win32Res.GetResource(lib, "#1", "#11", out elevenSize);
                Assert.NotNull(elevenRsrc);

                uint   wevtSize;
                IntPtr wevtRsrc = Win32Res.GetResource(lib, "#1", "WEVT_TEMPLATE", out wevtSize);
                Assert.NotNull(wevtRsrc);
            }
            finally
            {
                if (lib != IntPtr.Zero)
                {
                    FreeLibrary(lib);
                }
            }

            string expected =
                @"<?xml version=""1.0"" encoding=""utf-16""?>
<VersionResource Size=""1104"">
  <VS_FIXEDFILEINFO FileVersionMS=""000b0000"" FileVersionLS=""eacc0000"" ProductVersionMS=""000b0000"" ProductVersionLS=""eacc0000"" />
  <KeyValuePair Key=""CompanyName"" Value=""Microsoft Corporation"" />
  <KeyValuePair Key=""FileDescription"" Value=""Team Foundation Server Object Model"" />
  <KeyValuePair Key=""FileVersion"" Value=""11.0.60108.0 built by: TOOLSET_ROSLYN(GNAMBOO-DEV-GNAMBOO)"" />
  <KeyValuePair Key=""InternalName"" Value=""Microsoft.TeamFoundation.Framework.Server.dll"" />
  <KeyValuePair Key=""LegalCopyright"" Value=""© Microsoft Corporation. All rights reserved."" />
  <KeyValuePair Key=""OriginalFilename"" Value=""Microsoft.TeamFoundation.Framework.Server.dll"" />
  <KeyValuePair Key=""ProductName"" Value=""Microsoft® Visual Studio® 2012"" />
  <KeyValuePair Key=""ProductVersion"" Value=""11.0.60108.0"" />
</VersionResource>";

            Assert.Equal(expected, versionData);

            //look at the same data through the FileVersion API.
            //If the codepage and resource language information is not
            //written correctly into the internal resource directory of
            //the PE, then GetVersionInfo will fail to find the FileVersionInfo.
            //Once upon a time in Roslyn, the codepage and lang info was not written correctly.
            var fileVer = FileVersionInfo.GetVersionInfo(exe.Path);

            Assert.Equal("Microsoft Corporation", fileVer.CompanyName);
        }
コード例 #5
0
ファイル: Win32Res.cs プロジェクト: zhxjdwh/roslyn
        public void BasicResources()
        {
            System.IO.MemoryStream strm = new System.IO.MemoryStream();
            Microsoft.CodeAnalysis.Compilation.AppendNullResource(strm);

            //choose version values such that they would cause overflow when shifted as an int
            Win32ResourceConversions.AppendVersionToResourceStream(strm,
                                                                   true,
                                                                   "41220.41221.41222.41223",
                                                                   "originalFilenameMuddy.dll",
                                                                   "internalNameZep.dll",
                                                                   "41224.41225.41226.41227",      //4 ints
                                                                   new Version(41220, 41220, 41220, 41220),
                                                                   "this is the file description", //the old compiler put blank here if nothing was user-supplied
                                                                   "this is the legal copyright",  //the old compiler put blank here if nothing was user-supplied
                                                                   "this is the legal trademark",
                                                                   "product name the testproduct",
                                                                   "some comments",
                                                                   "testcompany");

            var xmlExpectedVersion = @"<?xml version=""1.0"" encoding=""utf-16""?>
<VersionResource Size=""1124"">
  <VS_FIXEDFILEINFO FileVersionMS=""a104a105"" FileVersionLS=""a106a107"" ProductVersionMS=""a108a109"" ProductVersionLS=""a10aa10b"" />
  <KeyValuePair Key=""Comments"" Value=""some comments"" />
  <KeyValuePair Key=""CompanyName"" Value=""testcompany"" />
  <KeyValuePair Key=""FileDescription"" Value=""this is the file description"" />
  <KeyValuePair Key=""FileVersion"" Value=""41220.41221.41222.41223"" />
  <KeyValuePair Key=""InternalName"" Value=""internalNameZep.dll"" />
  <KeyValuePair Key=""LegalCopyright"" Value=""this is the legal copyright"" />
  <KeyValuePair Key=""LegalTrademarks"" Value=""this is the legal trademark"" />
  <KeyValuePair Key=""OriginalFilename"" Value=""originalFilenameMuddy.dll"" />
  <KeyValuePair Key=""ProductName"" Value=""product name the testproduct"" />
  <KeyValuePair Key=""ProductVersion"" Value=""41224.41225.41226.41227"" />
  <KeyValuePair Key=""Assembly Version"" Value=""41220.41220.41220.41220"" />
</VersionResource>";

            System.IO.MemoryStream mft = new System.IO.MemoryStream();
            var manifestContents       = Resources.ResourceManager.GetObject("defaultWin32Manifest");

            Win32ResourceConversions.AppendManifestToResourceStream(strm, new System.IO.MemoryStream((byte[])manifestContents), false);

            var icon = Resources.ResourceManager.GetObject("Roslyn_ico");

            Win32ResourceConversions.AppendIconToResourceStream(strm, new System.IO.MemoryStream((byte[])icon));

            strm.Position = 0;

            var resources = CvtResFile.ReadResFile(strm);

            foreach (var r in resources)
            {
                if (r.pstringType.Ordinal == 16)    //version
                {
                    string rsrcInXml;

                    unsafe
                    {
                        fixed(byte *p = (r.data))
                        rsrcInXml = Win32Res.VersionResourceToXml((IntPtr)p);
                    }

                    Assert.Equal(xmlExpectedVersion, rsrcInXml);
                }
                else if (r.pstringType.Ordinal == 24)    //manifest
                {
                    Assert.Equal((byte[])manifestContents, r.data);
                }
                else if (r.pstringType.Ordinal == 14)    //icon group
                {
                    //first three words of the data contain 0, 1, iconCount.
                    short[] threeWords = new short[3];

                    unsafe
                    {
                        fixed(byte *p = (r.data))
                        {
                            Marshal.Copy((IntPtr)p, threeWords, 0, 3);
                        }
                    }

                    //find all of the individual icons in the resources.

                    for (short i = 0; i < threeWords[2]; i++)
                    {
                        bool found = false;
                        foreach (var rInner in resources)
                        {
                            if ((rInner.pstringName.Ordinal == i + 1) && //ICON IDs start at 1
                                (rInner.pstringType.Ordinal == 3))       //RT_ICON
                            {
                                found = true;
                                break;
                            }
                        }

                        Assert.True(found);
                    }
                }
            }
        }