?
Solved

CodeDomProvider to Target .Net 1.1

Posted on 2010-01-06
3
Medium Priority
?
1,304 Views
Last Modified: 2013-11-08
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
Comment
Question by:sujeshva
  • 2
3 Comments
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 26201428
0
 
LVL 3

Author Comment

by:sujeshva
ID: 26209420
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
 
LVL 3

Accepted Solution

by:
sujeshva earned 0 total points
ID: 26213414
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

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question