Quantcast
Channel: Visual C forum
Viewing all articles
Browse latest Browse all 15302

Trying to call a DLL from another DLL

$
0
0

Hello All,

I have created a DLL called MathFuncsDll.dll. It is a simple DLL that will do all basic math operations such as add, subtract, multiply and divide.

The DLL was created with Microsoft Visual C++ Express following this tutorial : http://msdn.microsoft.com/en-us/library/vstudio/ms235636(v=vs.100).aspx

Now in this tutoial it is just teaching how to create a simple DLL. The first part tells me how to create the DLL, the second part is to create a console application that references the dynamic link library essectially by creating a console application that uses load-time dynamic linking to reference the DLL.

Now I have followed all steps for the creation of this tutorial except I have instead changedApplication Settings page, under Application type, select Console application, I have selected DLL. I then tried to follow all the same steps but instead of running a console application, I have a DLL.

I receieve and error when I "start without debugging" or run saying "unable to start program 'c:\users\user\MyExecRefsDLL.dll' "

here is my source (also the same from the tutorial):

HEADER file:

// MathFuncsDll.h

#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport) 
#else
#define MATHFUNCSDLL_API __declspec(dllimport) 
#endif


namespace MathFuncs
{
    // This class is exported from the MathFuncsDll.dll
    class MyMathFuncs
    {
    public: 
        // Returns a + b
        static MATHFUNCSDLL_API double Add(double a, double b); 

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

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

        // Returns a / b
        // Throws const std::invalid_argument& if b is 0
        static MATHFUNCSDLL_API double Divide(double a, double b); 
    };
}


SOURCE CPP file:

// MathFuncsDll.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#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 invalid_argument("b cannot be zero!");
        }

        return a / b;
    }
}

Second DLL:

SOURCE CPP file:

// MyExecRefsDll.cpp
// compile with: /EHsc /link MathFuncsDll.lib

#include <iostream>

#include "MathFuncsDll.h"

using namespace std;

int main()
{
    double a = 7.4;
    int b = 99;

    cout << "a + b = " <<
        MathFuncs::MyMathFuncs::Add(a, b) << endl;
    cout << "a - b = " <<
        MathFuncs::MyMathFuncs::Subtract(a, b) << endl;
    cout << "a * b = " <<
        MathFuncs::MyMathFuncs::Multiply(a, b) << endl;
    cout << "a / b = " <<
        MathFuncs::MyMathFuncs::Divide(a, b) << endl;

    try
    {
        cout << "a / 0 = " <<
            MathFuncs::MyMathFuncs::Divide(a, 0) << endl; 
    }
    catch (const invalid_argument &e) 
    {
        cout << "Caught exception: " << e.what() << endl; 
    }

    return 0;
}


In the end I just want anyway to create a DLL, then create another that can interact with the other.

So essentially a wrapper-secondary DLL for a primary DLL but I have searched for this and are having trouble finding a straightforward solution.

I have also been experimenting with Platform Invoke from this article: http://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx
But it is not what I want to do.

Any help is great appreciated.

Regards


Viewing all articles
Browse latest Browse all 15302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>