How to create and use DLL's

I'm trying to find out how to create and use DLL's in eclipse.

I know general C++, library but i cannot figure out DLL's.

p.s. I've posted on this topic before please if you know make the internet a little smarter.
To make a DLL, you need to use the <windows.h> library (hence, this should really go into the "Windows Programming" section). To make a DLL, you need a header file and a source file, with the header file defining all your export functions and the source file giving the data. Here is a quick example:

dll.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef __dll_h_included__
#define __dll_h_included__

#ifdef DLL_EXPORT
#  define DLL_API __declspec(dllexport);
#else
#  define DLL_API __declspec(dllimport);
#endif

#define DLL_CALL __cdecl

namespace myFuncs {
    DLL_API void DLL_CALL doSomething(void);
    DLL_API double DLL_CALL doSomethingElse(int a, float b);
}

#endif 


dll.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "dll.h"
#include <windows.h>
#include <iostream>

namespace myFuncs {

void DLL_CALL doSomething(void) {
    std::cout << "Hello!" << std::endl;
}

double DLL_CALL doSomethingElse(int a, float b) {
    double c = a*b;
    return c;
}

}

BOOL APIENTRY DllMain (
    HINSTANCE hInst, 
    DWORD reason,
    LPVOID reserved)
{
    switch (reason) {
        case DLL_PROCESS_ATTACH:
            break;
        case DLL_PROCESS_DETACH:
            break;
        case DLL_THREAD_ATTACH:
            break;
        case DLL_THREAD_DETACH:
            break;
    }

    return TRUE;
}


To build this with MinGW, you would use something like:
g++ -c -o dll.o dll.cpp -D DLL_EXPORT
g++ -o dll.dll dll.o -s -shared -Wl,--subsystem,windows


Also, to make it have an import library as well, you could do the second command like this:

g++ -o dll.dll dll.o -s -shared -Wl,--subsystem,windows,--out-implib,libdll.a

Not sure about MSVC++, though. Look it up on MSDN, it is almost definitely there.

EDIT:
Got slightly confused when writing example, shouldn't have compiled...
Last edited on
Topic archived. No new replies allowed.