0

I have a Solution in Visual Studio 2017 with around 10 Projects intending to use the same dll. The easiest way to refernce the dll for me seems to add it to 'References' of each of the 10 projects. The samples are .exe files located in different folders. They should all access the same dll in one folder called ../DLL. Unfortunately the .exe files run only in case the dll is in the same directory. I found a lot of threads concerning that topic, but there seems to be no easy way out like just changing a kind of path property or like extending the search range to the environment paths or something like that ...

tom2051
  • 82
  • 9
  • Is there a reason why the library should not be in the same folder? It's the default. You can change it, but it might be more work than you are willing to invest. – nvoigt Jan 13 '20 at 15:36
  • the exe-files are in different folders, so I would need one copy of the dll for each folder. I only want to use one dll-file. I would also be happy to access the dll in for example c:\Windows\System32\. – tom2051 Jan 13 '20 at 15:48
  • add your dll contained folder to the system PATH environment variable. However you should do it on every computer is going to use your solution – oleksa Jan 13 '20 at 15:51
  • Are you asking about [gac](https://stackoverflow.com/q/1268342/1997232)? Never tried it, but [it seems easy](https://stackoverflow.com/q/2182316/1997232). – Sinatr Jan 13 '20 at 15:54
  • thanks for your comment, oleska. I tried adding my dll to a known path like c:\Windows\System32 and I added a own path containing the dll. Both did not work. – tom2051 Jan 13 '20 at 15:54
  • There is a reason that the default is to copy it with evry executable. All other solutions have serious drawbacks. So I'll ask again, *why* do you want to have it in one central location? There is no single best option, but maybe if you tell us what you want to achieve, we can suggest the least horrible thing to achieve your goal. – nvoigt Jan 13 '20 at 15:55
  • yes, I found the GAC solution, but I thought there would be a easier way ... – tom2051 Jan 13 '20 at 15:57
  • @tom2051 sounds strange since `system32` contains a lot of dlls that are referenced by windows applications and it works (usually). Is your dll `managed` or `unmanaged` ? – oleksa Jan 13 '20 at 16:03
  • thaks for your comment, nvoigt. The main thing why to have one location for the dll, is to be able to replace the one dll by one new one. There must be me misunderstanding something. Does adding the dll to 'References' of the project not link the dll dynamic ? Why are there sideeffects ? Does'nt each program calling the dll open a new instance of the dll ? – tom2051 Jan 13 '20 at 16:12
  • @oleska, thanks, how do I tell my project to search not only in the exe-path, but also in the paths stored in the environment variable 'path' ? Maybee the way to include the dll by just adding it to 'References' is wrong ? – tom2051 Jan 13 '20 at 16:25
  • @oleska, there is indeed some unmanaged code inside the dll. – tom2051 Jan 13 '20 at 16:28

2 Answers2

0

As far as I know you need to tell every single project what .dll to reference and where to find it. So no, I don't think there is any other way than to just add it to the refrences. Then again, adding 10 references costs you what, 3 minutes?

Honduriel
  • 117
  • 1
  • 10
0

I would like to suggest two solutions First is that you can put the referenced dll to the lib\dllreferenced folder and commit it to the source control that stores all projects. Then you can reference this dll from any project in the repository and the dll will be copied to the bin\Debug or bin\Release for each project.

To get new dll version to be used in the solution just put new dll version in the lib\dllreferenced folder and all of you projects will get this updated dll.

However storing dlls in the source control is not the best approach so you can try another one: to create nuget package containing the dll(s). Then you can manage package version easily using standard NuGet package versioning. You can put reference to this package to any project in any solution and it will be resolved by the VS automatically during build.

To deliver package to the computers that compile source code you will need to use certain repository to store nuget package. It can be nuget server (like http://nuget.org) or shared folder in your organisation. Here is how you can create nuget for unmanaged dlls.

oleksa
  • 3,688
  • 1
  • 29
  • 54
  • thanks a lot, oleska. I want to use one dll at one location. Is there no way to copy a dll to maybee c:\Windows\System32 and tell app1, app2 and app3 to use this dll by adding it to their 'References' and 'using' it ? Maybee there's no way, so we should stop here. I'll try some of the sharing methods like nuget or gac ... – tom2051 Jan 14 '20 at 10:49
  • @tom2051 how do you use the `unmanaged` dll in the project and how do you create references to those dlls? I used a lot of `[DllImport("advapi32.dll")]` attributes and there is no `advapi32.dll` in the same folder as my application binaries and it works. Please read that .net can load `managed` assemblies in the [assembly subfolders](https://learn.microsoft.com/en-us/dotnet/framework/deployment/how-the-runtime-locates-assemblies#step-4-locating-the-assembly-through-codebases-or-probing) but this is not about `unmanaged` dlls. – oleksa Jan 14 '20 at 11:38