about linking DLL's.

hello,
I've trying to learn about DLL's and linking on MSDN and have few questions.

there are talking about explicit and implicit linking.
is that mean implicit == static linkiing and
explicit == dinamyc linking.

some explanation or examples/reference would be nice.

thanks.

EDIT:
are there some standard calls to DLL's cos __declspec looks to me like microsoft specific.
or is it that "standard"?
what are other options which will work on other compilers.
Last edited on
I asked a similar question last week. Perhaps this can help:

http://cplusplus.com/forum/general/55526/
Here's a pretty good link from M$ that explains it in some detail: http://msdn.microsoft.com/en-us/library/253b8k2c(v=VS.80).aspx

Basically Implicit linking is when you use an import library to resolve the address of the functions exported by the DLL. Explicit Linking is when you use the "LoadLibrary(...)" function to load a DLL and then use "GetProcAdress(...)" to get a pointer to the function you are trying to use.
Last edited on
thanks alot for your replies,
implicit linking is relay easy but I'm strugling with explicit one :/

can you help me by teling why function call does not work in this example:
(access violation-- it's commented out in main.cpp)

DLL HEADER

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once

// MathFuncsDll.h

namespace MathFuncs
{
    class MyMathFuncs
    {
    public:
        // Returns a + b
        static __declspec(dllexport) double Add(double a, double b);

        // Returns a - b
        static __declspec(dllexport) double Subtract(double a, double b);

        // Returns a * b
        static __declspec(dllexport) double Multiply(double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        static __declspec(dllexport) double Divide(double a, double b);
    };
}


DLL SOURCE
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
// MathFuncsDll.cpp
// compile with: /EHsc /LD

#include "MathFuncsDll.h"
#include <stdexcept>
using namespace std;

namespace MathFuncs
{
    double MyMathFuncs::Add(double a, double b)
    {
        return a + b;
    }

    double MyMathFuncs::Subtract(double a, double b)
    {
        return a - b;
    }

    double MyMathFuncs::Multiply(double a, double b)
    {
        return a * b;
    }

    double MyMathFuncs::Divide(double a, double b)
    {
        if (b == 0)
        {
            throw new invalid_argument("b cannot be zero!");
        }

        return a / b;
    }
}


PROGRAM CALLING THAT DLL AS FOLOWS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <Windows.h>
using namespace std;

typedef double (CALLBACK* ptr)(double, double);

int main()
{
	HINSTANCE hDLL = LoadLibraryA("MathFuncs.dll");
	ptr func = (ptr)GetProcAddress(hDLL, "Add");
	double a = 3.3, b = 4.4;
	if(hDLL == NULL)
		cout << "error" << endl;
	else
		cout << "OK..." << endl;
	func(a, b); //ACCESS VIOLATION ERROR
	FreeLibrary(hDLL);
	cin.ignore();
	return 0;
}


here main program can't call function Add located in dll.

thanks.

EDIT:
forget it...
the error was that I didn't copy *.dll into folder where my application is lol
thanks for help!!
Last edited on
Topic archived. No new replies allowed.