Debugging assembly load issue in C# - c#

I created two projects. They are VB.Net dll and c# .net dll file. I am using VB.net dll file into c#.dll file. when i test c#.net dll file it is giving error message Could not load assemble. if i use both then it is working fine.But i have to use only one dll file.
Let me know how can i do with only c#.net dll file.

In .NET a project denotes the bounds of the resulting assembly. So if you want a single .dll you will need to put everything in a single project.
You have the additional task in your case of making everything the same language as you can't mix VB.NET and C#.NET in a single project.

Related

.NET cannot locate unmanaged DLL in same directory

I have a C++ DLL library (let's call it unmanaged.dll) that gets wrapped around a managed .NET library (let's call it managed.dll). The managed.dll uses unmanaged.lib to import/link unmanaged.dll. I have another ASP.NET Web API project that uses managed.dll (let's call the project webapi.dll). Now, whenever I build the Web project, in the output directory I get webapi.dll and managed.dll. Then, I manually copy unmanaged.dll to be in the same output folder so that (presumably) .NET can locate it and load it.
However, when I try to run webapi.dll, it fails with an error that it cannot load managed.dll:
Could not load file or assembly 'managed.DLL' or one of its dependencies. The specified module could not be found.
This is particularly weird because unmanaged.dll is in the same folder.
What I find very weird is that if I put unmanaged.dll under C:\Windows\system32 then .NET can properly load it!
My question is how can I make this unmanaged.dll visible to .NET in the folder where the .NET project output is residing?
It's the IIS that is causing the problems. Had the same problem, sovled it by adding the dll to a folder, that lies in the PATH variable.

Loading c++ dll in Asp.net Web App using winmd file

I have a c++ dll, and an idl file that was used to generate a .winmd file.
I created a C# application that consumes the .winmd file and I can successfully create an object for a class that was defined in the dll. i.e the program compiles and runs without any issues.
However, when I try and use the same exact winmd file (which works when consumed by a C# application) for a C# Asp.net web app (i.e I add a reference to the winmd file and I see that the winmd file and dll are copied into my web apps bin folder), I get a runtime exception saying:
"Could not find Windows Runtime type [Name of class]"
When I use Fusion Logs to see what went wrong when the application was attempting to load the dll, I see this:
END : Typename or Namespace was not found in metadata file. (Exception from HRESULT: 0x8000000F)
So my question is, what is the main difference between loading a native dll in a C# application vs an Asp.Net application?

how to create a dll file including a method in c#

Imagine I have different methods written in c#. And these are included in the main class now and work completely fine. But I have a plan to add an upadter function to this project. In this case my idea is to include each function in a DLL file with the function name. So my updater function can easily replace the old DLL file with the new if there's a new version available. The problem is I don't know how to create a DLL file by just including a method.
You shouldn't make a seperate .dll for each function. When writing the library I suggest adding a new project to the current solution of the type C# class library. Now you can easily use your newly written library inside the project you are using it for. Like this:
in the solution explorer you see a library project and a windows forms application project. The windows forms application uses the library's code to work. You can use the library when it's finished and compiled to a .dll by referencing it in the other project(s) under 'add reference'

calling function which is encrypted on dll file and that file is added as a reference in my sloution

I have made one common libarary using c# (.dll file is created) that is having some common functions .
Now I have one solution which is having so many projects made on vb and c++ .
Now here I added that above dll file in this solution (by adding refrence).
and I want to access those common function from dll to this all projects .
Is this possible??
IF YES THEN HOW??
For each C++ and VB.NET project, right-click on References and add the C# project as a reference.
Then access your c# classes & methods as you would access thus from the .NET "built-in" libraries.
If it's not a project within your solution, then for each project, browse to the dll and add it that way.

Adding a "winmd" reference vs Adding a project reference

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).

Categories

Resources