How to embeded resource into run time generated assembly (exe)

J

jcvoon

Hi:

My application will compile a source code and generate an executable
assembly during run time, i'm able to generate the exe, but i don't
know the correct way to link a resource file into exe.

here is my code to generate the exe during run time:

Public Sub GenerateStub()
Dim sSourceCode As String = Path.Combine(Path.GetTempPath,
"LaunchStub.vb")
Dim sResFile As String = Path.Combine(Path.GetTempPath,
"LaunchStub.resources")
Dim sOutput As String =
Path.Combine(Path.GetDirectoryName(edtAppPath.Text),
Path.GetFileNameWithoutExtension(edtAppPath.Text) & "Stub.exe")
Dim cp As New Microsoft.VisualBasic.VBCodeProvider
Dim ic As System.CodeDom.Compiler.ICodeCompiler =
cp.CreateCompiler()
Dim cpar As New System.CodeDom.Compiler.CompilerParameters
Dim cr As System.CodeDom.Compiler.CompilerResults

If File.Exists(sOutput) Then
File.Delete(sOutput)
End If

GenerateResourceForStub(sResFile)
ExtractResourceStream("AppLaunchStub.LaunchStub.vb", sSourceCode)

cpar.GenerateInMemory = False
cpar.GenerateExecutable = True
cpar.OutputAssembly = sOutput
cpar.ReferencedAssemblies.Add("system.dll")
cpar.MainClass = "LaunchStub"
cpar.CompilerOptions = "/res:" & sResFile

cr = ic.CompileAssemblyFromFile(cpar, sSourceCode)

If cr.Errors.Count > 0 OrElse cr.CompiledAssembly Is Nothing Then
MessageBox.Show("Unable to generate stub !")
End If
End Sub

Private Sub GenerateResourceForStub(ByVal sResFile As String)
Dim aAsm() As String =
{"Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll", _

"Microsoft.Practices.EnterpriseLibrary.Configuration.dll", _

"Microsoft.Practices.EnterpriseLibrary.Common.dll", _

"Microsoft.ApplicationBlocks.Updater.Downloaders.dll", _

"Microsoft.ApplicationBlocks.Updater.dll", _

"Microsoft.ApplicationBlocks.Updater.ActivationProcessors.dll", _
"AppStart.exe", _
"AppStart.exe.config", _
"updaterconfiguration.config", _

"securityCryptographyConfiguration.config"}
Dim resWriter As New ResourceWriter(sResFile)
Dim sUpdaterAsmPath As String =
ConfigurationSettings.AppSettings("ApplicationUpdater").Replace("+",
"&")
Dim sAppStartPath As String =
Path.GetDirectoryName(edtAppPath.Text)

For Each sAsmName As String In aAsm
Dim sPath As String = IIf(sAsmName.StartsWith("Microsoft"),
sUpdaterAsmPath, sAppStartPath).ToString
Dim fs As New FileStream(Path.Combine(sPath, sAsmName),
FileMode.Open)
Dim buffer(CInt(fs.Length - 1)) As Byte

fs.Read(buffer, 0, CInt(fs.Length))
resWriter.AddResource(sAsmName, buffer)
Next
resWriter.Generate()
resWriter.Close()
End Sub


and in the LaunchStub.vb (copy from C:\Program Files\Microsoft Patterns
& Practices\Updater Application Block
2.0\QuickStarts\vb\NoTouchDeployment\Client\NoTouchDeploymentStub\)

ExtractResourceStream(appStartPath, "AppStart.exe", "AppStart.exe")

will raise NullReferenceException, i've try replace the resource name
above with [Assembly].GetExecutingAssembly.fullname & ".AppStart.exe",
and the result is same.


Basically what i'm trying to do is to create an program to generate a
bootstrap program for no-touch deployment, so that i don't need to use
VS2003 to rebuild the bootstrap program when the AppStart.exe.config
has changed.


Please help.

Thanks
JCVoon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top