Dll is missing right after publishing it. .net WinForms - c#

Where the files of published project will go after installing it ? I tried putting the DLL inside of the setup folder there yet it has still a same problem.
I'm trying to use the DLL using pinvoke
[DllImport("tc-b_new_sdk.dll", CallingConvention = CallingConvention.Cdecl)]
I receive this error:
System.DllNotFoundException:
Unable to load DLL 'tc-b_new_sdk.dll': The specified module could not be found.
(Exception from HRESULT: 0x8007007E)

Try adding Path of your DLL like this :
[DllImport("C:\Users\User\Desktop\tc-b_new_sdk.dll", CallingConvention = CallingConvention.Cdecl)]

Either the DLL itself, or one of its dependencies cannot be found.
If the DLL is in the same directory as the executable, this points to the issue being the dependencies. We can't know what the dependencies are. That information should have been supplied with the DLL. A common cause of this error is that your program isn't able to resolve a dependency on the MSVC to which the DLL is linked.

Related

DLLnotfoundexception:Unable to load DLL 'Pine.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Am trying to load a unmanaged dll(C++) through DllImport method but am getting below exception.
Unable to load DLL 'Pine.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
I have placed my dll in the path bin\Debug\ (am running in debug mode). But am getting this dllnotfoundexception.
Before formatting my PC and freshly installed VS2015 it was working fine. But when i reinstalled my OS and installed VS-2015 it is giving this error. Am i missing any dll or something that support DllImport.
Any help are appreciated.
From what I remember on Windows the search order for a dll is:
Current Directory
System folder, C:\windows\system32 or c:\windows\SysWOW64 (for 32-bit process on 64-bit box).
Reading from the Path environment variable
In addition I'd check the dependencies of the DLL, the dependency walker provided with Visual Studio can help you out here, it can also be downloaded for free: http://www.dependencywalker.com
You can check what the current directory is with Environment.CurrentDirectory
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Environment.CurrentDirectory);
}
}
It's probably different from the bin\debug directory

DLL Suddenly Not Loading C# UWP

I put a dll build in native C++ as a Universal DLL in my project directory of C# UWP app and setting content to copy always it was finding it and then the next day suddenly nothing, constantly getting:
"Unable to load DLL 'AVEngine.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
I am calling with:
[DllImport(DLLName, CallingConvention = CallingConvention.Cdecl)]
private static extern int OpenForProcessing();
I ensured the architecture and build config matches, I have also checked SDK versions (min/target versions), I'm stumped.
Use Dependency Walker to check dependencies of your AVEngine.dll. Probably some of them missing and because of it Dll cannot be loaded.
Better proper approach to expose old functionality to your modern C# UWP application is to wrap AVEngine.dll logic using a WinRT component. Then you can reference this component in UWP application. Article "Use Existing C++ Code in a Universal Windows Platform App" could be helpful.

DllImport fails in C# test (MSTest)

I have a C# project which is a library I reference in one of my C# tests I have:
myproj
|-bin
+-External
| |-pHash.dll
|-main.cs
|-MyClass.cs
|-myproj.csproj
pHash.dll is a Visual C++ library (compiled using a vcproj project, so we are talking about Microsoft C++ flavor) called phash which exposes this method:
int ph_dct_imagehash(const char* file, ulong64& hash);
I want to use this method from MyClass.cs. So I do this:
public class MyClass {
[DllImport("pHash.dll")]
static extern int ph_dct_imagehash(
[MarshalAs(UnmanagedType.LPStr)] string file,
[MarshalAs(UnmanagedType.U8)] UInt64 hash);
public MyClass() {...}
public DoStuff() {
UInt64 outputValue = 0;
ph_dct_imagehash("a string", outputValue); // The second parameter should be a pointer in the C++ implementation, not really sure if I am doing it right here
}
}
In my project, I set External/pHash.dll to be Content and also set it to be Always copy in the output folder.
A possible mistake As I pointed out, the way I use ph_dct_imagehash might be wrong, but this question is about a different error I get, so please skip this and move on :)
MyClass is then referenced in one of my tests I execute using MSText.exe.
The error
When I try to run my test which is using that class I get:
System.DllNotFoundException: Unable to load DLL 'pHash.dll': The
specified module could not be found. (Exception from HRESULT:
0x8007007E)
What am I doing wrong?
Second attempt
I saw that, when building, actually my bin folder looks like this:
myproj
+-bin
|-External
|-pHash.dll
So I thought I had to change the path to the DLL:
public class MyClass {
[DllImport("External/pHash.dll")]
static extern int ph_dct_imagehash([MarshalAs(UnmanagedType.LPStr)] string file, [MarshalAs(UnmanagedType.U8)] UInt64 hash);
...
}
But then I get:
System.DllNotFoundException: Unable to load DLL 'External/pHash.dll': The
specified module could not be found. (Exception from HRESULT:
0x8007007E)
Another question
Is there a (maybe debugging?) tool to understand what path is the CLR trying to follow when encountering DllImport? So at least I know where it is trying to look for that dll...
More info
I have been asked to inspect my dll, so by using DependencyWalker on pHash.dll, I get this error:
Error: The Side-by-Side configuration information for
"c:\myuser\testresults\myuser-0131
2016-05-06 08_56_50\out\PHASH.DLL" contains errors. The application
has failed to start because its side-by-side configuration is
incorrect. Please see the application event log or use the
command-line sxstrace.exe tool for more detail (14001). Error: At
least one required implicit or forwarded dependency was not found.
Error: At least one module has an unresolved import due to a missing
export function in an implicitly dependent module. Error: Modules with
different CPU types were found. Warning: At least one delay-load
dependency module was not found. Warning: At least one module has an
unresolved import due to a missing export function in a delay-load
dependent module.
This error is also reported in the stack trace when I run my test. I thought it was a message coming from MSTest about something else, but now I realize it is a problem of this DLL!
Even if your DLL is found (sits in the test directory), it is still reported as missing when some of it's dependencies are not found. Make sure you copy the DLL itself and all DLLs it depends on.
You can set select your dll in Solution Explorer, right click on it and set BuildAction to Content and Copy To Output directory to CopyAlways. That way your dll will be copied to application directory and DllImport should find it.
Alternatively, use DllImport's SetDllDirectory with DLL path before first call of your imported dll
[DllImport("kernel32.dll")]
static extern bool SetDllDirectory(string yourPath);

Unable to load DLL ' mydll.dll': The specified module could not be found

On my laptop, where I am developing the WPF application, everything works fine, debug and launch the .exe app.
My app uses a native DLL, for resolve the reference problem I add the DLL in bin/debug (release) folder. I access it using DllImport like this:
[DllImport("xptodll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int LDA_About();
The problem is when I try to run the .exe app on another PC, when I need to access the DLL it crashes. I make a handle to log any unhandled exceptions and the following error appears:
Unable to load DLL ' xptodll.dll': The specified module could not be
found. Exception from HRESULT: 0x8007007E)
The bin/debug directory has xptodll.dll, and app files: .exe, .application, .exe.config, .exe.manifest, .pdb.
Maybe this is important, the xptodll.dll interacts with hardware, but why wouldn´t it have the same behaviour on both machines?
There is probably some further dependency that is failing. My guess is that xptodll.dll itself has dependencies on other libraries that are missing on the failing machine. The documentation of xptodll.dll should explain what dependencies are needed. If the documentation does not make it obvious what is missing, you can diagnose the problem yourself using Dependency Walker.
Another issue might be (beside all this "put the DLL in the correct location") that if the DLL was created with Visual Studio, eg. Visual Studio 2012, also the VCRedistributable for 64 bit must be installed (vcredist_x64.exe), which is profided by Visual Studio.

Unable to load Win32 Native DLL file from C#.NET

I have a C# winapp. I call a native .dll file (created in C++ by myself) from the C# app, and it works fine.
But when I copy my application (.exe and .dll files) to another machine, I get an error that says:
Unable to load DLL "c:\dllname.dll": The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Here is the C# code:
class IsoMessageHelper
{
public const string ISO8583_DLL = "c:\\Hc8583.dll";
[DllImport(ISO8583_DLL, CallingConvention = CallingConvention.Cdecl)]
public static extern bool InitializationRq(...)
}
What should I do?
A common issue when deploying .Net applications that have native dependencies, is that the native dlls may be missing dependencies themselves on the target machines e.g. the correct version of the C runtime.
Use a tool such a Dependency Walker to analyze your native dll and determine if it has a missing dependency on the machine you have copied it too.
Try not to hard code any paths in the DllImport attribute parameter that specifies the name of the file. Then you should make usre the file is right besides the executable.
Something like this:
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
Move the DLL to the root. If that works, then look at your attribute to determine why. You haven't posted any code, so I can't give you any specific reason.

Categories

Resources