M
moxman99
Guest
Hello All,
I have reposted this from the Visual C++ forum since it could also be solved in this C#Forum.
I am trying to call a C++ DLL in C#.
I have read numerous forums, however I cannot seem to get a simple math function to be called from within my C#.
One thing I have to mention is my DLL I am calling is made to call another DLL. It works fine when I created a console application in C++ and then called the secondary DLL, which in return called the methods from the first DLL. My secondary DLL being called from the C# is MyExecRefsDll.dll. The DLL in which it calls is MathFuncsDll.dll. All of the C++ code was taken from a tutorial here:http://msdn.microsoft.com/en-us/library/ms235636(v=vs.100).aspx
Here is the header and cpp code for MyExecRefsDll.dll:
// MyExecRefsDll.h
#pragma once
extern "C"{
#ifdef MYEXECREFSDll_EXPORTS
#define MYEXECREFSDll_API __declspec(dllexport)
#else
#define MYEXECREFSDll_API __declspec(dllimport)
#endif
namespace MyExecFuncsNS
{
class MyDoMathCall
{
public:
static MYEXECREFSDll_API int DoMath();
};
}}
And here is the cpp:
// MyExecRefsDll.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <iostream>
#include "MathFuncsDll.h"
#include "MyExecRefsDll.h"
using namespace std;
namespace MyExecFuncsNS
{
int MyDoMathCall:oMath()
{
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:ivide(a, b) << endl;
try
{
cout << "a / 0 = " <<
MathFuncs::MyMathFuncs:ivide(a, 0) << endl;
}
catch (const invalid_argument &e)
{
cout << "Caught exception: " << e.what() << endl;
}
return 0;
}
}
Now here is the MathFuncsDll.h code:
// 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);
};
}
And the respective 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:ivide(double a, double b)
{
if (b == 0)
{
throw invalid_argument("b cannot be zero!");
}
return a / b;
}
}
Here is my C# code I am calling the MyExecRefsDll.dll from:
// Marshal.cs
using System;
using System.Runtime.InteropServices;
class PlatformInvokeTest
{
public static class DllHelper
{
[DllImport(@"c:\Users\tester\Documents\Visual Studio 2010\Projects\DynamicLibrary\Debug\MyExecRefsDll.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "DoMath()")]
public static extern int DoMath();
}
static void Main(string[] args)
{
try
{
DllHelper.DoMath();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Now the error I receieve is when it cannot find the EntryPoint name. I receieve this error
A first chance exception of type System.EntryPointNotFoundException occurred in Example Project1.exe
So I have read many forums and I see that I need to include and extern "c" in my header files for the call however I am very confused for how to implement and why this is needed. I undertstand I am linking the DLL to the C# language but am having the hardest time.
I am using Microsoft Visual C# and C++ Express 2010.
Any help would be greatly appreciated.
Kind Regards.
Continue reading...
I have reposted this from the Visual C++ forum since it could also be solved in this C#Forum.
I am trying to call a C++ DLL in C#.
I have read numerous forums, however I cannot seem to get a simple math function to be called from within my C#.
One thing I have to mention is my DLL I am calling is made to call another DLL. It works fine when I created a console application in C++ and then called the secondary DLL, which in return called the methods from the first DLL. My secondary DLL being called from the C# is MyExecRefsDll.dll. The DLL in which it calls is MathFuncsDll.dll. All of the C++ code was taken from a tutorial here:http://msdn.microsoft.com/en-us/library/ms235636(v=vs.100).aspx
Here is the header and cpp code for MyExecRefsDll.dll:
// MyExecRefsDll.h
#pragma once
extern "C"{
#ifdef MYEXECREFSDll_EXPORTS
#define MYEXECREFSDll_API __declspec(dllexport)
#else
#define MYEXECREFSDll_API __declspec(dllimport)
#endif
namespace MyExecFuncsNS
{
class MyDoMathCall
{
public:
static MYEXECREFSDll_API int DoMath();
};
}}
And here is the cpp:
// MyExecRefsDll.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include <iostream>
#include "MathFuncsDll.h"
#include "MyExecRefsDll.h"
using namespace std;
namespace MyExecFuncsNS
{
int MyDoMathCall:oMath()
{
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:ivide(a, b) << endl;
try
{
cout << "a / 0 = " <<
MathFuncs::MyMathFuncs:ivide(a, 0) << endl;
}
catch (const invalid_argument &e)
{
cout << "Caught exception: " << e.what() << endl;
}
return 0;
}
}
Now here is the MathFuncsDll.h code:
// 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);
};
}
And the respective 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:ivide(double a, double b)
{
if (b == 0)
{
throw invalid_argument("b cannot be zero!");
}
return a / b;
}
}
Here is my C# code I am calling the MyExecRefsDll.dll from:
// Marshal.cs
using System;
using System.Runtime.InteropServices;
class PlatformInvokeTest
{
public static class DllHelper
{
[DllImport(@"c:\Users\tester\Documents\Visual Studio 2010\Projects\DynamicLibrary\Debug\MyExecRefsDll.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "DoMath()")]
public static extern int DoMath();
}
static void Main(string[] args)
{
try
{
DllHelper.DoMath();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Now the error I receieve is when it cannot find the EntryPoint name. I receieve this error
A first chance exception of type System.EntryPointNotFoundException occurred in Example Project1.exe
So I have read many forums and I see that I need to include and extern "c" in my header files for the call however I am very confused for how to implement and why this is needed. I undertstand I am linking the DLL to the C# language but am having the hardest time.
I am using Microsoft Visual C# and C++ Express 2010.
Any help would be greatly appreciated.
Kind Regards.
Continue reading...