r/visualbasic • • Aug 11 '21

How to embed and use references as resources?

Hello. Are you able to store a reference like “naudio” or “SQLite” into a VB app resource (references -> highlight reference -> do not copy // then add file -> select dll -> as embedded resource) and then call on it? I can get it to be embedded but how do I call on it and use it? Anyone have success in this? The objective is to avoid having the libs hanging out with the exe after build (or in a lib folder with something like pretty bin). Any help would be great. Thanks

6 Upvotes

7 comments sorted by

2

u/RJPisscat Aug 11 '21 edited Aug 11 '21

I don't see any way to do this, but start with the why:

Are you trying to simplify deployment without an installer? Are you trying to hide a lib? Or something else?

Edit: Look at this article and see if it describes what you want.

2

u/AznBlusuazn Aug 11 '21

Are you trying to simplify deployment without an installer? Are you trying to hide a lib? Or something else?

Hide the libs as much as possible. I am using PrettyBin to create a \lib\ with the application and I also have a Sub that hides that folder from Windows, but I'm just trying to make the deployment as thin as possible, file wise.

Edit: Look at this article and see if it describes what you want.

Yes! It did. So I had to convert the code from C# to VB.net and make some modifications to make it work in VB.net. But this is what I ended up using:

(in a sub within the initialization of the VBForm, I have this line for since CurrentDomainAssemblyResolve that will reference an EmbeddedResource.dll that I want to "hide")

AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf (CurrentDomainAssemblyResolve1)

Then I have functions for each .dll, for example:

Private Shared Function CurrentDomainAssemblyResolve1(ByVal sender As Object, ByVal args As ResolveEventArgs) As System.Reflection.Assembly

Using stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApp.NAudio.dll")

Dim assemblyData = New Byte(stream.Length - 1) {}
stream.Read(assemblyData, 0, assemblyData.Length)

Return System.Reflection.Assembly.Load(assemblyData)

End Using

End Function

This is repeated for each .dll and it seems to work.... except for the following .dlls:

  • NAudio.Core.dll

  • NAudio.WinForms.dll

  • NAudio.WinMM.dll

  • System.Data.SQLite.dll

I can get the other 10+ dlls to work embedded but not these. But it's fine. 4 files in my libs folder is much nicer than the 15+ that were there.

Thanks for your help.

2

u/RJPisscat Aug 11 '21

Regarding the NAudio DLLs that aren't working: Are you compiling the DLLs from source? Are you calling directly into those DLLs, or are they dependencies of the others that do work?

Brainstorming ... I pulled up the source on NuGet and there were two releases in Feb, 1.10.0 and 2.0.0 (and 2.0.1 around June 2021), so make sure the DLLs are synched. Have you looked at Issues for clues?

The SQLite issues will be separate but possibly of the same nature.

Edit: Look at this article and see if it describes what you want.

Yes! It did.

I am utterly shocked and also not at all shocked; re the latter, it's been a while since I told someone "go away, that's not possible" then come back an hour later and said "I found/figured out how to do it" - a mixture of embarrassment and pride.

2

u/AznBlusuazn Aug 11 '21

Regarding the

NAudio

DLLs that aren't working: Are you compiling the DLLs from source? Are you calling directly into those DLLs, or are they dependencies of the others that do work?

Here is a "Jukebox.vb" that I'm using for the game I'm building. That is where I'm using NAudio : https://github.com/AznBlusuazn/Limitless/blob/master/Jukebox.vb

Have you looked at Issues for clues?

I will definitely look through there and see if the issue exist. I might even reach out to the dev of it. Maybe they will give insight.

I am utterly shocked and also not at all shocked; re the latter, it's been a while since I told someone "go away, that's not possible" then come back an hour later and said "I found/figured out how to do it" - a mixture of embarrassment and pride.

Off and on I've been messing with this idea of "hiding" the dlls. I just need someone else to say it could be done and push me a little more. I appreciate it!

2

u/ArbSoft Aug 17 '21

You can use a software called Smart Assembly from Red Gate, it does what you want and more (did you know that unless you obfuscate your code, it can be easily accessed from other apps like dotFuscator, eazFuscator, .Net Reflector (made by Red Gate again) etc? Basically all these apps can easily read your code, so you need to protect it with apps like Smart Assembly.

1

u/AznBlusuazn Aug 17 '21

Will definitely look at this. $1100 isn’t bad to protect your code. Thanks for the suggestion!!

1

u/ArbSoft Aug 17 '21

Personally I pirate it xD, but shh.