cancel
Showing results for 
Search instead for 
Did you mean: 

FileExtract - Can anyone spot the deliberate mistake please?

Former Member
0 Kudos

Basically, the program includes my add-in along with SAPbouiCOM.dll both embedded.

The code below now extracts SAPbouiCOM.dll perfectly, but my program is condensed to 1Kb!!!

I bet there's something obvious here, but I just can't spot it!

Private Sub ExtractFile(ByVal path As String)

Try

Dim AddonExeFile As IO.FileStream

Dim thisExe As System.Reflection.Assembly

thisExe = System.Reflection.Assembly.GetExecutingAssembly()

Dim sTargetPath As String

Dim sSourcePath As String

Dim sTargetPathdll As String

Dim sFile As System.IO.Stream

sTargetPath = path & "\" & sAddonName & ".exe"

sSourcePath = path & "\" & sAddonName & ".tmp"

sTargetPathdll = path & "\" & "Interop.SAPbouiCOM.dll"

For Each resourceName As String In thisExe.GetManifestResourceNames()

sFile = thisExe.GetManifestResourceStream(resourceName)

If LCase(resourceName) <> LCase("Addoninstaller.Interop.SAPbouiCOM.dll") Then

' Create a tmp file first, after file is extracted change to exe

If IO.File.Exists(sSourcePath) Then

IO.File.Delete(sSourcePath)

End If

AddonExeFile = IO.File.Create(sSourcePath)

Dim buffer() As Byte

ReDim buffer(sFile.Length)

sFile.Read(buffer, 0, sFile.Length)

AddonExeFile.Write(buffer, 0, sFile.Length)

AddonExeFile.Close()

If IO.File.Exists(sTargetPath) Then

IO.File.Delete(sTargetPath)

End If

' Change file extension to exe

IO.File.Move(sSourcePath, sTargetPath)

Else

' Create a tmp file first, after file is extracted change to exe

AddonExeFile = IO.File.Create(sTargetPathdll)

Dim buffer2() As Byte

ReDim buffer2(sFile.Length)

sFile.Read(buffer2, 0, sFile.Length)

AddonExeFile.Write(buffer2, 0, sFile.Length)

AddonExeFile.Close()

End If

Next

Catch ex As Exception

MsgBox(ex.Message)

End Try

End Sub

Accepted Solutions (1)

Accepted Solutions (1)

barend_morkel2
Active Contributor
0 Kudos

Hi Daniel,

I'm a bit lazy - so I'm not going to go through your code -> I'll give you a code snippet I use (it is basically the same as in the SAP example)

"in someSub"

' Extract exe to installation folder

ExtractFile(strDest, "YourAddonName", "exe")

' Extract dlls to installation folder

ExtractFile(strDest, "Interop.SAPbobsCOM", "dll")

"end someSub"

' This function extracts the given add-on into the path specified

Private Sub ExtractFile(ByVal path As String, ByVal FName As String, ByVal ext As String)

Dim AddonExTFile As IO.FileStream

Dim thisExT As System.Reflection.Assembly

Dim file As System.IO.Stream

Dim sTargetPath = path & "\" & FName & "." & ext

Dim sSourcePath = path & "\" & FName & ".tmp"

Try

Try

thisExT = System.Reflection.Assembly.GetExecutingAssembly()

file = thisExT.GetManifestResourceStream(sInstallName & "." & FName & "." & ext)

' Create a tmp file first, after file is extracted change to ext

If IO.File.Exists(sSourcePath) Then

IO.File.Delete(sSourcePath)

End If

AddonExTFile = IO.File.Create(sSourcePath)

Dim buffer() As Byte

ReDim buffer(file.Length)

file.Read(buffer, 0, file.Length)

AddonExTFile.Write(buffer, 0, file.Length)

AddonExTFile.Close()

If IO.File.Exists(sTargetPath) Then

IO.File.Delete(sTargetPath)

End If

' Change file extension to exe

IO.File.Move(sSourcePath, sTargetPath)

Catch ex As Exception

MsgBox(ex.Message)

End Try

Finally

AddonExTFile = Nothing

thisExT = Nothing

file = Nothing

sTargetPath = ""

sSourcePath = ""

GC.Collect()

End Try

End Sub

''''''''''''''''''''''''''''''

AND remenber to include your exe and dll as embedded resources in your Add-On installer

Answers (0)