🪞.Net Framework/C# Winform

런타임 컴파일러 만들기 기초 (exe로 exe만들기)

Mawile 2021. 1. 5.
728x90

 

개발환경 >> Visual Studio

언어 >> C# WinForm

운영체제 >> Windows10

 


 

안녕하세요...!

요즘 포스팅이 많이 뜸했습니다.

이유는 개인공부를 좀 하고왔습니다.

 

저가 이번에 springLood에서 제작할 서버파일을 어떻게 할까하다가 vbs로 만들려했지만

vbs로 제작하면 단점이 코드가 그대로보이고, exe파일처럼 깔끔하지 않습니다.

 

그래서 좀 고민했죠..

어떻게 exe내에서 exe를 만들까..?

MSDN을 뒤지던도중 찾았습니다...ㄷㄷ 마소오빵형님들.....ㅠㅠ 써놓셨더군요.

 

CodeProviderICodeCompiler 클래스를 이용해서 런타임내에서 컴파일할 수 있습니다.

그럼 예제바로가시죠!!!

 

 


[[[   참고자료   ]]]

docs.microsoft.com/en-us/troubleshoot/dotnet/csharp/compile-code-using-compiler

 

Compile code by using C# compiler - C#

Describes how to compile code from a text source by using C# compiler.

docs.microsoft.com

docs.microsoft.com/en-us/dotnet/api/system.codedom.compiler.compilerparameters.referencedassemblies?view=dotnet-plat-ext-5.0

 

CompilerParameters.ReferencedAssemblies Property (System.CodeDom.Compiler)

Gets the assemblies referenced by the current project.

docs.microsoft.com


[[[   예제1   ]]]

using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Windows.Forms;

namespace CSharpCompiler
{
    public partial class Form1 : Form
    {
        public Form1() { InitializeComponent(); }

        private void button1_Click(object sender, EventArgs e)
        {
        	//결과물경로
            string Output = ".\\Output.exe";
            //icodecompiler에 CsharpCodeProvider를 할당한뒤 CreateCompiler함수호출
            ICodeCompiler icode = new CSharpCodeProvider().CreateCompiler();
            //컴파일러 파라미더 클래스 할당
            CompilerParameters parameters = new CompilerParameters();
            //true - exe , false - dll 반환
            parameters.GenerateExecutable = true;
            //결과물경로설정
            parameters.OutputAssembly = Output;
            
			//컴파일링 시작
            icode.CompileAssemblyFromSource(parameters, textBox1.Text);
        }

        private void Form1_Load(object sender, EventArgs e) { }
        private void textBox1_TextChanged(object sender, EventArgs e) { }

    }
}

 

이거는 오류내용을 제외한부분이구요

TextBox1에다가 내용을 입력하면 컴파일되서 exe파일로 추출됩니다!!

 

 

 

[[[   예제2   ]]]

using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Windows.Forms;

namespace CSharpCompiler
{
    public partial class Form1 : Form
    {
        public Form1() { InitializeComponent(); }

        private void button1_Click(object sender, EventArgs e)
        {
            string Output = ".\\Output.exe";
            ICodeCompiler icode = new CSharpCodeProvider().CreateCompiler();
            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = true;
            parameters.OutputAssembly = Output;
			//파라미더 어셈블리 참조추가
            parameters.ReferencedAssemblies.Add("System.dll");
            parameters.ReferencedAssemblies.Add("System.Net.dll");
            parameters.ReferencedAssemblies.Add("System.Net.Sockets.dll");
			//컴파일러결과를 받아올 객체
            CompilerResults ret = icode.CompileAssemblyFromSource(parameters, textBox1.Text);
            if(ret.Errors.Count > 0) //에러발생시
            {
                foreach(CompilerError err in ret.Errors)
                {
                    textBox2.Text =
                        textBox2.Text + " " + err.ErrorNumber + " "
                        + err.Line + " " + err.ErrorText + "\n\n"; //에러출력
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e) { }
        private void textBox1_TextChanged(object sender, EventArgs e) { }
        private void textBox2_TextChanged(object sender, EventArgs e) { }
    }
}

 


[[[   시연영상   ]]]

 

 

저가 테스트코드로 서버여는 exe파일을 만들었는데 성공적으로 방화벽이 열리면서 잘 컴파일 되었습니다!

 

 


 

그럼 저는 이만!!!

감사합니다!

728x90

'🪞.Net Framework > C# Winform' 카테고리의 다른 글

wav플레이어 만들기  (3) 2021.01.07
멀티 클라이언트 채팅서버 - C# winform  (2) 2020.12.06

댓글