Solved

DLL 16bits creation for Excel use

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now