Merge Win32 c++ dll with managed c++ dll - c#

I have a c++ project compiled with /clr support(e.g. wrapper.dll). This project(wrapper.dll) is a wrapper between .Net enviroment and unmaged dll(e.g. noCode.dll), which inherits from. I dont have access to code of inherited dll(noCode.dll), but I can link it to my wraper (by noCode.lib and noCode.h files).
When I want to use my wraper(wrapper.dll) in c# project I reference it and have to copy my unmanaged dll(noCode.dll) to location of c# project execution.
Is it possible to merge c++ unmamaged dll(noCode.dll) to managed dll(wrapper.dll) to allow to use only one file by reference(noCodeWrapper.dll) in c# projects?

It is not possible to link DLL files together.
But you can add that nocode.LIB to your DLL and then you don't need [DllImport]. Since you have LIB and H files, you can call the "nocode" functions directly from your managed C++ code.

If you had access to noCode.dll source code you could integrate it with wrapper.dll. Otherwise merging DLL binaries is not a task that could give predictable results.

Related

MFC managed code references

I am trying to reference a .Net DLL(call it B.dll), which basically is a wrapper for a .Net third party(call it C.dll), into c++ MFC project. I did create the tlb file for B.dll and am able to instantiate and call this within the MFC app.
At the moment all the dependencies, B.tlb, B.dll and C.dll, need to be in the bin
folder of the MFC application. What I want, and am struggling to do, is to put these three files in a sub folder of the MFC execution folder.
I tried setting the "privatePath" of the B.dll config file to a sub folder but as I understood it, it's not the B.dll "privatepath" that needs to be set but the MFC application(which obviously hasn't got any as far as I know, as it's not a .Net application)
Any help is appreciated.
You don't need to use COM (if you need it my answer is obsolet).
You can write your own C++/CLI wrapper DLL that has just exports a native interface. Than call you can this native wrapper and this wrapper again loads your .Net component directly and executes the code in it.
In this wrapper DLL you can add an ResolveEventHandler where you implement your own search (maybe in the subdirectory). Add this to your CurrentDomain->AssemblyResolve
With this trick you get around all this COM stuff, and you have full control were assemblies should be searched for that can't be loaded.
I have the solution from here

Does .net create any temporary C# files when uses manage wrappers to unmanage C++ DLL

Usually when we need to use native C++ DLL in C# program, write a manage wrapper is the way to achieve it.
Inside such program, If I press F12 (go to function definition) on a DLL function call, It goes to a C# file that looks like having all the functions forward declarations. I check the location of this file and it exist in the windows temp directory.
So my question is , When we uses a C++ unmanage DLL by manage wrapper in a C# program, Does .NET framework ( or any other component) create any temporary C# classes relevant to interface classes in the DLL?
There are no "temporary C# files". If you use Go to Definition and the IDE only has an assembly reference for the definition then it doesn't have a shot at finding the source code file for the definition. So it auto-generates one from the metadata in the reference assembly. That looks fairly similar to a source code file, but of course without any comments and without the code for the classes. It cannot be compiled. It is also in C#, even if the original assembly was created from C++/CLI or VB.NET code.
Nice feature, but don't confuse it for anything more than it is. If you want the IDE to take you to the actual source code file then you need a project reference, not an assembly reference, for the library.

A procedure imported by {dll} could not be loaded

I have several Unmanaged C++ written lib files which I need to link to Managed C++ dll.
Then I need to invoke functions of this Managed C++ from C# application.
First step is OK - Managed C++ dll is created, I can see with ildasm that it exports functions I need. However when I try to call this function from my C#-written test app it says:
An unhandled exception of type 'System.IO.FileLoadException' occurred in Unknown Module.
A procedure imported by {MyManagedCPP.dll} could not be loaded.
This message goes from VS2010.
I made simple experiment - removed dependencies from all lib files in Managed C++ dll and rebuild it.
With this change it is OK - app starts, I can call functions of Managed C++ dll from C# test app.
Is it not possible by design to call managed c++ functions when dll has static linkage with lib files? Technical restriction? Or there is some workaround?
Thanks
You no doubt have an implicit dependency on a native DLL. It isn't clear from the question what DLL that might be. It could be msvcrxx.dll for example, a runtime support library for native C++ code. Which would be rather bad, you don't want to mix CRT versions. Such a missing DLL otherwise prevents the C++/CLI assembly from getting loaded, producing the FileLoadException.
If you have no idea what that DLL might be then you could use SysInternals' ProcMon utility. The trace will show you the program searching for the DLL and not finding it. If it is msvcrxx.dll then be sure to rebuild the .lib files using the same compiler version you used to build the C++/CLI assembly. If it is something else then make sure you copy that DLL to the build directory.

create header file (.h) and .lib file in visual c#

I want to create a dll in visual c#, and use it in win32 program (visual c++).
From what I understand, for adding dll file in Visual C++ , I need also .h file and .lib file, but when I create a class library in visual c# I only get dll file.
Is it possible to create a .h file and .lib file in visual c# ?
No, it's not possible. But here is what you can do to use a C# library in C++:
C++/CLI Wrapper (this allows you to have both managed and unmanaged code in the same source file. The managed portion can then call the C# code). Here you can find an example.
Host CLR (the CLR acts as a library that can be loaded and "hosted" by a process).
COM Interop (expose your .NET type as a COM interface and matching coclass which you can easily use from unmanaged C++).
This thread is also quite interesting!
You could use LoadLibrary and GetProcAddress to load the DLL dynamically and you wouldn't need to create the files you refer to. You can read more here.

Adding C++ DLL's to a C# project

I'm trying to use the lame_enc.dll file from LAME in a C# project, but adding the thing seems impossible.
I keep getting an error that says that a reference could not be added and to please check if the is accessible, a valid assembly or COM component.
I have no C++ experience, though I would like to use the functionality. Right now I'm using Process from the .NET framework to call lame.exe and do stuff, but I'd like to know if there's another way.
You can only add managed assemblies as a reference to a managed project. What I normally do in this situation is to add it as ressource instead with "copy local" settings. That way the DLL is tied to and deployed with your project. I then use DllImport to manually get the APIs I need from that DLL.
You have to use P/Invoke to call unmanaged APIs from managed code.
To use an unmanaged dll (native C++) in C#, you have to use DllImport, not adding a reference to the project in visual studio (and that is why you get an error).
Here is the documentation of DllImport from the MSDN.
You will need to use PInvoke to call functions in your native lame dll. However, you will only be able to call functions that have been exported as "C" style.
You can use a tool like "PInvoke Interop Assistant" that will help you when working out the PInvoke call signatures to make calls from C# to your native dll:
http://clrinterop.codeplex.com/releases/view/14120

Categories

Resources