I've been wondering about DLLs. I understand that you can declare a class in a DLL and then using a wrapper, which can even be in a different language, you can bind to those functions and call them externally. I don't really understand how this works though.
What I'm wondering is, if i have a class in a DLL and I bind to and call one of the functions in that class from an external application, is a new instance of that class created for each application that binds to it? So that, for example, if in the DLL class is a private member variable which is used in one of the functions, each application that calls the DLL functions has its own instance of that private member which is worked upon when it calls the function.
I hope the question makes sense, I'm new to this stuff.
Thanks =)
Classes you declare in a DLL aren't instantiated when the DLL is loaded...so, no.
The DLL itself is a 'class' of sorts.
Be careful about the data types you use, a std::string in the implementation you compiled your DLL with may be very different from the std::string implementation in another program loading your DLL. AKA, it could crash.
It really depends on the operating system. But as you've used the term DLL, I'll assume you mean WIN32 or WIN64.
When you load the DLL, you've just loaded code (and resources), you haven't instantiated anything. Each class in the DLL you instantiate can have private and protected data, just like the classes in a program. And each instance of a DLL that's loaded by a different process is distinct instance.
As described above in L B's post, you need to be careful with objects that are passed to/from the DLL classes. They may not be the same thing.
So when I load a DLL I'm not actually creating any objects, but after I load the DLL in the external application, I can then instantiate one of the classes declared in the DLL in the calling application?
As you ought to know, a function is just code loaded at some address. When the program loaded loads the DLL, if maps function addresses onto function names for you. When you load it yourself (using LoadLibrary), have to ask it where the function is (with GetProcAddress).
When you call a function in a DLL, it's no different from calling one in your own program.
Can I have private members used in a DLL function?
Yes. The only parts of a DLL you can see are the explicitly exported parts. The can be exported with __declspec(dllexport) or by placing the symbol name in the .DEF file. Everything else is private.
So a function in a DLL can use internal objects that are not visible to the program.