■ 관리 DLL 여부 구하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


[TestLibrary 프로젝트]

 

ICLR4Support.cs

 

 

namespace TestLibrary

{

    /// <summary>

    /// CLR 4.0 지원 인터페이스

    /// </summary>

    public interface ICLR4Support

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

 

        #region 관리 여부 구하기 - IsManaged(filePath)

 

        /// <summary>

        /// 관리 여부 구하기

        /// </summary>

        /// <param name="filePath">파일 경로</param>

        /// <returns>관리 여부</returns>

        bool IsManaged(string filePath);

 

        #endregion

    }

}

 

 

DLLHelper.cs

 

 

using Microsoft.CSharp;

using System;

using System.CodeDom.Compiler;

using System.Collections.Generic;

using System.Reflection;

using System.Runtime.InteropServices;

 

namespace TestLibrary

{

    /// <summary>

    /// DLL 헬퍼

    /// </summary>

    public class DLLHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// CLR 4.0 지원 객체

        /// </summary>

        private static ICLR4Support _clr4Support;

 

        /// <summary>

        /// 동기 객체

        /// </summary>

        private static object _syncObject = new object();

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region CLR 4.0 지원 객체 - CLR4Support

 

        /// <summary>

        /// CLR 4.0 지원 객체

        /// </summary>

        private static ICLR4Support CLR4Support

        {

            get

            {

                if(_clr4Support == null)

                {

                    lock(_syncObject)

                    {

                        if(_clr4Support == null)

                        {

                            Dictionary<string, string> optionDictionary = new Dictionary<string, string>

                            {  

                                { "CompilerVersion", "v4.0" }

                            };

 

                            CSharpCodeProvider provider = new CSharpCodeProvider(optionDictionary);

 

                            CompilerParameters parameters = new CompilerParameters();

 

                            parameters.GenerateExecutable = false;

                            parameters.GenerateInMemory   = true;

 

                            parameters.ReferencedAssemblies.Add(typeof(DLLHelper).Assembly.Location);

 

                            string classText =

                                "using System.Reflection;"                                              + Environment.NewLine +

                                "using TestLibrary;"                                                    + Environment.NewLine +

                                "class CLR4Support : ICLR4Support"                                      + Environment.NewLine +

                                "{"                                                                     + Environment.NewLine +

                                "    public bool IsManaged(string filePath)"                            + Environment.NewLine +

                                "    {"                                                                 + Environment.NewLine +

                                "        try"                                                           + Environment.NewLine +

                                "        {"                                                             + Environment.NewLine +

                                "            return Assembly.ReflectionOnlyLoadFrom(filePath) != null;" + Environment.NewLine +

                                "        }"                                                             + Environment.NewLine +

                                "        catch"                                                         + Environment.NewLine +

                                "        {"                                                             + Environment.NewLine +

                                "            return false;"                                             + Environment.NewLine +

                                "        }"                                                             + Environment.NewLine +

                                "    }"                                                                 + Environment.NewLine +

                                "}";

 

                            CompilerResults results = null;

 

                            try

                            {

                                results = provider.CompileAssemblyFromSource(parameters, classText);

                            }

                            catch

                            {

                                return null;

                            }

 

                            if(results.Errors.Count > 0)

                            {

                                Console.WriteLine("Errors building into {0}", results.PathToAssembly);

 

                                foreach(CompilerError error in results.Errors)

                                {

                                    Console.WriteLine("    {0}", error.ToString());

                                    Console.WriteLine();

                                }

                            }

                            else

                            {

                                Type clr4SupportType = results.CompiledAssembly.GetType("CLR4Support");

 

                                ConstructorInfo constructorInfo = clr4SupportType.GetConstructor(Type.EmptyTypes);

 

                                _clr4Support = constructorInfo.Invoke(new object[] { }) as ICLR4Support;

                            }

                        }

                    }

                }

 

                return _clr4Support;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 관리 여부 구하기 - IsManaged(filePath)

 

        /// <summary>

        /// 관리 여부 구하기

        /// </summary>

        /// <param name="filePath">파일 경로</param>

        /// <returns>관리 여부</returns>

        public static bool IsManaged(string filePath)

        {

            try

            {

                return Assembly.ReflectionOnlyLoadFrom(filePath) != null;

            }

            catch(Exception exception)

            {

                int resultHandle = Marshal.GetHRForException(exception);

 

                if(resultHandle == unchecked((int)0x8013101b))

                {

                    if(CLR4Support != null)

                    {

                        return CLR4Support.IsManaged(filePath);

                    }

                }

 

                return false;

            }

        }

 

        #endregion

    }

}

 

 

[TestProject 프로젝트]

 

Program.cs

 

 

using System;

using System.IO;

 

using TestLibrary;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            Console.Title = "관리 DLL 여부 구하기";

 

            string workingDirectoryPath = Path.GetDirectoryName(typeof(Program).Assembly.Location);

 

            Console.WriteLine("닷넷 4.0 DLL : " + DLLHelper.IsManaged(Path.Combine(workingDirectoryPath, "net40.dll" )));

            Console.WriteLine("닷넷 2.0 DLL : " + DLLHelper.IsManaged(Path.Combine(workingDirectoryPath, "net20.dll" )));

            Console.WriteLine("네이티브 DLL : " + DLLHelper.IsManaged(Path.Combine(workingDirectoryPath, "native.dll")));

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG