人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

May 14, 2006

如何判断一个dll是否是.net assembly

Filed under: Code snippets

方法1.

引用自http://lozanotek.com/archive/2004/11/15/159.aspx

private const int COR_E_ASSEMBLYEXPECTED = -2147024885;
private bool IsAssembly(string asmFile)
{
    bool isAsmbly = true;
    try
    {
         AssemblyName.GetAssemblyName(asmFile);
    }
    catch(BadImageFormatException imageEx)
    {
         int hrResult = Marshal.GetHRForException(imageEx);
         isAsmbly = (hrResult != COR_E_ASSEMBLYEXPECTED);
    }

    return isAsmbly;
}
方法2.运用PE格式的知识,佩服.

出自http://geekswithblogs.net/rupreet/archive/2005/11/02/58873.aspx

  public static bool GetCLRHeaders(string fileFullName)
        {
            uint peHeader;
            uint peHeaderSignature;
            ushort machine;
            ushort sections;
            uint timestamp;
            uint pSymbolTable;
            uint noOfSymbol;
            ushort optionalHeaderSize;
            ushort characteristics;
            ushort dataDictionaryStart;
            uint[] dataDictionaryRVA = new uint[16];
            uint[] dataDictionarySize = new uint[16];

            Stream fs = new FileStream(fileFullName, FileMode.Open, FileAccess.Read);
            try
            {
                BinaryReader reader = new BinaryReader(fs);

                //PE Header starts @ 0x3C (60). Its a 4 byte header.
                fs.Position = 0x3C;
                peHeader = reader.ReadUInt32();

                //Moving to PE Header start location…
                fs.Position = peHeader;
                peHeaderSignature = reader.ReadUInt32();

                //We can also show all these value, but we will be      
                //limiting to the CLI header test.
                machine = reader.ReadUInt16();
                sections = reader.ReadUInt16();
                timestamp = reader.ReadUInt32();
                pSymbolTable = reader.ReadUInt32();
                noOfSymbol = reader.ReadUInt32();
                optionalHeaderSize = reader.ReadUInt16();
                characteristics = reader.ReadUInt16();
                /*
                    Now we are at the end of the PE Header and from here, the
                    PE Optional Headers starts…
                    To go directly to the datadictionary, we’ll increase the
                    stream’s current position to with 96 (0x60). 96 because,
                    28 for Standard fields
                    68 for NT-specific fields

                    From here DataDictionary starts…and its of total 128 bytes. DataDictionay has 16 directories in total,
                    doing simple maths 128/16 = 8.
                    So each directory is of 8 bytes.
                    In this 8 bytes, 4 bytes is of RVA and 4 bytes of Size.
                    btw, the 15th directory consist of CLR header! if its 0, its not a CLR file :)
                  */

                dataDictionaryStart = Convert.ToUInt16(Convert.ToUInt16(fs.Position) + 0x60);
                fs.Position = dataDictionaryStart;
                for (int i = 0; i < 15; i++)
                {
                    dataDictionaryRVA[i] = reader.ReadUInt32();
                    dataDictionarySize[i] = reader.ReadUInt32();
                }
                return dataDictionaryRVA[14] != 0;
            }
            finally
            {
                fs.Close();
            }
        }

软件开发的隐喻

Filed under: 技术以外

看<<代码大全>>闹程序革命

Filed under: 技术以外

VS2005的Code Snippets

Filed under: Visual Studio

1.如何使用

可以使用菜单: Edit->ItelliSencse->Insert Smnippet 或使用Context meun :Insert Snippet也可以使用快捷键:Ctrl+K,Ctrl+X

2.使用很简单,关键是可以定制.使用菜单Tools->Code Snippet Manager可以看到Code Snippet所在的路径.

C:\Program Files\Microsoft Visual Studio 8\VC#\Snippets\1033\Visual C#

所有的code snippets文件都是xml格式,并以.snippet结尾

文件中有两个核心的节点用来实现功能

<Header>节点用来描述code snippet的title,快捷键,描述,比如using.snippet

<Header>
   <Title>using</Title>
   <Shortcut>using</Shortcut>
   <Description>Code snippet for using statement</Description>
   <Author>Microsoft Corporation</Author>
   <SnippetTypes>
    <SnippetType>Expansion</SnippetType>
    <SnippetType>SurroundsWith</SnippetType>
   </SnippetTypes>
  </Header>

<Snippet>用来描述要生成的code,以using.snippet为例:

它会生成如下代码

using(resource)
{
 
}

用户可以把resource替换为自己的代码,xml中的描述如下

<Snippet>
   <Declarations>
    <Literal>
     <ID>resource</ID>
     <ToolTip>Resource to use</ToolTip>
     <Default>resource</Default>
    </Literal>
   </Declarations>
   <Code Language="csharp"><![CDATA[using($resource$)
 {
  $selected$ $end$
 }]]>

   </Code>
  </Snippet>

<Literal>部分定义了可替换的部分的id和default

[!CDATA]部分定义了要生成的代码模板,

参考资料

Code Snippets in Visual Studio 2005 讲解Code Snippets的使用和自定义,并提供了一个例子

Advanced Basics: IntelliSense Code Snippets — MSDN Magazine, April 2006

文章中的附件提供了几个例子,值得参考,同时还提供了几个Code Snippet编辑器的地址,用到时再看吧

 MSDN Code Snippet Schema Reference






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham