Solved

DLL 16bits creation for Excel use

Posted on 1997-03-26
3
218 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 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