DLLs

Started by
4 comments, last by GameDev.net 24 years, 4 months ago
The only real difference with MSVC is that Microsoft has created their own little standard for declaring DLL imports and exports. Here's a little example of how to make a DLL you can import into other projects.

//
// MyDLL.h
//

// Visual C++ version
#ifdef MYDLL_IMPORT
#define MYDLL_API _declspec(dllimport)
#else
#define MYDLL_API _declspec(dllexport)
#endif

// You'll have to find the way Borland declares variables as imported/exported
// and replace _declspec(...) with the appropriate commands

// Force compiler to use undecorated function/variable names.
// This is most useful if you plan to dynamically link the DLLs.
extern "C"
{
void MYDLL_API MyDLLFunc( void );
}

//
// MyDLL.cpp
//

#include "MyDLL.h"

void MYDLL_API MyDLLFunc( void )
{
}

Add the two above files to an empty DLL project and it will compile a DLL that exports one function (MyDLLFunc) that does nothing. You get the idea. Also, to import the DLL into a project, link the project to the .lib file compiled with the DLL and include the .h file like this:

//
// MyProject.cpp
//

#define MYDLL_IMPORT
#include "MyDLL.h"

Then you can just call the functions exported in the DLL wherever you like. If you want to dynamically load the DLLs (load them without having to link to their lib), check out the Windows API commands LoadLibrary and GetProcAddress.

Advertisement
Microsoft simply included that for simplicity and inline exporting and importing. If you want to do it the "old fashioned" way, you can construct a .DEF file that contains the information necessary to the compiler for creating the DLL. MSDN (as always) has a bunch of info on this method. MSDN is my friend...

- Splat

Alright, first, thanks for your replies, it answered my original question but I think I left something out, how would I export classes (and, for that matter, import them) ?

Thanks again for any replies!

Splat: I hope at least some of your friends are less annoying ...

/Niels

<b>/NJ</b>
I just finished the GUI for my new game, and it works perfectly, but I wanted to make it into a DLL. I've looked around quite a bit, but I havn't seen any tutorial on "How to make DLLs". I'm using Borland C++ 5, so if anyone knows any tutorials, or finds it simple enough to tell me right here, thanks! (Oh, and if you ONLY know how to do it for MSVC++, that's okay too, I could probably translate it okay! Thanx again!)
Hey, you insulting MSDN Library? I thought not...

Actually, I've initially learned a whole shit load of stuff from MSDN, it is the reason Win32 development is much better than any other platform. I mean, yes, every so often MSDN is wrong (gotta love the "undocumented" features of the Platform SDK), but what other platform can you in 5 seconds have a detailed syntaxical listing of a function with the desciption of each return value, quirks, and what it actually does?

However, some of my friends are A LOT more annoying than MSDN.

- Splat

This topic is closed to new replies.

Advertisement