I have a Windows Runtime Component that uses some native libraries (the PJSIP library). This component is used in a Windows Phone 8 Application and when I try to invoke a function from the native component I get the following error:
An exception of type 'System.IO.FileNotFoundException' occurred in TestingApp.DLL but was not handled in user code
with the Source (at Exception Details) in mscorlib.
Can anyone point out what may be causing this error and how can I find out what is missing actually? When I try to put a breakpoint in my Windows Runtime Component, at runtime, the breakpoint appears as disabled.
Regards,
Tamas
Add the missing dll to the project (like adding a new file) and mark it as content. Then reference the dll from this location. This may resolve your problem.
I guess it is not working as it is not being deployed within your application.
In Visual studio, under the debug menu select 'Exceptions' then in the dialogue that appears enable 'Thrown' for 'Common language runtime exceptions'
Then re-run your application, Visual Studio should break now at the line in the code that is causing the Null Exception.
If you reference a native library, double-check if you also reference C++ Runtime. Under your project in Solution Explorer, right-click References > Add Reference... Go to Extensions and check Visual C++ Runtime Package.
Check the library file you linked, it should be a import library for DLL file.
An import library (.lib) file contains information the linker needs to resolve external references to exported DLL functions, so the system can locate the specified DLL and exported DLL functions at run time.
When app start it find DLL files according to the import library, but there is no such DLL files, so C# throw a FileNotFoundException. Make sure link to a static library
Related
I have .NET 5 project which includes C++/CLI DLL, which includes C++ DLL, which includes opencascade DLLs. All these projects are built for the x64 platform. I've published this solution to my local folder and then set up IIS server everything works fine. But when I've tried to upload my project to two different hostings (https://www.smarterasp.net/ and azure) I've got this error. Project crashes when it starts use C++/CLI dll(using namespace C++/CLI). I've also set up x64 platform in azure portal.
I would appreciate any help.
Thank you Joe. Posting your suggestion as an answer to help other community members.
Project crashes because of missing DLL. Run the app and get the error.
Go to the Windows Event Viewer (Start Menu and type "Event"). In the EventViewer, under Windows Logs >> Application, you might see an error message regarding the failure. If the error is a missing DLL, it will typically tell you the name of the DLL as well.
You can refer to Could not load file or assembly ... An attempt was made to load a program with an incorrect format (System.BadImageFormatException), Troubleshooting BadImageFormatException and System.BadImageFormatException An attempt was made to load a program with an incorrect format
I have a Windows Phone 8 project and another project written in C++ ; both are in the same solution. The C++ project is a dynamic library used in the WP8 project, and it is configured to produce a Windows Metada file (.winmd) on top of the .dll file.
When adding the C++ project as a Project Reference in the WP8 project, everything works perfectly well.
However, I'd like to reference directly the binaries instead of the project so I tried referencing the .dll itself but VS2012 would not let me (which I totally understand since the library is unmanaged from what I understand). Adding the .winmd file instead works, I mean it compiles without warning/errors ; but it crashes at runtime (I get a TargetInvocationException which is raised because the "actual" code of the C++ library cannot be found).
When adding the .winmd file, I made sure the .dll file was next to it. Putting both the files in the bin directory of the WP8 project does not work either.
I can't find any clues on the internet and I'd be grateful if you could give me some, any hints are welcome!
Here is a schema of the trivial architecture I'm trying to set up:
And here is the stacktrace of the exception raised:
at System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)
at Sqlite.Sqlite3.sqlite3_open_v2(String filename, Database& db, Int32 flags, String zVfs)
at SQLite.SQLite3.Open(String filename, Database& db, Int32 flags, IntPtr zVfs)
at SQLite.SQLiteConnection..ctor(String databasePath, SQLiteOpenFlags openFlags, Boolean storeDateTimeAsTicks)
at SQLite.SQLiteConnection..ctor(String databasePath, Boolean storeDateTimeAsTicks)
at WP8ClassLibrary.SomeManager..ctor(String databasePath)
at WP8App.SomeViewModel..ctor()
at WP8App.MainPage..ctor()
Tools + Options, Projects and Solutions, Build and Run. Change the "MSBuild project build output verbosity" setting to Normal.
Pay attention to the build output, the messages you see after "XapPackager". Which show which files are getting added to the Xap package. Your DLL needs to be in that list. If it is not then your program will fail as described. In which case you'll need to find out why it is getting skipped. Start that by checking that the Copy Local property of the .winmd reference is True.
I just wanted to add my experience to the answer..
If you are trying to add a C++ library directly to C# code (for a Windows Phone 8.1 app in my case), then including the .winmd file enables the compilation but the app crashes on launch. The stack trace only says failed to load C++ dll.
I had to also add reference to Visual Studio C++ runtime library in the C# application. I found out about the missing reference by diffing the working .xap (created from a solution that includes both C# and C++ projects) and non working .xap (created from a solution that only includes C# project along with reference to C++ .winmd file)
Well I managed to make it work. As the stacktrace in the original post would suggest, I am using SQLite in my application. If I add the winmd file as a reference instead of the C++ project, it looks like some dependencies are not satisfied.
Therefore I had to add a reference to SQLite for Windows Phone into the WP8ClassLibrary project. That was somehow a dumb mistake of mine because this C++ project probably carried this dependency with itself (there are several winmd references in it but I could not guess they would be part of the SQLite for Windows Phone based on their obscure names).
I have a existing Java Project which needs functionality from a SDK written in C#. It should open a WPF Window and send the information back to Java on close.
For a basic connection of those two worlds i created a Java Project ("DotNetCaller") calling native functions. These are implemented in a C++/CLI Project ("DotNetBridge") which calls the C# Project ("DotNetApplication").
I already can set Strings from Java in C# and callback from C# to Java.
But as soon as i add a WPF Window and try to launch it with:
Application app = new Application();
app.Run(new DotNetWindow());
in a STA Thread it crashes.
The DotNetApplication doesnt find mscorlib.resources, after i provide the DLL, PresentationFramework.resources is missing and if i provide that, the DotNetApplication.resource is missing (which i cant provide).
If i call the DotNetApplication alone or from the DotNetBridge the Window displays as expected.
Can anyone tell ma what i'm really missing here?
Thanks
Edit:
I looked at this example once more and tried to adapt it to my needs.
I have set the dll directory of the ResolveEventHandler to the .NET dir in "Referenced Assemblies"
C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework.NETFramework\v4.0
and added a Window in C#.
It failed aswell but with a new exception in the C++ part rather than C#.
The ResolveHandler gets called with an empty argument causing an uncatchable exception in mscorelib.
I added a check if the String is empty and this basic approach works fine now.
I'm still unsure if i have the correct approach for this, so feel free to contribute.
Your AppDomain::AssemblyResolve handler probably needs to be overhauled and based on your own understanding of what you want to do. There is some guidance here. The basic rule is that you return nullptr for requests that you can't handle.
But first you have to plan the locations in which you want to deploy (and/or debug) your assemblies. A simple layout would be to put all of the assemblies that your JNI DLL depends on in the same folder as the JNI DLL (with the exception of any that will be installed in the GAC). You can then use its location to satisfy resolution requests. But remember to return nullptr if no file containing a manifest for an assembly with the requested name is present there. (This is likely the case with your ".resources" requests. If there isn't one it's okay unless you know otherwise.)
I'd be a little surprised if an assembly in a Reference Assemblies folder wasn't also in the GAC—but it'd be up to the assembly provider. Reference Assemblies is for design and build tools (e.g. Visual Studio). (The old way was for each folder that had assemblies in it to be registered for each version of Visual Studio so the assemblies could be used for design and build.) If a dependency is not in the GAC, you can use the "Copy Local" property on the reference to make it available for debugging.
You might find the Assembly Binding Log Viewer useful while designing and troubleshooting. With it you can see all the folders and extensions that are tried before giving over to calling the AppDomain::AssemblyResolve handler chain. (Disable logging when you are done.)
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
I am developing on a Windows 7 64bit machine. I have a solution in Visual Studio using C# and am trying to add the solution for ImageMagick I found here to my project.
I managed to add the solution to my project, checked the linker and c/c++ general settings as well as the common properties and all appears to be in order. I also added a reference in my project to ImageMagickNET8 (the name of the ImageMagick project).
When I try to make a call to the ImageMagick project:
ImageMagickNET.Image img = new ImageMagickNET.Image("c:\picture.jpg");
...the following exception is thrown:
BadImageFormatException was unhandled
Could not load file or assembly 'ImageMagickNET8, Version=1.0.4189.18742, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
I assume I am forgetting something fundamental about adding a project to my existing project.
It's likely that ImageMagick is set to compile to x86 only, instead of Any CPU. Try changing your project to be x86. Project + Properties, Build tab, Platform target setting.