Using lib in dll for c# - c#

I have some problems with dll. When I make dll without using of lib files (which I need), everything is fine. But when I'm trying to use some functions in dll that uses functions in lib then some exception appears:
System.DllNotFoundException, cant load a dll module (exception from HRESULT:0x8007007E).
dll file is in correct place.
P.S. using Visual Studio 2010.
What could be wrong?

More detail in the question will get you better answers. But with current information, the most likely cause is that the lib file you are referencing or one of its dependencies is unavailable. This could be because it is not in your GAC or your runtime location, a file format conflict, or a number of other things.
I'm assuming that everything compiles without errors, of course. Again, please add detail if this is not what you need.

if you are making a new class you should reference the default dlls.
in example when you make a new class library and want to use a messagebox in your code ,
you should first reference that required dll in your program(i dont mean your dll,i mean dot net default dll like system.windows.forms) and then add using something; in the top of your class.
example : we want use messagebox in a class library then :
1. first from solution explorer right click the project > add reference ,now reference manager opens now from left tabs click assemblies then framework and then find and select system.windows.forms
2. now its time to use it in our program first add this line in very top of your class file
using System.Windows.Forms; //add this line in top of your class
after that we can use messagebox without any compiler errors.
keep in mind any other dll files should be referenced this way but in windows form applications default lib files are referenced by default

Related

Referencing a Windows Phone Runtime Component DLL/WinMD from another project

I need to implement a wrapper to expose some native C++ code to C#, and have followed this tutorial:
http://www.silverlightshow.net/items/Windows-Phone-8-Native-Code-Support.aspx
So far in my C# test project, I don't have problems instantiating a class written in C++/CX from the Runtime Component project and using methods from that class, so long as I reference the entire project (.sln).
Visual Studio doesn't allow me to reference the Runtime Component DLL alone, but does allow me to reference the .winmd file in the project. C# then recognizes the namespace correctly, however at runtime I get a TypeLoadException when trying to create the same object.
This doesn't appear to be a namespace problem (as mentioned here: Changing namespace name of C++ component in Windows Phone causes exception), since everything is alright so long as I create a project reference (or does referencing a project vs a .winmd affect the namespace somehow?).
Is it possible to bundle the Runtime Component in some form that an end user can reference it without needing to provide the entire project?
You need to add following the to WMAppManifest.xml
<ActivatableClasses>
<InProcessServer>
<Path>YourComponent.dll</Path>
<ActivatableClass ThreadingModel="both" ActivatableClassId="YourComponentNamespace.YourComponent"/>
</InProcessServer>
</ActivatableClasses>
With YourComponent being the name of your WinMD.
I think what you are seeing is a manifestation of the problem described here.
In short, when creating a WinRT component using C++, just referencing the output DLL or the output winmd is not sufficient. You need both.
I had this same problem, and (eventually) figured out that the .dll and .winmd file needed to have the same name (which was the same as the namespace they defined) and be in the same directory.
For example, if your classes are in the X::Y namespace, the files must be X.Y.dll and X.Y.winmd.
Then all I needed to do was add a reference to the .winmd file in my project (by right-clicking on the References folder for that project in the Solution Explorer, choosing "Add Reference...", then choosing "Browse" from the dialog that comes up). I didn't need to add anything to the manifest file.

Creating DLL files in C#

When creating DLL files for a program that already exists, is it customary to create them by going to
File >
New >
Project >
Class Library,
File >
Add >
New Project >
Class Library,
or
File >
Add >
Existing Project >
Class Library?
You should differentiate 2 things:
DLLs - compiled code in machine (in case of .net, clr) readable format for execution
Code - source code files that get compiled into DLLs.
If you have a dll that you can use, you add it as a reference to your project.
If you have source codes, you can add them the way you specified (add existing project). If you want to write new .dll, you should use "Create new class library". Note that whenever you add through "Add existing project", project (along with source codes) isn't copied to your solution folder.
Basics you should know before you go on:
Solution - means to tie several projects together into one logical bunch.
Project - means to tie several source code files/resources/etc. into one logical bunch that gets compiled into one physical unit - dll/exe/etc.
Source file - code file like MyClass.cs. This is where the code is written.
Reference - reference from one project to another one in order to obtain/use public-visible functionality.
I'd recommend reading some books on C#/.NET to get clearer understanding.
You are right, that is one way of creating dll,(for a class library in visual studio, you will get a dll) but its lot more than that.
You should gather some knowledge for DLL check out this link
http://msdn.microsoft.com/en-us/library/1ez7dh12.aspx
Also check out this link how to create dll in c#
http://msdn.microsoft.com/en-us/library/3707x96z(v=vs.80).aspx
Well what you have mentioned in your question is a good and simple way of creating DLL with C# (that is with Class Library project) , further more you can direct your Compiler (csc.exe , it the Csharp compiler) with some commands to make a DLL for you,
Consider that you have few classes such as Add.cs (can Add numbers) Mult.cs (can multiply)
To build the file MathLibrary.DLL, you can use command like this
csc /target:library /out:MathLibrary.DLL Add.cs Mult.cs
The /target:library compiler option tells the compiler to output a DLL instead of an EXE file.
The /out compiler option followed by a file name is used to specify the DLL file name.
P.S: Solution derived from How to: Create and Use C# DLLs
Referencing Custom Made DLL in C# Projects:
Add a reference of the DLL
Add namespace in you project (or just start using DLL by fully qualified name)
Snapshots

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

C# Class Library method summaries not showing in intellisense of vb.net project

(VS 2008)
I'm using a C# library for my VB.NET project. And the method summary/notes or what they are called do not show in intellisense. Is this supposed to be like that? Or is there something I must do to fix it? And if not, will VS 2010 be able to do this?
EDIT: Still unresolved. Now building library dll + xml file, but how to import the xml file in my vb project?
See image: http://i52.tinypic.com/25kh5xw.png
In C# library, go to the properties on the build tab, and check the checkbox for including XML documentation and specify the name and path. After that include the new library in your VB.Net project.
one reason could be resolved by importing the namespace needed
another reason could be due to faulty writing
if you send the code where the problem is we might be able to help you
If you're using source control (TFS or Github) then you need to the following:
Check in ( Push) for safe return point (base line)
Delete the references from the project.
Delete the custom dll's from the solution.
At this point the libs are marked as [removed], if you would add them again at this point, they will just be marked as [changed] again. This did not include the summaries for me.
Check in (push). dll's are now removed from source control as well.
Drag and drop the dll's (including xml) into the solution (I recommend using this method for including dll's
Add references to these dll's via Browse.
Check if you have summaries.
Check in (push).

My C# and DLL Data Woes

I'm trying to see if it is possible to pull data from a DLL. I did some research and found that you can store application resources within a DLL. What I couldn't find, was the information to tell me how to do that. There is a MS article that explains how to access resources within a satellite DLL, but I honestly don't know if that is what I'm looking for. http://msdn.microsoft.com/en-us/library/ms165653.aspx I did try some of the codes involved, but there are some "FileNotFoundExceptions" going on.
The rest of the DLL information is showing up: classes, objects, etc. I just added the DLL as a resource in my Visual Studio Project and added it with "using". I just don't know how to get at the meat of it, if it is possible.
If dlls are .net, you can use reflection.
Using System.Reflection;
....
Assembly A= Assembly.LoadFrom(YouDllFileName);
string[] STA;
STA= A.GetManifestResourceNames();
// STA contains all the resource names in the dll
...
// to extract them
Stream str= A.GetManifestResourceStream(STA[I]);
// then, you can make that stream became a file if you wish
You can also extract a .net assembly resources by using ildasm
I'm not totally sure what you might be running into based on your description, but a couple of general pointers...
If what you are trying to find is files you've added to the project you do this:
Right click on the resource in solution explorer, hit properties and set the "Build Action" to "Embedded Resource".
For strings and icons, add a .resx file to the project and insert them in there. If that's what you're doing and still running into issues, check the Build Action.
There is two types of dll.
Managed dll - dll that writen on any .net language (like csharp)
The link that you provide is working with managed dlls.
Unmanaged dll - classic c/cpp dll.
in this case you need to link between managed (your code) and unmanaged.
To find what the type of your dll, you need to add this dll as reference.
In visual studio open project, right click on references(in Solution Explorer).
Then "add reference"->browse-> add your dll.
Then at references, you can see your dll.
Right click on him, and add view in Object Browse.
If you see something like class "c" inside namespace "b", you working with managed dll.
In Object Browser you can learn a lot about your dll (maybe this is more important, than just extract resources)
At this point you can do the way that "Daniel Dolz" answer to you.
Since you say you are able to add the DLL in a using directive you can probably use the methods that the DLL exposes. If you do not have the documentation for the DLL then you may just have to try using the object browser to see what it has to offer.
assume:
using MyDll;
you should them be able to call the methods like this:
string x = MyDll.SomeType.GetValue();
is that what you were asking?

Categories

Resources