Solved

DLL 16bits creation for Excel use

Posted on 1997-03-26
3
229 Views
Last Modified: 2010-04-10
I need to build a basic DLL: f(x)=x*x in Borland 4.52 in W31
I call this DLL from a Visual Basic macro in Excel5 to get
the result of the calculation.
I know how to call this DLL from Excel, but I don't know how to create the DLL in Borland.
For the moment my .cpp file contains:
#include <windows.h>
extern double _export FAR test_calc (double x);

double FAR _export test_calc (double x)
{
return x*x;
}

int FAR PASCAL LibMain (HANDLE, WORD, WORD, WORD, LPSTR)
{
return 1;
}
But the macro returns:"Not definied function in the specified DLL" (I know the Visual Basic macro is correct).

Is the .cpp file correct to idenfiy the entry variable and
to return this simple result ?
0
Comment
Question by:chiarelli
3 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1162461
Since your file name is *.cpp, you should use C linkage.

extern "C" double _export FAR test_calc (double x);

extern "C" double FAR _export test_calc (double x)
                  {
                  return x*x;
                  }
0
 

Author Comment

by:chiarelli
ID: 1162462
This answer does not resolve the problem.
The question is still the same.
Thanks

0
 

Accepted Solution

by:
meinn earned 50 total points
ID: 1162463
chensu is correct that you must use the C++ construct
  extern "C"
to force the compiler to treat your function as a C, rather than
a C++, function. (See any book on C++ for a discussion).

However, there are a number of "C" calling conventions used in the DOS/Windows world - PASCAL, CDECL, FASTCALL and STDCALL. By default, your C++ compiler will be generating a CDECL function (probably called something like _test_calc), while Excel is looking for a PASCAL function (called TEST_CALC).

The conventional solution is something like:

// ----- In your .h header file -----

#ifdef __cplusplus
extern "C" {
#endif

#ifdef BUILDING_DLL
#define PUBLIC FAR PASCAL EXPORT
#else
#define PUBLIC FAR PASCAL
#endif

double PUBLIC test_calc(double x);

// more functions...

#ifdef __cplusplus
}
#endif

// ------ In your .cpp implementation file -----

#define BUILDING_DLL
#include "whatever.h"

double PUBLIC test_calc(double x)
{
// ...
}

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question