Hack Forums
[.NET] -[ HELP CodeDom Ressources Add A Exe and extract ]- - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] -[ HELP CodeDom Ressources Add A Exe and extract ]- (/showthread.php?tid=3490532)

Pages: Pages 1 2


-[ HELP CodeDom Ressources Add A Exe and extract ]- - MemoryCarter - May 19, 2013

Hi, I want know if is possible to use a ressource added in my codedom compiled programme.

Exemple of my CodeDom compiler:
Code
Public Shared Sub COMPILE(ByVal OUTPUT As String, ByVal CODE As String, ByVal ICON As String, RESSOURCE As String)
        On Error Resume Next

        Dim Compiler As ICodeCompiler = (New VBCodeProvider).CreateCompiler()
        Dim Parameters As New CompilerParameters()
        Dim cResults As CompilerResults

        Parameters.GenerateExecutable = True
        Parameters.OutputAssembly = OUTPUT
        Parameters.ReferencedAssemblies.Add("System.dll")
        Parameters.ReferencedAssemblies.Add("System.Data.dll")
        Parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
        If Not RESSOURCE = Nothing Then
            Parameters.EmbeddedResources.Add(RESSOURCE) ' Add a ressource is ok
        End If

[Image: TY7vhgT.png]

In My CodeDom Code:
Code
My.Computer.FileSystem.WriteAllBytes("C:\user...", My.Resources.File, False) ' Not Working in CodeDom :( help please :)
My But is; inject a ressource in my codedom app ( it's ok ) and Extract this ressource with a codedom app.


* - 0xBB - May 19, 2013

Take a look here : http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager.aspx

🤝


* - MemoryCarter - May 19, 2013

Humm no other solutions ? More simple !??


* - 0xBB - May 19, 2013

(May 19, 2013 09:08 AM)MemoryCarter Wrote:  Humm no other solutions ? More simple !??

It's really simple , You get the current assembly loaded and read the .resources file from it. You get the object (normally string) 's "title" then get the data related to it.

There is something I wrote really quick , maybe it will give you ideas :

Quote: 'write resources
Dim r As New System.Resources.ResourceWriter("path/resourcefilename.resources")
r.AddResource("my title", "my data as string/byte or whatever")
r.Generate()
r.Close()

'Read it back
Dim mydata As String = New System.Resources.ResourceManager("resourcefilename", System.Reflection.Assembly.GetExecutingAssembly).GetObject("my title")




* - Felix.Frogg - May 19, 2013

(May 19, 2013 09:08 AM)MemoryCarter Wrote:  Humm no other solutions ? More simple !??

Code
Imports System.CodeDom.Compiler
Imports System.IO
Imports System.Resources

    Public Shared Sub COMPILE(ByVal OUTPUT As String, ByVal CODE As String, ByVal ICON As String, ByVal RESNAME As String, ByVal RESBYTES As Byte())
  Dim ResourceWriter As New ResourceWriter("res.resources")
  ResourceWriter.AddResource(RESNAME, RESBYTES)
  ResourceWriter.Close()

  Dim Compiler As ICodeCompiler = (New VBCodeProvider).CreateCompiler()
  Dim Params As New CompilerParameters()
  Dim Results As CompilerResults
  Dim Errors As String = Nothing

  Params.GenerateExecutable = True
  Params.ReferencedAssemblies.Add("System.dll")
  Params.ReferencedAssemblies.Add("System.Drawing.dll")
  Params.ReferencedAssemblies.Add("System.Data.dll")
  Params.ReferencedAssemblies.Add("System.Windows.Forms.dll")
  Params.CompilerOptions = " /platform:x86"
  Params.OutputAssembly = OUTPUT
  If IO.File.Exists(ICON) Then Params.CompilerOptions &= " /win32icon:" & Chr(34) & ICON & Chr(34)

  Dim Version = New Dictionary(Of String, String)
  Version.Add("CompilerVersion", "v2.0")
  Params.CompilerOptions &= " /optimize"
  Params.CompilerOptions &= " /t:winexe"
  Params.EmbeddedResources.Add("res.resources")
  Results = Compiler.CompileAssemblyFromSource(Params, CODE)

  If Results.Errors.Count > 0 Then
    For Each CompilerError In Results.Errors
    Errors &= "Error: " & CompilerError.ErrorText & vbNewLine
    Next
    MessageBox.Show(Errors, "Error")
  Else
    MessageBox.Show("All done")
  End If
    End Sub



* - InVista - May 19, 2013

(May 19, 2013 09:08 AM)MemoryCarter Wrote:  Humm no other solutions ? More simple !??

Dim BBYYTTEE As Byte() = IO.File.ReadAllBytes(My.Resources.YourFile) '< here the bytes read into memory!
System.IO.File.WriteAllBytes(Directory.GetCurrentDirectory & "yournewfilename.exe", BBYYTTEE) '<and here the bytes are written to the specified path name!

Oh, before I forget:

Set your My.Resources.YourFile to : add as Embedded Resources from the properties window
and set to Copy Alway's


* - MemoryCarter - May 19, 2013

Code
Dim BBYYTTEE As Byte() = IO.File.ReadAllBytes(My.Resources.name)
System.IO.File.WriteAllBytes("C:\Users\Admin\Desktop\" & "yournewfilename.exe", BBYYTTEE)
Not compatible in CodeDom.

[Image: asUxbYo.png]


(May 19, 2013 09:22 AM)Felix.Frogg Wrote:  
Code
Imports System.CodeDom.Compiler
Imports System.IO
Imports System.Resources

    Public Shared Sub COMPILE(ByVal OUTPUT As String, ByVal CODE As String, ByVal ICON As String, ByVal RESNAME As String, ByVal RESBYTES As Byte())
  Dim ResourceWriter As New ResourceWriter("res.resources")
  ResourceWriter.AddResource(RESNAME, RESBYTES)
  ResourceWriter.Close()

  Dim Compiler As ICodeCompiler = (New VBCodeProvider).CreateCompiler()
  Dim Params As New CompilerParameters()
  Dim Results As CompilerResults
  Dim Errors As String = Nothing

  Params.GenerateExecutable = True
  Params.ReferencedAssemblies.Add("System.dll")
  Params.ReferencedAssemblies.Add("System.Drawing.dll")
  Params.ReferencedAssemblies.Add("System.Data.dll")
  Params.ReferencedAssemblies.Add("System.Windows.Forms.dll")
  Params.CompilerOptions = " /platform:x86"
  Params.OutputAssembly = OUTPUT
  If IO.File.Exists(ICON) Then Params.CompilerOptions &= " /win32icon:" & Chr(34) & ICON & Chr(34)

  Dim Version = New Dictionary(Of String, String)
  Version.Add("CompilerVersion", "v2.0")
  Params.CompilerOptions &= " /optimize"
  Params.CompilerOptions &= " /t:winexe"
  Params.EmbeddedResources.Add("res.resources")
  Results = Compiler.CompileAssemblyFromSource(Params, CODE)

  If Results.Errors.Count > 0 Then
    For Each CompilerError In Results.Errors
    Errors &= "Error: " & CompilerError.ErrorText & vbNewLine
    Next
    MessageBox.Show(Errors, "Error")
  Else
    MessageBox.Show("All done")
  End If
    End Sub

Good, but how to use the .resources in my codedom code ? extract and what is the ".resources" ?? extension


* - InVista - May 19, 2013

What does your file do?

You select a file and then press compile and the res.resource should be added or bind (from the same folder) with the file you selected?

Rephrase:

What is your program supposed to do?


* - Felix.Frogg - May 19, 2013

(May 19, 2013 09:45 AM)MemoryCarter Wrote:  Good, but how to use the .resources in my codedom code ? extract and what is the ".resources" ?? extension

Code
Dim ResourceManager As New ResourceManager(RESNAME, GetExecutingAssembly())
Dim EmbeddedBytes As Byte() = ResourceManager.GetObject("your_reesource_name")

and than you can do anything with "EmbeddedBytes" bytes array - drop, execute, modify etc.


* - MemoryCarter - May 19, 2013

(May 19, 2013 09:51 AM)InVista Wrote:  What does your file do?

You select a file and then press compile and the res.resource should be added or bind (from the same folder) with the file you selected?

Rephrase:

What is your program supposed to do?

[Image: nVdQ1Uc.png]

I use a .ressources extension but my ressource is a .exe WTF ?


* - Felix.Frogg - May 19, 2013

(May 19, 2013 09:54 AM)MemoryCarter Wrote:  I use a .ressources extension but my ressource is a .exe WTF ?

What are you trying to do?
The resource file to add (in my example) as embedded is res.resources. You can't append new embedded resource in codedom by code in 6th post

If you want add multi-resource (as embedded), you should use code like that:

Code
Dim ResourceWriter As New ResourceWriter("res.resources")

ResourceWriter.AddResource("file1", IO.File.ReadAllBytes("c:\file1.exe"))
ResourceWriter.AddResource("file2", IO.File.ReadAllBytes("c:\file2.exe"))
ResourceWriter.AddResource("file3", IO.File.ReadAllBytes("c:\file3.exe"))

ResourceWriter.Close()



* - InVista - May 19, 2013

You select a file, which should be added as the new "res.resources" file to another file?

I don't know the rest of your code and therefore I don't know what your program is supposed to do.

Here is what I know:

I see a button and a textbox and another button.


* - MemoryCarter - May 19, 2013

My but is:
Step1: Add a .exe in my CodeDom programme and compile. ( is ok )
Step2: On CodeDom programm run, then extract the .exe of the CodeDom programme

But i Learn the codedom, is difficult :/ wtf!!

( sorry for my bad langage ..)


* - InVista - May 19, 2013

I think you double compile : you have the SUB COMPILE and you , in your post, called your My.Resources.Code, which is called in the SUB COMPILE.

You build an .exe with a file inside which you added via the codeom.

When the file is clicked the file runs and extracts, also, the in step1 added ,as resources added, file?

Edit:
Textbox1 = the file to add?


* - MemoryCarter - May 19, 2013

(May 19, 2013 10:10 AM)InVista Wrote:  I think you double compile : you have the SUB COMPILE and you , in your post, called your My.Resources.Code, which is called in the SUB COMPILE.

You build an .exe with a file inside which you added via the codeom.

When the file is clicked the file runs and extracts, also, the in step1 added ,as resources added, file?

Edit:
Textbox1 = the file to add?

I do not understand, have you teamviewer ?


(May 19, 2013 10:10 AM)InVista Wrote:  I think you double compile : you have the SUB COMPILE and you , in your post, called your My.Resources.Code, which is called in the SUB COMPILE.

You build an .exe with a file inside which you added via the codeom.

When the file is clicked the file runs and extracts, also, the in step1 added ,as resources added, file?

Edit:
Textbox1 = the file to add?

Yes textbox1 is a file to add in a ressources !!