Calling a DLL from a Console Application

  • Thread starter Thread starter moxman99
  • Start date Start date
M

moxman99

Guest
Hello all,

I am trying to have my console application (CallDLL.exe) use a DLL I created named MyExecRefsDll.dll. MyExecRefsDll calls another DLL named MathFuncsDll.dll. I have previously asked a question about the use of MyExecRefsDll.dll and MathFuncsDll.dll here: http://social.msdn.microsoft.com/Forums/vstudio/en-US/3c35dcad-9969-4a6c-83d2-a8b1bd4bb8c3/trying-to-call-a-dll-from-another-dll

It contains the source code of a sample tutorial project I found here: http://msdn.microsoft.com/en-us/library/vstudio/ms235636(v=vs.100).aspx which was on creating a DLL with a console application to test it.

My console app is called CallDLL. I have been unable to test the functionality of the program because I keep receiving the fatal error:



1>------ Build started: Project: CallDLL, Configuration: Debug Win32 ------

1>LINK : fatal error LNK1104: cannot open file c:\users\tester\documents\visual studio 2010\Projects\DynamicLibrary\Debug\MyExecRefsDll.lib

========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========
CallDLL has the Common Properties reference "MyExecRefsDll" (Located in Project\References\Common Properties\Framework and References).


It also has the Configuration Properties general C/C++ reference as:

1. C:\users\tester\Documents\Visual Studio 2010\Projects\DynamicLibrary\MyExecRefsDll\
2. C:\users\tester\Documents\Visual Studio 2010\Projects\DynamicLibrary\MathFuncsDll\

Here is the simple code from the console app CallDLL I created:

// CallDLL.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "MyExecRefsDll.cpp"
#include "MathFuncsDll.h"

int _tmain()
{
main();
}



It calls main() which is inside of MyExecRefsDll.cpp

MyExecRefsDll.cpp looks like so (and also inside of the previous asked question from the top):

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

#include "stdafx.h"

#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;
}



perhaps I need to create a header file for the MyExecRefsDll but the tutorial said I do not need to but because I am modifiying this tutorial to my own needs, I do not have one. But I totally understand if this is my problem and will tinker around with this idea until I receieve some input from this forum.


Main() calls methods from MathFuncsDll which has this header file:

// MathFuncsDll.h
#pragma once

#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);
};
}


Also the MathFuncsDll.cpp is this:

// 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;
}
}




**MyExecRefsDll has the Common Properties reference MathFuncsDll (Located in Project\References\Common Properties\Framework and References).

It also has the Configuration Properties general C/C++ reference as:

1. C:\Users\nicalder\Documents\Visual Studio 2010\Projects\DynamicLibrary\MathFuncsDll


So I would like to have my console app call the second DLL which in then will call the 1st DLL.

Please let me know if there is anything else I can provide.

I realize all of my code is perhaps non-conventional to say the least however I am new to C++ and trying to implement something I was told I had to do.


Kind Regards.

Continue reading...
 
Back
Top