DLL Help

Started by
2 comments, last by StaticEdge 22 years, 4 months ago
I have gotten to the point where I would like to use DLL''s in programs (Windows/DOS) but, I can not find any information on them. If someone could direct me to tutorials on coding DLL files it would be highly-appreciated. Thank you. -Matt setNick("StaticEdge", NULL);
-StaticEdgeThe Immersion Project
Advertisement
I don''t think you can load DLL''s in DOS (not officially anyway...I think there''s a DOS extender that has a support library with routines that do that kind of thing...can''t remember what though).

You can however in Windows (funny that, what with system DLLs etc.). The main functions you use are

  // this loads the named DLL file into memoryHMODULE LoadLibrary(LPCTSTR *szLibrary);// you then need to load each DLL export you want to use, you can load it by name or ''ordinal'' - // basically a function number, a 16 bit number, but you''ll need to cast it as a LPCTSTR (2nd arg)FARPROC GetProcAddress(HMODULE hModule, LPCTSTR lpProcName);// when you''ve finished using the DLL unload it with this:BOOL FreeLibrary(HMODULE hModule);  


You should find more info about these on the MSDN (via MS website)

Other point worth mentioning is how to form pointers to a function. You can do it with a typedef, like so

  // this creates a variable type that will operate as a pointer to the MessageBox Funcntiontypedef int (*PFNMSGBOX)(HWND, LPCTSTR, LPCTSTR, UINT);// a variable can be declared, eg:PFNMSGBOX  pfnMessageBox = NULL; // good idea to init it to a known valueHMODULE hDll = NULL;// example function to load the user32.dll file & the functionbool LoadDll(void) {  // load the library & return false on failure hDll = LoadLibrary("user32.dll"); if(!hDll) return false; // get the proceedure address, note the type cast on the // returned value & also that windows has 2 varients of many string functions // suffixed with W and A for Widestring & Ascii strings  pfnMessageBox = (PFNMSGBOX) GetProcAddress(hDll, "MessageBoxA"); if( pfnMessageBox ) return true; // loaded badly, clear up FreeLibrary(hDll); hDll = NULL;}// example of using the functionvoid ShowMessageBox(void){ if(pfnMessageBox) pfnMessageBox(NULL, "Dynamically loaded from USER32.DLL", "Hello",MB_OK);}  


Thats the long & short of it really - hope that helps a bit
hey, im not a total programming newbie, but what do u use a dll for??? thanks, Matt

------------------------------
"kill one and your a murderer, kill thousands and your a conquerer"
"We must move forwards not backwards, upwards not forwards, and always twirling, twirling towards freedom."
------------------------------"kill one and your a murderer, kill thousands and your a conquerer""We must move forwards not backwards, upwards not forwards, and always twirling, twirling towards freedom."
quote:Original post by dark_angel09
hey, im not a total programming newbie, but what do u use a dll for??? thanks, Matt


DLLs are not statically linked to your program. Hence you can change them without having to recompile it. Or, even better, with some precautions, even while the program is running, updating on the fly the code it is using. Or, since you can load as many DLLs as your system can handle, and that you can extract function pointers from them, you can implement a plug-in system to let your game handle new kind of objects with the same game executable (Creatures 3, The Sims ...), by having DLLs providing pointers to functions returning the new objects (cast to a common base class that your main executable know how to deal with)

Or say, your game uses openGL. Well, each video card manufacturer can provide their own optimized implementation of the library, your program won't know, so long as the functions it uses are exported by the library.

Edited by - Fruny on December 6, 2001 7:29:31 PM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement