Use C++ dll in C#(WPF) - c#

I had one C++ project written in MFC and some of the project are of DLL type. I wanted to use those dll in my C# code(using DllImport).
I tried to add as a reference but could not able to and getting an error. Do i need to copy those dll's in any of particular location? How my code is going to link those dll's?

You can't add a reference to a native DLL. In DllImport you just import the DLL at runtime so be careful with the path to the DLL if it is not in the same directory as your managed application.
A problem with C++-DLLs is, that the function names in that DLLs can be decorated so you have to find out that decorated name before using it.

Related

Is it possible to use LoadLibrary from a dll in Borland C++ from a given path?

I have a C++ dll which loads a C++ CLI library using LoadLibrary function as follows:
HMODULE myDLL = LoadLibrary("DLLtoBeLoaded.dll");
and this works.
But I want my "DLLtoBeLoaded.dll" in a different directory(Different from the executable directory). So I tried:
HMODULE myDLL = LoadLibrary("C:\\DLLtoBeLoaded.dll");
This does not work. Although myDLL is not null after this but DLLtoBeLoaded.dll's constructor is not called.
EDIT
MyDLL is not null because LoadLibrary is actually successful. But the reason why it did not seem to work was that the DLLtoBeLoaded.dll references some dlls which are also not in executable directory(The program works if I just copy referenced dlls in executable folder). Is it possible to keep the referenced dlls in different folder from executable?
It is possible to reference dlls from a different folder than the executable.
Whenever the runtime fails to load the referenced assemblies, it fires Assembly Resolver event.
I got the basic idea from this link.
There are other methods too (probing and code base). But I liked this one the most.
You can use SetDllDirectory or simply SetCurrentDirectory before first LoadLibrary to set DLL directory (to C:\ in your example).

How to bundle a required DLL with a solution in Visual C# (Express, 2010)

I have an application written in C# which interfaces with some custom hardware using a vendor supplied .Net assembly. The .Net assembly in turn loads another DLL at run time. I can add the vendor supplied assembly to my project but when I run it, the vendor assembly complains that it can't load the required DLL. I can work around this for the moment by copying the DLL to the bin\Debug and bin\Release folder.
The problem is that I will need to distribute this application to clients at some point and they will not have this DLL in place. I can't see how I can make the solution require it; I can't add it as a reference since I get the error "A reference to foo.dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component."
I can't convert the DLL to a .Net assembly using TlbExp, it gives an error "The module was expected to contain an assembly manifest."
I thought if I published the application via "click once" that I could declare the dependency there but I don't see any way for this either. I also tried adding it as a file resource but this didn't seem to help.
I can see other questions on SO relating to how to invoke functionality in an external DLL but in this case, I just need a way to bundle the DLL with the project.
Thanks.
Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL)
The DllImportAttribute attribute provides the information needed to call a function exported from an unmanaged DLL. As a minimum requirement, you must supply the name of the DLL containing the entry point.
For further reference go here
Link to Review
You could add the dll as a resource, write it out as a byte[] to a file on loading, and do an Assembly.Load() at runtime for deployment.
You might have to use an AppDomain.AssemblyResolve Event to make sure the assembly resolves in case of multiple versions.
you could add both (all) of the dlls in your project as references and mark them as "copy local". That should do it unless the first DLL expects the second DLL in a specific place.

DLLNotFoundException The Specified module could not be found

I make use of the Belgium Identity Card SDK for reading data from a idcard.
The SDK exists of 2 components: interface dll and a wrapper dll.
In VS2010, i can make a reference to the interface dll, but not to the wrapper dll, so I put it manually in the bin folder. When I migrate my application to another pc on the localhost, it is not able to find the wrapper dll.
Not even when I (on the 2nd pc):
-installed the sdk.
-put the wrapper dll into the bin folder and system32 folder
In visual studio, properties of the interface dll, I've set "Copy Local" to true.
What can I do?
This could just be a difference in path names between machines.
I would create a folder at the top level of your solution and place these DLLs in there. Call it something obvious like "Solution dependencies". Then you can reference them as needed and set them copy to local as required. You wont always be able to reference a DLL, especially if it isn't .NET compatible.
I'm curious about your statement of interface and wrapper dlls. Is the wrapper dll not meant to be a .NET wrapper for a C++ style dll?

Convert VB6 .dll file to a .net dll

I have a .dll file written in VB6. I understand that I can add this .dll file as a reference in my VS project, but I need to be able to add this .dll file as an embedded resource to my VS solution. I then would like to invoke this .dll file to call a function.
However I cannot load the .dll file as an assembly because it was written in VB6. Assuming I cannot simply rewrite this code to .net, does anyone have a working solution for converting this dll to a recognizable assembly file?
You won't need to convert it, you just need to reference it as an unmanaged DLL. See http://msdn.microsoft.com/en-us/magazine/cc301501.aspx for instructions on how to do this.

Using managed C++ dll from C#

I've created a dll using managed C++. Now I'm trying to use it from C#. I've added the Object to project references. Object browser shows the object in the dll correcly, path to the dll in object browser corresponds to the actual path.
However, when I run the C# program it complains:
Unhandled Exception: System.IO.FileNotFoundException: The specified module could
not be found. (Exception from HRESULT: 0x8007007E)
Any idea what else have to be done?
Thanks.
I think that you're missing the other assemblies or dll's references by your managed C++ assembly.
Does your managed C++ assembly have an other dependencies, including unmanaged dlls? You'll see this error at runtime if your referenced assembly fails to load a dependency.
Are you running the application in release on a machine without VS installed?
I only ask because I ran into a similar problem here: Mixed Mode Library and CRT Dependencies - HELP
if you scroll down to my answer you can see what I did that helped me.
Check that the c++ assembly is present in the same folder as your c# program. It should be copied over automatically if the 'Copy Local' property is set to true (on the reference to the c++ dll in your c# app).
If the c++ dll is there, the most likely problem is that the c++ dll depends on another non-managed dll which cannot be found (i.e. c# will not copy these to your app folder because it does not know about unmanaged references). You can use DependencyWalker on the c++ dll to check for missing dependencies.
Another probable couse could be a problem with your MSVC runtime dlls. see if DependencyWalker complains about missing MSVCR*.dll, MSVCP*.dll etc... files.

Categories