?
Solved

DLL 16bits creation for Excel use

Posted on 1997-03-26
3
Medium Priority
?
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 150 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
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.
Suggested Courses

762 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