How to link a .DLL statically? - c++

We have a (pure native C++) .DLL that is build by VS. As clients we have some native C++ applications and a .Net-Wrapper around this DLL written in C++/CLI. Finally there are some client applications for the .Net-Wrapper written in C#.
My problem is that the native.dll must be distributed in a different way than the .Net world works and the VS does not keeps track of that DLL.
So to let all my C# Apps work correctly I have to copy it to each executable directory or put it somwhere in %PATH% (which I would avoid on developer computers since they may want to start different apps with different versions of the DLL).
Even bigger problems occur if there are UserControls that reference the Wrapper-DLL: You have to copy the DLL to VS's directory or again to %PATH%.
But the worst case occurs with our Translator tool. This tool keeps track of .Net-Assemblies and packs them into Translator-packages that can be send to an external translator. As far as I know there is no way to put the native .DLL into that package!
So I plan to link the native DLL statically into the .Net-Wrapper which would solve my problems.
But for our Native applications this native DLL must still be a DLL.
So I have two options:
Make two projects of that (one that generates a static library; and one that creates a dynamic one => I try to avoid this)
Find a solution to link DLLs statically
Find a way to let VS generate two outputs from one project

In the C++ project file for the dll, create two configurations, one that generates a DLL and one that generates a .lib. Two projects are not necessary, since any .NET/C++ project can support multiple build configurations (this is how Release and Debug versions build differently).

Another option is to have two projects, one project will output a .lib which can be statically linked, and a second project which will output a .dll and will have your .lib as dependency, you should add .def to your .dll with the symbols that you are planning to export, or else it will be empty.

Pick up a copy of DLL to Lib (Edit: If you can't find a cheaper option)

You can generate a dll and export the entry point to a lib using dllexport, this is explained here
http://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx

Related

Running Allegro 5 on other computers

I have made an allegro simple game . But when I open the *.exe file on another computer it says that there are many missing .dll files . How can I make my game to run on other computers without Visual Studio and Allegro 5 library installed ?
Longer version of my comment:
When you created your application, it links to certain DLL's that exist on your computer. When you distribute your game, you will either need to ZIP the DLL's along with your .exe or package them using package creators and ship it.
The best way to find which DLL's your exe depends on will be to use a tool like Dependency Walker. You don't need to copy absolutely all DLL's that your EXE depends on. Only the ones that you see are in non-standard paths like ones that are not in C:\Windows\System32. That being said, you might need to copy some from C:\Windows\System32. You will need to find that out on your own.
To package them all as a setup, you can use package creators like InnoSetup or NSIS. Otherwise, create a script that ZIPs it all up for you. AFAIK, there is no easy way to get all DLL's required that are missing from the other persons' system. You'll need to find them out by trial and error. It is a pain, unfortunately.
If you downloaded the pre-built binaries, link against the static, monolithic, mt build of Allegro. You'll need to adjust your compiler settings to match (/MT) and add ALLEGRO_STATICLINK to your list of preprocessor definitions.
If you do that, then you only need to distribute your executable file and your resources (images, sounds, etc).
Note that you should have at least two configurations: Debug & Release. When working on your application, you should use the Debug configuration (linking against the regular debug Allegro library). When distributing your application, you should use the Release configuration.

Confused with the DLL. No .lib or .h

I currently have a C# project that uses a 32 bit dll. What I am trying to do is to use the dll in a C++ project , unfortunately the dll referenced in the project does not come with a .h or .lib file so I am confused on how I could access the classes in the dll or use the dll file in my project in C++ (Visual Studio 2010) . I read here that if a dll file contains classes its a good chance it might be a COM component.Any suggestions on what I should be looking at in order to integrate and consume methods and objects in that dll.
Update:
I checked if it was a COM by doing an import statement. However I get a "cannot find .tlb file". Then I decided to use dependency walker to check for any exported methods (objects)it seems there are none. I clicked on the main file (which has been highlighted) and couldn't see any exported functions. Any hints on what I should check for next ?. Here is what I get from dependency walker.
With only a .dll and without a corresponding .h or .exp, one place that you might try looking would be the exports table. Tools such as Dependency Walker can show you this information.
One caveat with Dependency Walker is that you'll want to use the right version (x86 or x86_64) for the binary (your DLL). If you're not sure, pick one version and try loading it. Under the CPU column, middle panel, look to see if it says "x64" or "x86" to determine which one you should be using.
With the provided screenshot of Dependency Walker (indicating a dependency on MSCOREE.DLL), I agree with Eugene - this is most likely a .NET library, not a COM library. You will want to check one of the other questions about calling .NET managed code from C/C++.
The dll is most likely a .Net assembly (seeing that it imports mscoree.dll), so you probably want this: How do I call a .NET assembly from C/C++?

Creating a .dll out of existing code

This is a newbie request. I'm looking for materials on .dll creation. Specifially, i want to create a .dll out of a simple 3D engine i've made to learn about the process. I need information on dynamic link libraries that go deeper than the wikipedia page, how are they created, what's necessary, how to create different .dll files for "debug" and "release", how to create a PDB file for it and how to create a header file that'll allow for easy usage of the library from a, f.e., C++ program. Material with strong theoretical side (not as much, "how to create a dynamic link library in visual studio") would be great.
Please share good materials on the subject, all i can find is some information here and there and it doesn't paint the picture for me.
Reading between the lines, I think you really want to know about libraries in general rather than dll's specifically. A library is simply a handy package of object (compiled) code, along with some information about how to call into it. In C++, this usually takes the form of a .h file.
With static libraries (.lib), the linker pulls in the code it needs in exactly the same way as it does with all the rest of your classes. A normal class will get compiled to object code (MyClass.obj), and when they're all done the linker sticks them all together and wires up any inter-object calls with the appropriate addresses. It's the identical process with .lib library files. You end up with a big ball of executable code which includes both your classes, and the library functions within it.
With a dynamic library (.dll), the only difference is that the linking (wiring) happens at runtime instead of at compile time, and the library object code remains in a separate ball - the dll file. When you compile your exe, all calls that use functions in the library are mapped to a stub function. When Windows loads the dll for you, it will stick the dll code into the same memory area as your process, and wire up the stub functions to the real functions that are now available.
The only other difference is that a dll must expose a function that Windows can call when it loads or unloads the dll, in case the dll wants to do any initial setting up / clearing down. This is traditionally called DllMain().
If you want to learn about libraries, I would concentrate on creating a static .lib first before worrying about dll's. This is where most of the work is. Once you have a lib it is child's play to turn it into a dll.
The main thing you need to think about when creating a library is how you are going to expose your API (Application Programming Interface). This is just which functions/classes you are going to expose to the people using your library. You don't have to expose them all, but you do have to decide WHAT to expose. Are you just going to expose some C style functions, or are you going to expose entire objects? This is the real challenge when designing a library. You should try and make your API as easy to use, and obvious as possible if people (and you!) are going to find your library useful.
As for pdb files, differently named release/debug modules, and creating .h files. These are identical to when doing so in an exe.
1) Create a new DLL project, using VS wizard.
2) Add your existing source files to it.
3) Put into *.def file (which should have been created by the wizard) the names of the functions that you want to export from your DLL, as described here.
Basically, that's it. Debug and release configurations are automatically created by the wizard, but if you want the 2 variants to be named differently, you can change their output names: go to Project Properities -> Configuration Properties -> General -> Target name.

Use two different versions of a .lib file in one C++ project?

I'm working on a Volume Shadow Copy program. I have just found out that I need to use a different set of .h files (vsbackup.h) and associated .lib files (vssapi.lib) to compile the project for Windows XP (in comparison to Vista and 7). This is due to a series of subtle changes in the .h files: in XP, a call such as CreateVssBackupComponents() was a call into the API (there was only the declaration in vsbackup.h); the more recent vsbackup.h changes this into CreateVssBackupComponents() { CreateVssBackupComponentsInternal(); } and when I compile my project with the newer .h and .lib files and run the program under XP, it says
The procedure entry point "CreateVssBackupComponentsInternal" was not found in DLL "vssapi.dll".
Is there any possibility to incorporate these two different lib files in one project, so that I don't have to compile two different versions of the program?
If you need to work with two different versions of the dll (because if I understood correctly the problem lies there, the different lib/headers are just the tip of the iceberg) you should dynamically load it with LoadLibrary, check the presence of the entrypoints you need with GetProcAddress and call the functions you need with the function pointer that it returned.
You can also create two project configurations one for XP and one for newer Windows versions. Then add both files to your installer and let the installer check which Windows version it is installing at and select the right version of your program.
(That's basically what I did when I had your problem)
The more proper solution though would be putting it in a DLL, create two versions of that DLL and then runtime check and load the right DLL. That way a Windows update wouldn't cause problems.
Personally, when libraries start to get a bit /crazy/, I start looking into giving the problematic libraries their own process, and use some sort of RPC to interact with them.

C++, Qt - How do I get rid of dll dependencies?

I have compiled my Qt application and now have the following question - now my built project requires QtCore4.dll and QtGui4.dll to be located at the same folder where the .exe file is. (I built my project using MSVS2008 with Qt addon)
Q:
Is there any way to combine my final application with these .dll files so that they make one large .exe-file? (I simply don't want to have another bunch of dll files with my release - app)
Thank you.
You need to build and link to Qt statically.
Edit: Here's an updated link to at least similar information.
Bundle them into a self-extracting .exe (e.g. using 7zip) which extracts all files to a temporary directory, runs the program, then deletes the files after the program exits.
This will be easier, less time consuming and less legally constraining than statically linking Qt as previously suggested.
Of course you could statically link someway. But the point of using DLL should be to make program smaller (both on disk and in memory, if other apps are using Qt libs of course)... DLL such as those should be systemwide so that other apps needing them can use them. Basically you have to say to people wanting your program to work, to install the Qt framework.
Deploying the other way is explained here, read the part related to Static Linking.