DllNotFoundException for .dll which is in current folder - c#

I'm getting a System.DllNotFoundException for a .dll which is in the same folder as the executable whenever my application attempts to use a function which exists in the DLL. The weird thing is that it is only occurring on one user's PC; it works fine on my dev PC and it works fine on one non-dev PC that I tried it on. Also, there are other DLLs in the folder which are being found and used correctly. The DLL in question is a native library which is referenced by my application via another DLL which is a c# wrapper for the native library.
My initial instinct is that there must be some other library being referenced by this DLL which doesn't exist on the problematic PC, but I cannot imagine what library this PC could be missing that the other non-dev PC has.
So my questions are this: is there a way to determine the dependencies of a given DLL file? Keep in mind that the DLL in question is a native library (i.e. not managed code), and I do not have access to it's source code. And if it turns out no dependency is missing, what else might cause such an issue?

For unmanaged dlls you can use Dependency Walker to find dependencies.

I would suggest using ILSpy to open the dll and view its dependencies first.

Related

Registration-free COM and DLL location

My C++ app was required to communicate with a web application. Since writing in C++ was hard I wrote the web application client in C# (DLL), COM enabled it, and called it from C++ app and it worked.
But the C++ app can be installed on a server PC, its folder shared over a network, mapped to a drive on a client PC, and run from there. When I tried running so, it doesn't run because it is expecting the C# DLL to be registered on the client PC. I would like to avoid it. I would like to keep them both on the server. Is it possible to do it using registration-free COM? If yes, can the C# DLL (and its dependent DLLs) be placed in a folder different to the C++ EXE folder?
I see this is an outdated post. Hope you had found out a solution or a workaround. Anyhow, I shall answer as it may help someone with the same problem.
Is it possible to do it using registration-free COM?
Yes, it is. Follow this MSDN article. You will have to create a manifest for the c# com dll and embed the manifest into it. Which would be with the help of a RT_MANIFEST resource. That article is a little outdated, so you may face problem with .net framework version. If you did, you will need to specify the .net framework version in your machine. You can simply replace the clrclass line with this :
<clrClass
clsid="{16AD5303-E154-44B4-A72B-C129859714AD}"
progid="SideBySide.SideBySide"
threadingModel="Both"
name="SideBySide.SideBySideClass"
runtimeVersion="v4.0.30319" >
can the C# DLL (and its dependent DLLs) be placed in a folder
different to the C++ EXE folder?
Yes, it can be. But I wouldn't recommend it. According to this resource, the assembly loader searches for assembly manifests in the following order :
Side-by-side searches the WinSxS folder.
\\<appdir>\<assemblyname>.DLL
\\<appdir>\<assemblyname>.manifest
\\<appdir>\<assemblyname>\<assemblyname>.DLL
\\<appdir>\<assemblyname>\<assemblyname>.manifest
The above locations are respective to the application (appdir). If you want your dll to be elsewhere, then you can use the file element of the assembly manifest (the name attribute can include a path), see here for more details and other elements.
Hope this helps someone.

CoCreateInstance C# COM component from native app does not find reference

I have a question concerning a COM C# component being created from a native C++ application.
The C# component is referencing an assembly which contains the COM interface.
So project A(.dll) contains the COM interface, project B(.dll) contains the COM class that implements this interface.
This C# COM component is then registered using regasm from some folder on my system, say for example:
C:\TestComponent which contains A.dll & B.dll, B being the one registered using regasm.
When my native C++ application (COM server), which is installed in another path, is trying to create an instance of the C# COM class from project B this fails complaining that A.dll can not be found.
If I copy (only) A.dll to the installation directory of my native C++ app, everything works perfectly.
Any ideas on how to tell my native C++ app that it needs to find the A.dll in a specified directory or in the directory where B.dll resides?
Kind regards,
Dwight
Having dependencies in a COM server is always a problem, whether it is an unmanaged server or one written in C#. Just like Windows, the CLR will only look in a few select places for a DLL. Which are the GAC and the directory where the EXE resides. With an option to also look in subdirectories of the EXE directory by using a app.exe.config file.
Knocking off the candidates here: you'll want to avoid giving the client EXE a .config file, you don't control its location nor configuration, it is somebody else's program. Same problem with the EXE directory.
Which leaves the GAC.
Also the location that Regasm.exe prefers, just omit the /codebase option when you register after you put the assemblies in the GAC. And a very good way to solve the DLL Hell problem that's so strongly associated with COM, the GAC can store different versions of your DLLs.
As far as I understand, you only registered B.dll. That is the reason, the COM System has no knowledge, where A.dll reside. I do not know a way to tell the application where it can found a, except of changing the workingdirectory or add the path to the A.dll to the system path variable or last but not least copy the A.dll into the system32 directory (but I hate that).
By the way, having a DLL proxy, like A.dll is, reside in the same directory as your application seems totally fine to me.
Ah one more thing. You can, if you now the path of the A.dll but did not want any of the above solutions, you can have a look into Loadlibrary.

Loading C# DLL to C++/CLI - dependencies directory

I wrote a dll c++/cli library which uses my other c# dll library. C++/cli library works fine when I've got c# dll in the same folder as application which calls it. This library will be finally loaded to many applications and a C# dll must not be copied into directory with application. It has to be in the same folder as c++/cli library, but in that cases I've got System.IO.FileNotFoundException.
My suggestion is to load c# library manually or to change path where f.ex. firefox is looking for dependencies, but I tried with LoadLibrary() and Assembly::LoadFrom() methods to force loading from right directory. Of course I added directory path with dll to system PATH.
I work on VS2010.
You don't change the default directory where an application will look for dlls.
At design time put your dll in some well know location, the one you are going to deploy to. Add a reference to it, make sure it's set to Don't copy ever, otherwise it will end up in the bin folder. You have to do this otherwise it won't compile.
When you deploy, you'll need one package to deploy common dlls, and one for each application. Careful you don't create your own version of dll hell, if appA needs an older or new version of the common dll, compared to AppB
Add an AppDomain.AssemblyResolve event to main (for windows app). At run time the event handler will get fired when you reference a type in your dll, and the assembly has not yet been loaded.
In there you load it from the well known location. That usually in config, or in a relative path.
E.g.
AllMyApps
CommonDLLS
MyFirstApp
So the path you load the required common dll from would be "..\CommonDlls\MyCommondll.dll".
NB you will want to secure the dlls in some way, otherwise a bad guy might be able to inject their version of one in to your app, which would be bad...
You can use this mechanism to get the dll from a remote server or a database blob as well.
The simplest case is only a few lines of code, just look the event up. Took me about 15 minutes to get this going in a similar scenario.
Not on this machine though, otherwise I'd have pasted in the code.

Referenced DLL Cannot Be Found When Running Application

I have a weird situation with some code that I inherited at work. Their application is a multi-project solution, with several of the solutions being (code) pieces of the MS Enterprise Library (not sure which version).
They also have an existing C++ (unmanaged) application which has a bunch of DLLs. One of these DLLs is built in a separate solution, both in 64-bit and 32-bit flavours.
The main application has a reference to this DLL, and calls a couple of static functions (I can see intellisense, even). I can compile and build the main application EXEs, but when I run it, I get an exception that this DLL from the unmanaged code (lets call it CPlusPlusCode.dll cannot be found:
FileNotFound Exception was unhandled: Could not load file or assembly 'CPlusPlusCode.dll' or one of its dependencies. The specified module could not be found.
I'm quite stumped, because I can compile the code, see intellisense for the imported classes, and dig into the DLL in the object browser. I even made sure there's a copy in the \bin\Debug folder (although I don't see why that would make a difference). This is for a Windows Forms application.
Also, if it matters, I had some build issues related to x86 vs. x64 for different projects; I think (hope?) that this is not related to that, but I solved that by using the Configuration Manager to build everything as x64.
Check the GAC, and if necessary you might need to add it or register the DLL there.
I had this problem with a project, it all works ok from Visual Studio and most of my times running the project locally on my machine. But because of the unmanaged code I needed specifically allow the project to be executed with correct permission levels.
So have a look in the manifest file, that enough permissions etc exist.

c# dll not found

I am writing a small AOL IM application in C#. I have all the dlls that I need and I have been able to compile and run my app. However, when it runs I get an error that says
"Unable to load DLL 'acccore.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
I understand that this means the acccore.dll file couldn't be found but I don't know why. I tried putting it in C:\Windows\System32 and it is also in the debug directory that is created when the project is build in Visual Studio. Can anyone tell me how to make my application know where this dll is located?
Thanks!
I did some research and it looks like acccore.dll is a COM DLL file. That means you need to run:
regsvr32.exe C:\Windows\System32\acccore.dll
This will register the COM DLL into the registry, you can then use that DLL in your .NET code. Check out the link:
http://64.12.130.129/forum?c=showthread&ThreadID=1173
So you will need to use P/Invoke to use the DLL (I guess the AOL SDK has some example code you can use).
is this dll an assembly?
If so then fuslogvw will show you where the CLR is looking for assemblies. Put it where .net is looking
.NET assemblies need to be in the directory of the application (or one of its subdirectories, especially if they represent a localized version of a different assembly), or in the GAC (global assembly cache.) If the DLL isn't in the same directory as the .EXE, then that's your problem. If it is and it still doesn't work, it means that the assembly doesn't match for some reason.
I used dependancywalker to find the dll I was missing that caused it to error. This was suggested by Taylor Leese in a comment.

Categories