Unable to load DLL in WPF C# application - c#

I get following error message (VS2010) when running in debug mode my C# WPF appliction:
"Unable to load DLL 'VCECLB.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
The code where this error occurs is (see hDevEnum):
namespace Imperx.FLExGrabber
{
public class Enumerator:IEnumerator
{
private IntPtr hDevEnum;
VCECLB_EnumData enumData;
/// Default constructor
public Enumerator()
{
enumData.cbSize = (UInt32)Marshal.SizeOf(enumData);
// Open enumerator handle
hDevEnum = NativeFunctions.VCECLB_EnumInit(); <<--- Error message here!!!
}
/// Destructor
~Enumerator()
{
NativeFunctions.VCECLB_EnumClose(hDevEnum);
}
}
}
From the existing project, which uses a windows form application, it runs perfectly. Now I need to transfer this solution into a WPF-application. Therefore I am using the same machine (Win7/64ibt) with the same VS2010. The platform target is X64.
Question: Where I need to add the VCECLB.dll file into my project? I can not add it under references and therefore I put it into the folder "...\bin\x64\Release" - but no success.
When I check the VCECLB.dll with dependency walker I get following:
Does the question marks means that those dll's are missing? If so, why I can run the windows form project with the same VCECLB.dll?
Does anybody know how I can solve this issue?
Thanks in advance

There are two common causes for such an error:
The DLL that you are referring to is not on the DLL search path, or
The DLL that you are referring to is found, but its dependencies cannot be found.
Resolve problem 1 by putting the DLL in the same directory as the executable. Resolve problem 2 by making sure that all dependencies are installed. Typically this involves deploying the MSVC runtime that the DLL depends upon.

You can put your VCECLB.dll any where in your solution (in root for example by Drag & Drop)
Then, once you added the file, click right on your file, choose properties
In Advanced three:
Choose content in Build Action
And Always copy in Copy to output Directory
to get something like the follwing:

Related

Using multiple DLL's written in C within a C# program

I have a program written in C#. I want to use a DLL made in C within this program.
public class Test
{
[DllImport(#"C:/.../surcouche2.dll")]
public static extern int refreshEntities();
}
I call the function as:
Test.refreshEntities();
I am using Visual Studio. To create the DLL surcouche2.dll, I created a new Visual C++ Application Console, then I chose DLL and left the options on their defaults. Then, I told to VS to compile using C.
The function is :
__declspec(dllexport) int refreshEntities() {
int ret = 0;
LibData *n;
n = newLibData(LIB_MODULES_MODULE_ENTITES, LIB_MODULES_ACTION_SELECT,
"http://10.0.2.2:4242/WebService1.asmx");
if (n)
{
ret = n->refreshDb(n);
n->destroy(n);
}
return (ret);
}
The functions called in are in another DLL (that is referenced). But I get this error:
system.DllNotFoundException: Impossible de charger la DLL 'C:/.../surcouche2.dll': Le module spécifié est introuvable. (Exception de HRESULT : 0x8007007E)
à app.Lol.refreshEntities()
à app.MainWindow..ctor() dans c:\...\MainWindow.xaml.cs:ligne 30
But, if I change the function refreshEntities like this, then it works fine:
__declspec(dllexport) int refreshEntities() {
return (42);
}
The second DLL uses another DLL (both in C). I created them with the option "create an empty project" so I haven't the stdafx.h, ... files. I guess it's not a problem because I can use a function from the third DLL in the second one.
This error can happen if a dependency of loaded dll can't be resolved.
First of all I propose you to put both native assemblies in a bin folder and change a reference
from [DllImport(#"C:/.../surcouche2.dll")] to [DllImport("surcouche2.dll")]
In general to debug dependency issues tools like Dependency Walker and Process Monitor can be useful. First helps to understand what the dependencies are, second to check there your application is trying to find the assemplies.
Configure Process Monitor's to show process activity only and apply process name filter with your application name like on the picture below and you will get all files your app trying to access. It will help to find missing dll's.
[DllImport(#"C:/.../surcouche2.dll")]
This is bad idea, you are getting a preview of what is going to go wrong on your user's machine as well. Hard-coding the path to the DLL like that helps the pinvoke marshaller to find the DLL file on disk. But it does absolutely nothing to help the operating system to find DLLs that surchouce2.dll needs. Like the one that contains the newLibData() function.
The error message is often mis-interpreted. It says it "cannot load" surcouche2.dll. Which is accurate, but most programmers will read the message as "cannot find" surcouche2.dll. The most common reason for the exception. Not the problem here.
Never mess with DLL Hell. The days that keeping DLLs in a separate directory and sharing them between different programs was useful are long, long gone. Always copy dependent DLLs into the same directory as the EXE that needs them. Makes it very easy on the operating system to find them. And avoids the considerable misery you'll have to deal with when it finds the wrong file.
Use Project + Add Existing Item and select the DLLs. Set their Copy Local property to True. If you don't like the clutter then use XCOPY in a post-build event.

Unable to load DLL "x.dll" build warning, but it actually loads fine when I run

I'm making a forms app in C#. I've built a custom control which makes a couple of calls to a c++ DLL I've had to put together so I can re-use some old code. When I run, it works perfectly. However, I get a build warning that says:
Unabled to load DLL 'x.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
When I open the forms in the VS designer that use that custom control, it will throw an exception and say it can't find the DLL, but if I hit "Ignore and Continue" then it seems to be fine.
I think that the problem is because the search path for dlls in VS design view is not the same than during runtime. During runtime, VS has an explicit path set for your application which include for example the /Debug or /Release folder, which is not in the global path.
So VS can't find it, but runtime can. What you could do is to add your dll path to the global windows path or find how to tweak VS search path

C# DLL run-time error - builds, runs, but throws unhandled exception upon trying to use DLL functions

I am trying to compile the example project shown here for C#. It had to be converted to VS2010, but that worked fine. It builds, runs, but then dies when it tries to access a DLL function.
I made a series of images to show my steps. As you can see, the device I designed is attached and correctly configured, but I really don't think that has anything to do with the issue. Inside the Form1.cs file, the following comment explains how to use the DLL:
/*
PLEASE NOTE
You must add a reference to the FTChipIDNet.dll in order to use this sample
To do this:
1. Click on Solution explorer tab.
2. Right click the References tree.
3. Choose Add Reference option.
4. Browse to the FTChipIDNet.dll (as a personal preference I place this in my bin directory)
5. Click OK
*/
I followed the instructions shown above and the undefined reference to the namespace FTChipID was fixed. I also manually checked the Object Browser to be sure the GetNumDevices function exists and it does.
Clicking the button produces this error:
DLLNotFoundException was unhandled:
Unable to load DLL 'FTChipID.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at this line of code:
FTChipID.ChipID.GetNumDevices(ref numDevices);
Now, you may be thinking "the error says it needs FTChipID.dll, not FTChipIDNet.dll." I'm wondering the same thing. I have FTChipID.dll along with a .lib and .h file, but I don't know how to use them or where they need to be in order for this program to find them. I tried adding a reference to FTChipID.dll, but VS2010 said
A reference to ...\FTChipID.dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.
I don't know what that means either. Any ideas? Thanks in advance.
FTChipIDNet.dll is a wrapper for the FTChipID.dll, so you need to add the reference to your solution (as you did above) and then copy the FTChipID.dll to the bin folder there FTChipIDNet.dll will be located.
Both dll's should be located in your solution's bin folder to operate and should not be located in windows\system32.
I built the FTDI sample program "CSChipID" in VS2013 using a build to "Any CPU". The two DLL files "FTChipID.dll" and "FTChipIDNet.dll" were copied to the bin directory, but I continued to get errors when the first DLL function was called, "FTChipID.ChipID.GetNumDevices(ref numDevices);" The fix is change the build to "x86" as follows.
Build - Configuration Manager
Active Solution Platform - "x86"
Note: If "x86" is not available, select -New...- to select "x86".
It sounds like there are multiple .dll's: both FTChipIDNet.dll (the one you interface to), and FTChipID.dll (the one with the actual, non-COM, non-.Net functionality). You need both.
SUGGESTION: Copy both to your \windows\system32 directory

"Unable to create component" error

I'm trying to develop a custom PDF viewer using PDFLibNet library.
I downloaded compiled dlls (32 and 64 bit) and took a look to examples: very nice.
Then I started a library project in VS2010 and created a new user control (the one I want to export): dll compiled succesfully.
So I created a GUI project (net 4.0), referenced my assembly and dropped my control to main form: I have an error that says (I'm translating, so it could be wrong) "Unable to create component 'PdfViewer'. Error message: 'System.IO:FileNotFoundException: unable to load file or assembly 'PDFLibNet, Version 1.0.6.8, .... or one of its dependencies. Unable to find specified file.".
To prevent some question:
Yes, my custom assembly references exactly that assembly
Yes, PDFLibNet assembly is exactly 1.0.6.8
In my control I tell to copy that DLL everytime
I tried to add PDFLibNet reference to GUI project, but I had the same error
Here is what I left after deleting everything (for testing purpose):
public partial class PdfViewer : UserControl
{
PDFWrapper _pdfDoc = null;
public PdfViewer()
{
InitializeComponent();
}
}
Just to tell you: if I try to remove any reference to PDFLibNet (removing PDFWrapper _pdfDoc = null;), my control is dropped correctly on form.
I even tried to register PDFLibNet into GAC, but the result is the same.
I'm really confused...
make sure you don't check debug folder and the output mode is release.

Deploying XBAP with win32 DLL

My XBAP application uses two win32 dlls and as usual many .Net dlls. Since we add .net references to the project itself, that is not a problem, as they all get deployed automatically when publishing the project; only that their names change from mydotnet.dll to mydotnet.dll.deploy.
But the problem is, the win32 dlls don't get deployed, neither their names change. And I understand the reason. It's because I call them using p/invoke techniques, and they're not added to the project. In fact, visual studio 2010 doesn't let me add them to the references. So I manually copied them to the publish-folder, and tried experimenting if that works. But it didn't work. The error the browser shows, is this:
System.DllNotFoundException: Unable to
load DLL 'Player.dll': The specified
module could not be found. (Exception
from HRESULT: 0x8007007E)
at Player.PlayerProxy.Initialize()
atRunaware.WpfBrowserApp.Player.Page1.PlayFile(Object
sender, RoutedEventArgs e)
Can anyone explain me what is going on? And any solution for that?
Previously I was trying to do what I want to do in Silverlight, but Austin suggested me to use XBAP to achieve the same thing. URL to the previous topic:
Handle to Silverlight UserControl
Can you try:
Checking your project properties under the Publish > Application Files section and if your native dlls appear there, making sure they are set to include.
Try including the dlls as content files in your project with copy to output directory set to Copy Always (I would think this may make them appear in the list of references mentioned in point 1, allowing you to set them to "Include").

Categories

Resources