• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1325
  • Last Modified:

CodeDomProvider to Target .Net 1.1

I have a project that is being written in VB.Net 3.5. The idea of this project (part of it) is to generate a compiled assembly at runtime. The user of the generated assembly is a service which is written in .Net 1.1 framework and at this time is not scheduled for a conversion.

I have the following code as shown below. This works fine and does generate the assembly, but in .Net 3.5 target framework. How can I target 1.1??

I have tried

Dim provOpt = New Dictionary(Of String, String)
            provOpt.Add("CompilerVersion", "v1.1.4322")

This does not work and throws out an error as "Compiler executable file vbc.exe cannot be found".

I have tried v1.1, just 1.1 and other possible combinations!

I absolutely need a solution to this and the target framework has to be 1.1 and the code needs to be written in 3.5 as the entire project is going to make use of it.

Please help.

Thanks,
Suj
Dim Compiler As VBCodeProvider = Nothing
    Dim pvdr As CodeDomProvider
    Dim CompilerParams As CompilerParameters
    Dim CompileResults As CompilerResults

    Public Function CompileCode() As String
        Dim strReturn As String = ""
        Try
            CompilerParams = New CompilerParameters
            With CompilerParams
                .TreatWarningsAsErrors = False
                .WarningLevel = 4
                .GenerateInMemory = False
                .IncludeDebugInformation = True

                Dim References() As String = {"System.dll", "System.Data.dll", "Microsoft.VisualBasic.dll"}
                .ReferencedAssemblies.AddRange(References)
                .OutputAssembly = "C:\Projects\ThisIsATestDLL.dll"

            End With

            Dim provOpt = New Dictionary(Of String, String)
            provOpt.Add("CompilerVersion", "v3.5")


            Compiler = New VBCodeProvider(provOpt)


            CompileResults = Compiler.CompileAssemblyFromFile(CompilerParams, "C:\RandD\VS08\TestClassLib\Test.vb")

            If CompileResults.Errors.HasErrors Then
                For Each Err As CodeDom.Compiler.CompilerError In CompileResults.Errors
                    strReturn &= Err.ErrorText & " @Line: " & Err.Line & vbCrLf
                Next
            End If

        Catch ex As Exception
            Throw New Exception(ex.Message, ex)
        End Try
        Return strReturn
    End Function

Open in new window

0
sujeshva
Asked:
sujeshva
  • 2
1 Solution
 
Kumaraswamy RCommented:
0
 
sujeshvaAuthor Commented:
rkworlds,

Thats not the answer I am looking for. I am looking to use the CodeDomProvider to compile a dll targeting the 1.1 Framework from a 3.5 Assembly.
0
 
sujeshvaAuthor Commented:
Found the solution myself. A bit of a cheat, but there is no other way. I am posting the solution for the benefit of others who might have the same problem.

First of all, it is important to understand how the target is set.

Dim provOpt = New Dictionary(Of String, String)
provOpt.Add("CompilerVersion", "v3.5")

Compiler = New VBCodeProvider(provOpt)

What this code does is, tells the Compiler that the version of vbc.exe is not "2.0". It is however absolutely required to mention "v3.5". So the internal CodeDomCompiler code will look for the environment variables COMPLUS_InstallRoot and COMPLUS_Version. Since these environment variables are not present, it will default it to go look up for the 3.5 version directory.

So the trick is, to introduce (SetEnvironmentVariables) in your code to point to the .Net 1.1 framework directory. Since SetEnvironmentVariables will keep the environment variables alive only for the current session, any other code running on the same system will not be affected. Below is the complete code..

Imports Microsoft.VisualBasic
Imports System.CodeDom
Imports System.CodeDom.Compiler

Public Class CodeCompiler
    Dim Compiler As VBCodeProvider = Nothing
    Dim pvdr As CodeDomProvider
    Dim CompilerParams As CompilerParameters
    Dim CompileResults As CompilerResults

    Public Function CompileCode() As String
        Dim strReturn As String = ""
        Try
            CompilerParams = New CompilerParameters
            With CompilerParams
                .TreatWarningsAsErrors = False
                .WarningLevel = 4
                .GenerateInMemory = False
                .IncludeDebugInformation = True

                Dim References() As String = {"System.dll", "System.Data.dll", "Microsoft.VisualBasic.dll"}
                .ReferencedAssemblies.AddRange(References)
                .OutputAssembly = "C:\Projects\ThisIsATestDLL11.dll"

            End With

'You can put these values in the config file if you dont want to 'hard code it.
            Environment.SetEnvironmentVariable("COMPLUS_InstallRoot", "C:\WINDOWS\Microsoft.NET\Framework")
            Environment.SetEnvironmentVariable("COMPLUS_Version", "v1.1.4322")


            Dim provOpt = New Dictionary(Of String, String)
            provOpt.Add("CompilerVersion", "v3.5")


            Compiler = New VBCodeProvider(provOpt)
            CompileResults = Compiler.CompileAssemblyFromFile(CompilerParams, "C:\RandD\VS08\TestClassLib\Test.vb")

            If CompileResults.Errors.HasErrors Then
                For Each Err As CodeDom.Compiler.CompilerError In CompileResults.Errors
                    strReturn &= Err.ErrorText & " @Line: " & Err.Line & vbCrLf
                Next
            End If

        Catch ex As Exception
            Throw New Exception(ex.Message, ex)
        End Try
        Return strReturn
    End Function
End Class

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now