How can I copy resource files from my Visual Studio VB NET application onto the local system when it's executed? - vb.net

I want to be able to copy resources I've compiled into the program to a location on the local machine where the application is run. Mostly a few DLLs that my program will not use, but others will (think Installer type of application).. But I can't find a way to reference the resources by path in order to do a File.Copy, for instance:
File.Copy(My.Resources.conversion, BinPath & "conversion.dll")
...tells me "Value of type 'Byte()' cannot be converted to 'String'". And I get why, obviously the first parameter needs to be a string path and not the resource itself. Is there a better way to copy resources or how can I obtain a reference path to the resource so I can use File.Copy?
For what it's worth, I've also tried
File.WriteAllBytes(BinPath & "conversion.dll", My.Resources.conversion)
...which "works" to copy it where I want it, but then I get errors when I try to call regsvr32 on the
new dll: "The module ... was loaded but the entry-point DLLRegisterServer was not found."
If there's a better way altogether, I'm open to any idea.

The following steps work for me:
Change the 'Build Action' of the dll in 'Resources' file to 'Embedded Resource'.
Use the following method to save resource file to disk.
Public Sub WriteResourceToFile(ByVal resourceName As String, ByVal fileName As String)
Using resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)
Using file = New FileStream(fileName, FileMode.Create, FileAccess.Write)
resource.CopyTo(file)
End Using
End Using
End Sub
Example of usage:
WriteResourceToFile("yournameSpace.conversion.dll", BinPath & "conversion.dll")

Related

How to get custom dll's working in a ssis script component?

I try to use my own .dll in a script compoment within ssis. The normal procedure gives me an error: "could not load file or assembly 'xxx' or one of its dependencies. The system cannot find the file specified."
What I tried yet is I went to project -> Open in Explorer and put my .dll into the bin folder but that same error occures.
I found this C# Code and converted it to vb.net:
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute> _
Public Partial Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
Shared Sub New()
AppDomain.CurrentDomain.AssemblyResolve += New ResolveEventHandler(AddressOf CurrentDomain_AssemblyResolve)
End Sub
Private Shared Function CurrentDomain_AssemblyResolve(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
If args.Name.Contains("ssisHelper") Then
Dim path As String = "c:\temp\"
Return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "ssisHelper.dll"))
End If
Return Nothing
End Function
End Class
But I do not have Micorosoft.SqlServer.Dts.**Tasks**. Anyone who can help me either get this script working or can provide another solution to get my dll running within the script compoment?
And before adding it to the GAC you need to strong name it, example here: http://microsoft-ssis.blogspot.com/2011/05/referencing-custom-assembly-inside.html
You need to add the assembly to the GAC (Global Assembly Cache) by running gacutil -i assembly.dll
More information can be found here
https://msdn.microsoft.com/en-us/library/dkkx7f79(v=vs.100).aspx

Let VB Form prepare working environment in chosen directory

I am working on a GUI for a simulation program. The simulation program is a single .exe which is driven by an input file (File.inp placed in the same directory).
The Original.inp file functions as a template from which the form reads all the values into an array. Then it changes these values reflecting the changes done by the user in the form. After that it writes all the new values to File.inp.
By pushing the "Run" button the Simulation.exe file is executed.
The folder structure looks like this:
root
|
|---input
| |
| |--Original.inp
|
|---GUI.exe
|---Simulation.exe
|---File.inp
Ideally I would supply only the GUI, the user would select the working directory and then the GUI.exe would create an input folder and extract the Original.inp and Simulation.exe in the appropriate locations. So far I have only managed to include Original.inp and Simulation.exe as "EmbeddedResources" in my VB project and I have let my code create an input folder in the working directory chosen by the user.
Can someone please explain to me how I can extract the .inp and .exe file into the correct directories? I've searched on google, tried File.WriteAllBytes and Filestream.WriteByte but did not get the desired results.
The problem with File.WriteAllBytes was that I could not point to the embedded resource ("Simulation.exe is not a member of Resources" and with Filestream.WriteByte I got a 0 kb file.
The question commenters are correct, this is probably a task best left for a setup program. However, that having been stated, in the interest of answering the question as asked I offer the following approach.
Contrary to your supposition in your question comment, you do need to "read" the embedded resource from the GUI's executable file, since it's an embedded resource and not an external resource. It won't magically extract itselt from the executable file. You need to do the manual read from the assembly and write to your specified locations. To do this, you need to read the resource using .Net Reflection, via the currently executing assembly's GetManifestResourceStream method.
The Simulation.exe file is a binary file so it must be handled as such. I assumed that the Orginal.inp file was a text file since it afforded the opportunity to demonstrate different types of file reads and writes. Any error handling (and there should be plenty) is omitted for brevity.
The code could look something like this:
Imports System.IO
Imports System.Reflection
Module Module1
Sub Main()
'Determine where the GUI executable is located and save for later use
Dim thisAssembly As Assembly = Assembly.GetExecutingAssembly()
Dim appFolder As String = Path.GetDirectoryName(thisAssembly.Location)
Dim fileContents As String = String.Empty
'Read the contents of the template file. It was assumed this is in text format so a
'StreamReader, adept at reading text files, was used to read the entire file into a string
'N.B. The namespace that prefixes the file name in the next line is CRITICAL. An embedded resource
'is placed in the executable with the namespace noted in the project file, so it must be
'dereferenced in the same manner.
Using fileStream As Stream = thisAssembly.GetManifestResourceStream("SOQuestion10613051.Original.inp")
If fileStream IsNot Nothing Then
Using textStreamReader As New StreamReader(fileStream)
fileContents = textStreamReader.ReadToEnd()
textStreamReader.Close()
End Using
fileStream.Close()
End If
End Using
'Create the "input" subfolder if it doesn't already exist
Dim inputFolder As String = Path.Combine(appFolder, "input")
If Not Directory.Exists(inputFolder) Then
Directory.CreateDirectory(inputFolder)
End If
'Write the contents of the resource read above to the input sub-folder
Using writer As New StreamWriter(Path.Combine(inputFolder, "Original.inp"))
writer.Write(fileContents)
writer.Close()
End Using
'Now read the simulation executable. The same namespace issues noted above still apply.
'Since this is a binary file we use a file stream to read into a byte buffer
Dim buffer() As Byte = Nothing
Using fileStream As Stream = thisAssembly.GetManifestResourceStream("SOQuestion10613051.Simulation.exe")
If fileStream IsNot Nothing Then
ReDim buffer(fileStream.Length)
fileStream.Read(buffer, 0, fileStream.Length)
fileStream.Close()
End If
End Using
'Now write the byte buffer with the contents of the executable file to the root folder
If buffer IsNot Nothing Then
Using exeStream As New FileStream(Path.Combine(appFolder, "Simulation.exe"), FileMode.Create, FileAccess.Write, FileShare.None)
exeStream.Write(buffer, 0, buffer.Length)
exeStream.Close()
End Using
End If
End Sub
End Module
You will also have to add logic to determine if the files have been extracted so it doesn't happen every time the GUI is invoked. That's a big reason why an installation program might be the correct answer.

Importing B.exe into A.exe an then run B.exe from A.exe

I am using Visual Basic 2008 and I have a question about it?
I have a.exe and b.exe ( the a.exe is an vbApp, and b.exe is an executable file ). Is it possible to include the b.exe into a.exe and then running it from a.exe? By, for example, importing the b.exe into vbProject and then running it without extracting it.
The question is a bit vague, but you can definitely do something along those lines, in several different ways.
First, you can certainly compile a separate EXE (I'll call it EXEA) into a VB Project (Call it EXEB). When user runs EXEB, it extracts the resource containing EXEA, saves it as a file (likely to the temp folder or someplace with WRITE rights) and then shells to EXEA.
Another possibility would be to compile external functionality into a DLL, call it DLLA, then compile that dll into a VB project (call it EXEB).
When user runs EXEB, it extracts the resource containing DLLA, storing it as a memory stream, then uses ASSEMBLY.LOAD to load the DLL from the memory stream (instead of from a file), and at that point can create objects from that dll, and use it as normal.
In both these cases though, it's probably better to simply compile the second EXE or DLL and include both in an MSI installation project.
More details in the question might help narrow down other possible solutions as well.
Try this :
1) Open project of B.exe.
2) Create a new module in project B, add "Sub Main" , then write this
Sub Main(Byval args() As String)
Dim X As New Form1 'replace Form1" with your real startup form
X.ShowDialog
End Sub
3) Open properties of that project B, and uncheck checkbox with name "Enable application framework". And change "Startup Object" to "Sub Main"
4) Compile and close project B.
5) Now open project of A.exe, and use following code to run B.exe in memory:
Dim tmpAssembly As System.Reflection.Assembly = System.Reflection.Assembly.Load(System.IO.File.ReadAllBytes("C:\b.exe"))'Replace "C:\b.exe" real b.exe path
Dim TmpMethod As MethodInfo = tmpAssembly.EntryPoint
If TmpMethod IsNot Nothing Then
Dim TmpObject As Object = tmpAssembly.CreateInstance(TmpMethod.Name)
Dim args() As String = Nothing
TmpMethod.Invoke(TmpObject , args)
End If
Me.Close()
CAUTION! You can run applications on memory only if you have done steps 1 and 2 with those applications. Otherwise you will get errors , which are not solved yet...

How do I get resource file values in Visual Basic?

I'm new to Visual Basic, and I'm having issues accessing the resource file for my project.
Dim rm As Resources.ResourceManager = New Resources.ResourceManager("MyProjectName.My.Resources.Resources", [Assembly].GetExecutingAssembly())
Dim myValue = rm.GetString(lookUpKey) 'boom Object reference not set to an instance of an object.
I think the issue is with the string "MyProjectName.My.Resources.Resources".
Would I be better off moving the strings into their own resource file?
I thought it was something similar to:
my.Resource.whateverhere
Is that not the kind of resources you are looking for?
Try
Global.<MyNamespace>.My.Resources.<ResourceStringName>
to access Resource Strings
Try ResourceManager("MyProjectName.Resources", ...), otherwise if it's the application resources you can simply use My.Resources.HD (see here:My.Resources Object)
or
Open Reflector, load your assembly there, go to resources, a list of resources appears, search for the one containg 'HD', copy the name (it's like MyProjectName.Resources.resources), remove the last .resources and try with that.
Refer to the MSDN article Retrieving Resources with the ResourceManager Class for naming convetions:
Dim myManager As New _
System.Resources.ResourceManager("ResourceNamespace.myResources", _
myAssembly)
If you load an external .resx file and want it to show up under intellisense My.Resources then you need to do 2 things.
First the file should be in the root of your project. Simply right click the project and do "Add Existing Item", and give it your .resx file. You should notice that there is no chevron to expand the resx file like your built-in resource file.
The last step is to highlight your resx file and go to the properties window. Under Custom Tool put "ResXFileCodeGenerator" and under the Custom Tool NameSpace put "My.Resources". You should now be able to programmatically access this resource under My.Resources.[name of the resx file].resource_item.
I was unable to access the resource file until I moved the .resx file into its own project and referenced that project from my main one. I also had to create a dummy class in that project so that it would compile into a DLL file.
The code for accessing the resource file is actually located in the generated Resource.resx.vb file.
I was able to access the resource file using the following code.
'Name of Class Library where I moved the resx file
Dim classLibraryName As String = "ResourceProj"
'Name of Resource File without the .resx suffix
Dim resourceFileName As String = "Mappings"
'Finding the assembly of the resx file, ResourceProjClass is a dummy class I created so that the dll would build.
Dim myAssembly As Assembly = GetType(ResourceProj.ResourceProjClass).Assembly
Dim rm As Resources.ResourceManager = Nothing
rm = New Resources.ResourceManager(classLibraryName & "." & resourceFileName, GetType(myAssembly)
Return rm.GetString(lookUpKey)
Simply:
Dim loginMessage As String = Global.Resources.NameOfYourResxFile.NameOFVariable

How to find a DLL given a CLSID?

I have a situation in which a managed DLL calls some unmanaged DLL. I know the CLSID of the unmanaged DLL, is there any way to find out what binary file houses that CLSID?
Normaly, you can just go to:
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\"GUID"
And find a key called "InProcServer32" for instance and there will be the default value that has the DLL. This is one simple way to do it.
Can you not just search for it in the registry using regedit and look for the binary path.
Based on BobbyShaftoe reply we can build a simple vbs script that reads that registry for us:
Dll_RegPath = "HKEY_CLASSES_ROOT\CLSID\<GUID>\InProcServer32\"
Paste the following to "test.vbs"
Sub Main
' used to find location of "System.Collections.ArrayList" progid dll
Const csGUID = "{6896B49D-7AFB-34DC-934E-5ADD38EEEE39}"
MsgBox srGetDllPathByGUID(csGUID)
End Sub
Function srGetDllPathByGUID( sGUID )
Const csRegPath = "HKEY_CLASSES_ROOT\CLSID\<GUID>\InProcServer32\"
Dim oShell: Set oShell = CreateObject("WScript.Shell")
Dim sReg: sReg = Replace( csRegPath, "<GUID>", sGUID ) ' build str
srGetDllPathByGUID = oShell.RegRead(sReg)
Set oShell = Nothing ' clean up
End Function
Call Main
You can also find ProgId by:
ProgID_RegPath = "HKEY_CLASSES_ROOT\CLSID\<GUID>\ProgID\"
I've found this question because I was troubleshooting some incorrectly installed application and my objective was to find and register ActiveX dll given the CLSID (which I've got from app sources). Hence my a little bit hacky approach.
I've issued a search in the directory where I believed the dll is located looking for CLSID in file contents. That did the trick, because it was stored in plain text in resources. I believe it's not always the case, but my problem was solved.