반응형

* VBNET 소스코드 동적 컴파일 예제...

 

 

Main

 

전체 소스 코드

Form1.vb

 

Imports System.Runtime.InteropServices
Imports System.CodeDom.Compiler

Public Class Form1
    Public Declare Function ShellExecuteA Lib "shell32.dll" ( _
    ByVal hWnd As IntPtr, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Integer) As IntPtr

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        TextBox1.Text = "Imports System " + vbCrLf + _
                        "   Module Module1 " + vbCrLf + _
                        "       Sub Main() " + vbCrLf + _
                        "           System.Console.WriteLine(""Hello World!"")" + vbCrLf + _
                        "           System.Console.WriteLine(""Press the Enter Key to Continue."")" + vbCrLf + _
                        "           System.Console.ReadLine() " + vbCrLf + _
                        "       End Sub " + vbCrLf + _
                        "   End Module"

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim cdp As CodeDomProvider = CodeDomProvider.CreateProvider("vb")
        Dim cp As CompilerParameters = New CompilerParameters()

        'Flase : Dll output True : EXE File out put
        cp.GenerateExecutable = True
        'Path Set
        cp.OutputAssembly = "C:\vbnettest.exe"
        Dim cr As CompilerResults = cdp.CompileAssemblyFromSource(cp, TextBox1.Text)

        If cr.Errors.Count > 0 Then
            MessageBox.Show("Compile Error")
            Return
        End If

        ShellExecuteA(Me.Handle, "open", "C:\vbnettest.exe", "", "", 4)

    End Sub

End Class

 

 

*예제 결과

 

반응형

+ Recent posts