How to control C++ DLL exported functions "easily" - c++

Say I have a bunch of functions exported in a C++ DLL.
A customer asks for some code implementation which uses only some of the functions in the DLL.
I'd like to give the customer a DLL with the relevant functions only (because full DLL is too big, I dont want to send code he doesnt need to see, etc..).
Is there any way to export only part of the functions easily (e.g. without adding/removing "__declspec dllexport" declarations everytime)?
Perhaps "hide" function from full DLL or re-compile slimmer DLL (but control which functions included in a clean way).
Thanks!

Related

Is it possible to nest dll references to avoid requiring access to the original uncompiled code?

I'm trying to import C++ functions from an instrument SDK into LabVIEW, and I'm running into issues with function decorations. I have a header file, the dll, a lib, and a .exp file. Unfortunately, I only have the function declarations in the header. If I had the definitions, I'd recompile the dll using the "extern C" flag I've read about.
I feel like I should be able to wrap the SDK's dll in some way using a definitions file or something to translate the compiled function names and effectively "unmangle" them for LabVIEW. Something (very loosely) like:
myFunc(arg){
?myFunc#Class#namespace##otherstuff(arg);
}
However, I haven't been able to find any discussion about this anywhere I've looked. It's entirely possible that it's abstracted enough that I don't recognize it when I see it, but I feel like it's more likely there's a reason this isn't done.
So, that's my question: can [referencing one dll from another] be done, since I've never seen dll imports combined with exports, and could I do that to get around the need to recompile the dll from the original code?
I'm guessing no, but I'd love to be wrong.

Wrapping DLL to log certain functions

So if I have both a complete set of headers and a .lib file for a C++ dll, is it possible to create a second C++ dll that wraps the original and allows me to see when certain functions are called and then it just calls the original functions? Is there a simpler way of doing this? I also am only concerned about a couple of functions in a large dll
Of course it is possible. Why wouldn't you think it is? It is even possible to define an exported function to be an alias for an exported function in another DLL, to pass through the functions you are not interested in.
Where you might run into a problem is when software uses the original .lib file to static link to the original DLL. Since you likely wouldn't be able to recompile such software to use your .lib files, your DLL would need to have the same filename as the original DLL, and replicate the original DLL's exports exactly (names and ordinal).
Another problem would be if the original DLL exports a class that software uses. Those would be harder to replicate.
A different approach would be to not replace the original DLL at all, but instead to inject your DLL into the target process and then detour just the DLL exports that you are interested in.

Do I need to distribute a header file and a lib file with a DLL?

I'm updating a DLL for a customer and, due to corporate politics - among other things - my company has decided to no longer share source code with the customer.
Previously. I assume they had all the source and imported it as a VC++6 project. Now they will have to link to the pre-compiled DLL. I would imagine, at minimum, that I'll need to distribute the *.lib file with the DLL so that DLL entry-points can be defined. However, do I also need to distribute the header file?
If I can get away with not distributing it, how would the customer go about importing the DLL into their code?
Yes, you will need to distribute the header along with your .lib and .dll
Why ?
At least two reasons:
because C++ needs to know the return type and arguments of the functions in the library (roughly said, most compilers use name mangling, to map the C++ function signature to the library entry point).
because if your library uses classes, the C++ compiler needs to know their layout to generate code in you the library client (e.g. how many bytes to put on the stack for parameter passing).
Additional note: If you're asking this question because you want to hide implementation details from the headers, you could consider the pimpl idiom. But this would require some refactoring of your code and could also have some consequences in terms of performance, so consider it carefully
However, do I also need to distribute the header file?
Yes. Otherwise, your customers will have to manually declare the functions themselves before they can use it. As you can imagine, that will be very error prone and a debugging nightmare.
In addition to what others explained about header/LIB file, here is different perspective.
The customer will anyway be able to reverse-engineer the DLL, with basic tools such as Dependency Walker to find out what system DLLs your DLL is using, what functions being used by your DLL (for example some function from AdvApi32.DLL).
If you want your DLL to be obscured, your DLL must:
Load all custom DLLs dynamically (and if not possible, do the next thing anyhow)
Call GetProcAddress on all functions you want to call (GetProcessToken from ADVAPI32.DLL for example
This way, at least dependency walker (without tracing) won't be able to find what functions (or DLLs) are being used. You can load the functions of system DLL by ordinal, and not by name so it becomes more difficult to reverse-engineer by text search in DLL.
Debuggers will still be able to debug your DLL (among other tools) and reverse engineer it. You need to find techniques to prevent debugging the DLL. For example, the very basic API is IsDebuggerPresent. Other advanced approaches are available.
Why did I say all this? Well, if you intend to not to deliver header/DLL, the customer will still be able to find exported functions and would use it. You, as a DLL provider, must also provide programming elements with it. If you must hide, then hide it totally.
One alternative you could use is to pass only the DLL and make the customer to load it dynamically using LoadLibrary() + GetProcAddress(). Although you still need to let your customer know the signature of the functions in the DLL.
More detailed sample here:
Dynamically load a function from a DLL

Creating a dll without namespace for cpp

currently I am writing on a cpp-DLL. Afaik I have to put the functions into a class and a namespace if another cpp-program wants to use them. But I want to use the DLL with Labview too. Labview only recognizes the functions if they are free, e.g. neither in a namespace nor in a class. How can I implement this in my DLL? At the moment, I have set a #define-variable. If this variable is set, the functions are enclosed in a namespace and a class, if not, then they are free, but I have to compile the whole thing twice and I get two separate DLL files. So, what can I do if I only want one DLL file for both applications? (Please don't tell me to write the functions twice, the administrative outlay is even worse, I have tried this before).
Or do I simply have to call the DLL via LoadLibrary() when not using namespaces?
Thank you very much!
Afaik I have to put the functions into a class and a namespace if another cpp-program wants to use them.
This is plain wrong. You do not need to do this at all. On the contrary, DLL were originally introduced as libraries of C functions. C++ uses mangled names to represent namespace/class and types of parameters. There is no standard on this. Different compilers use their own scheme.
To summarize:
If you export simple C function from your dll, this will always work.
If you export classes or something from a namespace, this will
definitely work if other .exe/.dll is compiled with the same version of the
compiler. If not - this depends.
Regarding the LoadLibrary: it should be used when you do not know the name of the DLL or a name of the function in this DLL ahead or when you do not want to load this DLL at the beginning of your process. Otherwise (simple case) link your executable with implib for that DLL. This perfectly works for simple c-functions. LoadLibrary should be used when direct linking is not good for some reason.

why do we need to export a class in C++?

I am a beginner, so, please bear with me, if this sounds too trivial.When i searched over the net for this, i got results showing how to do it. My question is why we do it in the first place ?
This is specific to the Windows platform, when you're developping C++ DLLs.
You have to use the __declspec(dllexport) modifier in order for your class and its methods to appear on the exported symbol list for your DLL.
This way, executables using your DLL can instanciate and call methods in these classes.
However you have to make sure that the executable and the DLL are compiled by the same version of the same compiler, because C++ symbols are exported using a relatively complex name mangling encoding (you can see that using depends.exe), which format varies from one compiler to another.
You don't need to export anything, unless you're creating a DLL. In that case, you can use the dllexport attribute as an alternative to the "old school" way of using .def files.
Technically you cannot export a class, only functions.
However you can designate on a class level that all functions be exported.
Exporting a function means that that function can be called from outside of the current executable.
This is needed when you are writing a dll for example which is a separate entity.