Forcing project to load DLL's from the current directory - vb.net

I am trying to make a program that works on every operating system by forcing it to load and use the DLL's in the current directory, not the windows directory, but it don't works. I tried to enable "copy local" and change the refference path, but without any success, the program tries to load the DLL's from the windows directory.
My question is: how can I fix this?

The Search Order for DLL's is documented here on MSDN. It also includes instructions on how you can modify the search order so that the local bin directory is searched first, instead of the GAC.

The directory %windir%\assembly is called the GAC. Assemblies are not copied there, but installed typically using gacutil /i or by installation packages.
GAC is a suitable folder for libraries referenced by lots of other libraries and applications in build versions that are not centrally coordinated. Using GAC allows you to have multiple versions of the same library, all of which might be indirectly required even by a single application, installed side by side on the system. Case in point is the .NET framework itself.
The assemblies that you build are probably not that kind. Application assemblies and libraries that are basically part of a single application should never make it to the GAC or you can get into trouble. There is a variety of possible trouble:
one accidentally or intentionally creates different (incompatible) builds of the same library with the same version number.
assembly in GAC references an assembly not in GAC
one app installs the same assembly into GAC, but another app wants to load it from its local folder (where application binaries reside).
Code in the GAC gets a preference when assemblies are loaded. To remove an assembly from the GAC, use gacutil /u.

Related

VB Newtonsoft JSON.Net "Could not load file or assembly"

I am using JSON.Net to get my Console application to translate sentences with Google Translate. My code fully works fine with absolutely no errors when I run it in Visual Studio. But when I take the .exe out of the "[project_name]\bin\debug" folder and copy it to the desktop to run it returns the following exeception.
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified....
I noticed that in my "[project_name]\bin\debug" folder there is the .dll file that I am using, but not only this, I am also using a .dll that I referenced somewhere else.
When I drag this .dll file with the exe onto the desktop and run it, it works. How do I make it so that I don't need the .dll with the .exe on the desktop? Or is this something that is necessary? I am sure that I have referenced everything properly.
If your application makes use of a library then that library has to be there to make use of. There are basically two ways to make libraries available to .NET applications. The library needs to either be in the same folder as the application or it needs to be installed in the GAC (Global Assembly Cache). The GAC is a common location for libraries so that multiple applications can access them. Unless you intend to ensure that JSON.NET is installed in the GAC on every machine you plan to run your app on, you need to make sure that the library is deployed along with your app. This is how applications work. There is no magic solution.

Why does .NET Core not add the reference Dlls from the nuget packages to the bin folder

.Net Core projects do not put the reference DLLs from the nuget packages in the bin folder. Is there a way of any properties that helps in doing that?
It's needed for some third party tools to understand the reference DLLs.
.NET Core, unlike .NET Framework, can resolve assemblies from half a dozen locations. This includes the NuGet cache, servicing cache, runtime store, local app directory, and shared framework folder. During development, these are typically found in the NuGet cache (%USERPROFILE%.NuGet\packages) This makes it unnecessary to copy referenced assemblies to the build output folder (bin) until you publish your application. For more details on how that works, see https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/corehost.md
You can force the SDK to copy assemblies to your build folder by setting the proper below, but it increase disk use and build time.
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
Or you can use the deps.json and runtimeconfig.json file to locate required assemblies.

Registering DLLs and .TLBs

I am working for a company that registers a lot of COM DLLs in the System32 folder/SYSWOW64 folder. I recall a question some time ago where Hans Passent said that this is wrong as the folders specified are for Windows DLLs. I cannot find the post so I want to check that this is correct?
Also if I want to register a COM DLL or a TypeLibrary in a foreign folder then can I just use the following command (for COM):
regsvr32 app.dll
Finally I was reading about the CODEBASE flag of Regasm: http://www.vbforums.com/showthread.php?597928-RESOLVED-How-to-delete-a-VB6-Reference. Do you have to use this flag if you want to register a TypeLibrary (.TLB)? What happens if you .NET assembly uses a third party library that is not signed?
If you are talking about installing COM DLLs in the System folder, then you are correct. All applications, and their support libraries, should be installed under the Program Files folders, or the Common Program Files folders.
You are also correct that REGSVR32.EXE can be used to manually register DLLs and OCX. "Foreign folder" is not a Windows concept - you can register a component anywhere in the file system, including the Windows and Windows System folders. By the way, if you use an installer, then you shouldn't have to use REGSVR32.EXE.
However, TLB files cannot be registered with REGSVR32.EXE, because that application basically loads the DLL/OCX, and calls an exported function on the library, so effectively the library registers itself. Instead, you need another tool, e.g. REGTLIB.
You don't use /CODEBASE to register a raw type library, because REGASM is used for registering .NET DLLs as COM components, not TLB files.

Can .local files be used during VB6 compile to avoid registering COM ocx and dll files

In an attempt to keep my build machine clean can .local files be used during the compile of an application or is there a better way to keep the bloat off the machine.
.local is used to force Windows to use the COM DLLs in the VB6 application directory in preference to the most recent version stored in the registry. It doesn't replace the necessity of registering the DLL as Windows need to look in the registry for the CLSIDs of the older version.
Registry free COM DLLs is explained here and involves the creation of a manifest file. Similar in principle to what .NET goes through with it's assemblies except .NET handles this issue automatically and with ActiveX DLL it is more of a manual process.

Linking against a .dll version older than the installed one in Visual Studio 2005

Please excuse the large amount of background info, but this is a fairly specific question.
I have a Visual Studio 2005 project that creates a .dll file. This project depends on a .dll from another company, "BigCorp", which gets installed with BigCorp's software.
Ideally the dll I create should be usable with any (backwards compatible) version of BigCorp's dll. That is, if I link against version 4.3.2.100 my program should still work if the user upgrades BigCorp's app so that the .dll is version 4.3.3.20.
This is straightforward enough in VS2005, by looking at the project's References, selecting the .dll and making sure the "Specific Version" property is false.
Now for my actual question: Is it possible to specify a base version of the .dll that is older than the one installed on the machine that is building the project? Continuing my previous example, my build machine has version 4.3.3.20 installed, but I want the .dll it generates to be compatible with installations that haven't upgraded, and only have version 4.3.2.100 installed.
It seems that VS resolves the path to the .dll and detects the runtime version based on the file it finds. Trying to override it by specifying the version number directly in the .vbproj or .csproj, i.e.,
<Reference Include="BigCorp.Program.Component, Version=4.3.2.100, Culture=neutral, PublicKeyToken=abc123def456654fed321cba, processorArchitecture=MSIL">
has no effect. Trying to register my assembly on a computer that has the older version fails with the good old "Could not load file or assembly" error because it tries to find the newer version of BigCorp's .dll.
Is what I want to do even possible?
You can do this by referencing the older DLL directly from the project, so copy the .dll file to your project, then reference it from there.
I do this all the time with my various development projects. Typically I'll put a "References" folder inside my project to hold referenced DLL's