Calling a DLL from C# (VS2008) - c#

I have a DLL (Test.dll) which contains some Excel Addin, i don't know which version this is build with (might be with VB6, but i am not sure).
When i tried referencing this DLL in the VS2008 i get the message
A reference to the <DLL Name> could not be added. Please make sure that the file is accrssible, and that it is a valid assembly or COM Component.
If anyone could please help me in this, it will be great.
Any help is appreciated.
Thanks and Regards,
Tushar

It seems to me the DLL you're trying to load isn't a managed DLL, or one that the CLR can treat as managed.
One solution would be to use managed C++ (C++/CLI) to build a wrapper around the DLL. Another is to use PInvoke which is explained here and there's a tool for it I came across here.
You can also look in this thread: Unable to Use DLL of VB6 Into ASP.NET.
Hope one these work out for you,
Asaf

Related

Reading resources from an un-managed dll with C#

I've written a command-line tool in C# which swaps out a resource in a .resources file for an alternative one. It uses ResourceReader and ResourceWriter.
I'd like to do the same thing for dll files, but I can't find a way of doing this. I tried using Reflection, but that only works on DLLs which are .net (managed) ones. Most of the dlls I'm using are built with other things.
I can't for the life of me figure out how to do this! Things like Resource Hacker do it, so it must be possible.
Can anyone help, please?
Thanks
A really good step-by-step guide can be found here: How to load unmanaged (native) resources from managed C# code

It there have any way to create the DLL with a header file by using visual C#?

I am doing mine FYP by using the
Visual C#
Agilent VEE
I am try to import the DLL file which created from C# into Agilent VEE but VEE required the header file and library file for the importing.
Please kindly help me.
In C#, there is no header file, this is different from C++! To use old C/C++ code, use PInvoke.
You cannot consume a c# dll in your C++ project and vice versa (normally). You should either create a com aware dll in C# to consume it in C++ or on the other hand, you need to declare all the functions in C# to use from a c++ dll.
Here I am talking about standard dlls (not activex or com all). They require a different methodology to work with.
Perhaps you can avoid the "Import Library" primitive in VEE, and not need a header file.
Use Device --> .NET Assembly References... and browse to your DLL files. Then you have to import or select a namespace. Finally, functions of the DLL appear in the function browser of VEE.
Not all DLL files can be used. I have two similar from a hardware vendor. The one named somename_net.dll works.
If you find an answer to your original question, I'd like to know. My method

System.DllNotFoundException in .NET DLL

I took a program written in C/C++ and modified it's main function to accept some arguments as input and return a variable as output and created a Win32 DLL out of it. I then created a .NET DLL which uses InterOp to access the first DLL. Now when I load the .NET DLL in my C# app I get a System.DllNotFoundException from the DLL which is really baffling me as there were never memory issues with the program and both Win32/.NET dlls are located in the same directory (apart from modifying the main function, the code has not really changed).
The solution was provided in this thread, which was my original question some time ago. I'm pretty sure that answer is correct but I'm just missing something.
You can download my VS solution Here. The solution contains three projects: the Win32 DLL, the .NET DLL, and a winform app that references the .NET DLL (but when trying to test gives the DLL exception). Any help or debugging guidance would be greatly appreciated.
UPDATE: I have tried all the tips/suggestions below but I still get the exact same error. If it makes things easier, my VS solution is available to download in the hyperlink above.
Make sure you have placed the win32 dll on /windows/system32 folder(if only the dll name is passed to DllImport)
Alternatively you can also pass the full path of the dll to the DllImport Attribute.
Use a tool such as Dependency Walker to make sure you are not missing out on any dependent assembly.

import c++ dll to windows phone project

I'm new on windows phone developement and i have one problem that i don't know how to resolve....
the problem is....
i have a c++ project that i had complided with visual c++ 2010 and this create one dll with code compiled...
so i know that C# import dll libraries but when i add refrences it's make this error "Unable to retrieve assembly fullname ""Parameter name: AssemblyPath" and i dont kown what it means...
I searched on google and i found one method to import c++ dll manualy with DllImport and calling a external method... that causes one error because it's dont find the dll location... it's happens because wp7 don't suport C++???
thanks for help me
Neither P/Invoke nor C++/CLI is supported, only managed user code. See social.msdn.
3rd party WP7 apps may only comprise of managed code. C++ is also not a supported language, even in managed form, at this time.
WP7 doesn't support c++. you would also probably need to recompile the c++ dll anyways, even if it was supported.

getting error while adding dll reference in the c# .net windows application why?

i have download the code from the codeProject web site but while runnig this i got the reference error of the dll in the reference folder. the dll file is located in my bin/debug/
folder. still it not found it. when i add this file from add reference tag i give the error
that this is not valid com component.
please help in this
thanks in advance.
If the DLL is a COM library, you will need to register it manually using regsvr32. Then you can access it by name from the COM tab of the Add Reference dialog (see below).
(source: com.com)
If, on the other hand the DLL is native code as Jon Skeet suggested, you will only be able to use it by p/invoking to call its interface directly - if this is the case, it is best to create a class that acts as a wrapper around the DLL - that puts a layer of indirection between your code and all the p/invoke stuff so your code isn't too tightly bound to the interface of the DLL.
Well, you haven't said which project it is... but my guess is that you're trying to add a reference to a native code DLL, rather than either a COM library or a .NET class library.
If you provide more details, we're far more likely to be able to help you.

Categories