Discussion:
Use dll that has scope resolution operators?
(too old to reply)
Brian
2010-09-29 19:30:19 UTC
Permalink
I am trying to create a simple dll that I can use the functions into
my Delphi program.

I created a dll following this howto, but it has scope resolution.
Does Delphi 6 support that?
http://msdn.microsoft.com/en-us/library/ms235636%28VS.80%29.aspx

Is it possible to do something like the following? I still have yet to
get a simple function to import from a dll.

function MyMathFuncs::Add(x: double, y: double): double; external
'MathFuncsDll.dll';

brian
Brian
2010-09-29 19:39:15 UTC
Permalink
Post by Brian
I am trying to create a simple dll that I can use the functions into
my Delphi program.
Here is the sample program, but it can't resolve the symbol
MyMathFuncs_Add.

brian

program Project14;

{$APPTYPE CONSOLE}

uses
SysUtils;

function MyMathFuncs_Add(x: double; y: double): double; external
'MathFuncsDll.dll';

var
x: double;
y: double;
result: double;

begin
{ TODO -oUser -cConsole Main : Insert code here }


x := 5.0;
y := 10.0;
result := MyMathFuncs_Add(x,y);
Writeln('Hello World ', result);
end.
Brian
2010-09-29 19:50:43 UTC
Permalink
Post by Brian
Post by Brian
I am trying to create a simple dll that I can use the functions into
my Delphi program.
Here is the sample program, but it can't resolve the symbol
MyMathFuncs_Add.
brian
program Project14;
{$APPTYPE CONSOLE}
uses
  SysUtils;
function MyMathFuncs_Add(x: double; y: double): double; external
'MathFuncsDll.dll';
var
  x: double;
  y: double;
  result: double;
begin
  { TODO -oUser -cConsole Main : Insert code here }
  x := 5.0;
  y := 10.0;
  result := MyMathFuncs_Add(x,y);
  Writeln('Hello World ', result);
end.
I figured it out. I put extern "C" in my C++ code and now the dll
doesn't use the C++ scope resolution operator. Works now.

program Project14;

{$APPTYPE CONSOLE}

uses
SysUtils;

function Add(x: double; y: double): double; external
'MathFuncsDll.dll';

var
x: double;
y: double;
result: double;

begin
{ TODO -oUser -cConsole Main : Insert code here }


x := 5.0;
y := 10.0;
result := Add(x,y);
Writeln('Hello World ', result);
end.


// MathFuncsDll.h



// Returns a + b
extern "C" __declspec(dllexport) double Add(double a, double
b);

// Returns a - b
extern "C" __declspec(dllexport) double Subtract(double a,
double b);

// Returns a * b
extern "C" __declspec(dllexport) double Multiply(double a,
double b);

// Returns a / b
// Throws DivideByZeroException if b is 0
extern "C" __declspec(dllexport) double Divide(double a,
double b);

// MathFuncsDll.cpp
// compile with: /EHsc /LD

#include "MathFuncsDll.h"

#include <stdexcept>

using namespace std;


double Add(double a, double b)
{
return a + b;
}

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

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

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

return a / b;
}

Loading...