I'm clueless as to how this works. I would think that I would load the DLL using LoadLibrary and setting the address of the externed functions, but apparently not. Anyone know how? I've seen the SDL use this, and while I know that you can use function pointers instead, I like having VC++'s intellisense helping me out
After calling LoadLibrary(), you have to call GetProcAddress() to obtain the function pointer as a void *. Then you'll have to cast the pointer to type that you're expecting (e.g. to (int(*)(int))).
Note that you can't GetProcAddress() from normal C++ functions. When you're writing the library, you have to add extern "C" to the declaration (the definition can stay as non-extern "C", however) or the compiler will mangle the exported symbol. This means that you can't overload functions.
I know how to load the functions, however, whenever I extern a function and try to compile VC++ always gives me an error saying that the DLL function is undefined.
That's because you're trying to declare and call the function, which not what you should do. You should typedef the function pointer type and call the pointer. For example, suppose you want to dynamically load strlen():
1 2 3 4 5 6 7 8 9 10
#if //building a DLL (can't remember the macro ATM)
extern"C" __declspec(export) size_t strlen(char *);
#else
//We're not building a DLL. We're building a program that will link the DLL at run time.
typedef size_t(*strlen_f)(char *);
#endif
//...
strlen_f strlen=GetProcAddress(/*...*/);
size_t n=strlen("Hello, World!\n");
I've used that before, and the VC++ intellisense system wasn't able to help with the arguments. In the SDL, they use extern to declare their functions. How do they do it?
I've used that before, and the VC++ intellisense system wasn't able to help with the arguments.
Odd. It usually works for me. Then again, IntelliSense is known to have seizures at random.
In the SDL, they use extern to declare their functions. How do they do it?
Well, that's entirely different. When the SDL is built, the compiler is told to create an import library (.lib) that a library user can then link to his code. This library includes the definitions of all the functions that aren't exported, plus pseudo-definitions of the ones that are. What it really does is call LoadLibrary() and GetProcAddress() for you.
Are you sure that's what you want to do? Using this type of linking, the program will not be able to run without the DLL. If that's works for you, fine, but if the component the DLL defines is supposed to be optional, it will become mandatory.