I have this DLL code, but I cannot access function.
Where am I wrong?

main.h

#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>
/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif

//simple functions are being exported
EXPORT_DLL double Addition(double a, double b);
EXPORT_DLL double Multiplication(double a, double b);
#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__

main.cpp

#include "main.h"

//add them and return the answer
double DLL_EXPORT Addition(double a, double b)
{
	double c = a+b;
	return c;
}

//multiply them and return the answer
double DLL_EXPORT Multiplication(double a, double b)
{
	double c = a*b;
	return c;
}

And here is the console App that calls the DLL

#include <iostream>
#include <windows.h>
typedef double(*FuncPtr)(double a, double b);
//declare DLL handle
HINSTANCE hDLL;
//declare pointers to the functions
FuncPtr AddFunc, MultiFunc;

using namespace std;

int main()
{

    //create handle to our DLL
    hDLL = LoadLibrary("mathdll");
    //Get proc address if handle is valid
    if (hDLL!=NULL)
    {
        cout<<"Loaded DLL! \n"<<"Getting Proc Address"<<endl;
        AddFunc = (FuncPtr)GetProcAddress(hDLL, "Addition");
        if(AddFunc)
        {
            cout<<"Sum of 8 and 2 is: "<<AddFunc(8.0, 2.0);
            FreeLibrary(hDLL);
        }
        else
        {
            cout<<"Failed to get a function! Unloading the DLL"<<endl;
            FreeLibrary(hDLL);
        }
    }
        return 0;

}
This article has been dead for over six months. Start a new discussion instead.