How to import functions from a Delphi DLL into VC++ 5.0

Posted on 1998-10-25
Last Modified: 2010-05-18
I would like to use functions in a DLL (created in Delphi) in
a VC++ 5.0 program.  I *believe* the DLL was created with
'export "C"'.  I have something like the following :
  typedef int (*AFUNCTION)(double, double*);
  AFUNCTION newfuncn;
  glibMyLib = LoadLibrary("thedll.dll");
  newfuncn        =(AFUNCTION)GetProcAddress(glibMyLib,"thefuncn");

Later, when I call this function I will get a page fault error
or something similar.  It also seems that I need to call
FreeLibrary(...) later.  Is there something unusual about
calling a function from a Delphi-created DLL or am I doing
something wrong?  One of these functions is meant to
access the serial port and setup HCOMM.  Am I missing
something here?  An example of the correct way to set
up the calls and use of these functions would be very

Thanks in advance...
Question by:mconsidine
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

Expert Comment

ID: 1176066
does the DEF file have the name of the function you want to import?
LVL 22

Expert Comment

ID: 1176067
Does GetProcAddress() return a NULL pointer?  If so, you are specifing the wrong function name and need to fix that.  If not, you are specifying invalid parameters or calling conventions.  (which can be fixed, but will be harder.)

Author Comment

ID: 1176068
I know the name of the function to import.  I also have checked
the pointer returned by GetProcAddress() and it is not NULL.
So I think my calling conventions are off.  Should I be
using something other than "typedef int ..."?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 12

Expert Comment

ID: 1176069
You are almost certainly using the wrong calling convention. By convention (sic) functions in a DLL (regardless of whether they are writen in C or pascal) use the pascal calling convention. This should be specified in your function prototype.

eg: (you may, or may not, also require an 'extern' here too)
 typedef pascal int (*AFUNCTION)(double, double*);



Author Comment

ID: 1176070
I tried changing the declarations to

typedef pascal int (*AFUNCTION)(int, double*, double*);
AFUNCTION myFunction;

and this would not compile in VC++ 5.0.  I'm not sure where the "extern" would go - I tried a few things which also did not compile.

As I understand it, the following is the .DEF file created by
************************ Start ************************************
library My_LIB;
   function MyFunc1( COM: INTEGER; var L1, L2: DOUBLE):        INTEGER; stdcall;

   procedure MyFunc2( L1, L2, L3, L4: DOUBLE; var R1,
     D1: DOUBLE); stdcall;

   procedure MyFunc3( R1, D1: DOUBLE); stdcall;
   MyFunc1     index 1,
   MyFunc2  index 2,
   MyFunc3     index 3;
**************************** End ************************************

So I'm trying to find the correct way to
  - declare these at the start of the program (because I
        am obviously missing something!)
  -  load the DLL and setup up the pointers to the routines
  -  call the routines
  -  unload the library.

For parts 2 & 4 above I am doing :
    gLibDLL = LoadLibrary("mydll.dll");

These don't seem to be where the problem is ...
LVL 12

Accepted Solution

rwilson032697 earned 70 total points
ID: 1176071
Sorry, it should be __pascal. Also some C/C++ systems use stdcall in place of pascal (to mean the same thing) so you could also try __stdcall.
LVL 12

Expert Comment

ID: 1176072

To get the function use MyFunction = GetProcAddress(Handlem "MyFunction");

To call it do (*MyFunction)(1, 1.0, 1.0)



Author Comment

ID: 1176073
Ok, here's what ended up working :


typedef int (WINAPI *MyFunction1)(int, double*, double*);
MyFunction1 MyFunc1;

typedef void (WINAPI *MyFunction2)(double, double, double, double, double*, double*);
MyFunction2 MyFunc2;

typedef void (WINAPI *MyFunction3)(double, double);
MyFunction3 MyFunc3;

gLibMyDLL = LoadLibrary("mydll.dll");
MyFunc1 = (MyFunction1)GetProcAddress(gLibMyDLL,                                                                      "MY_FUNC1");
MyFunc2= (MyFunction2)GetProcAddress(gLibMyDLL,                                                                      "MY_FUNC2");
MyFunc3=(MyFunction3)GetProcAddress(gLibMyDLL,                                                                      "MY_FUNC3");

The only difference from what I started out with is the
inclusion of WINAPI in the typedef statement and omitting
(AFUNCTION) where the pointer is set up.

I wouldn't have figured that out if I hadn't followed rwilson's
suggestion of __pascal (obsolete now in VC++ 5.0) and then
__stdcall.  At that point the online help clued me into using
WINAPI, though I sort of stumbled on where it should go ...

Thank you all for your help - I don't think I could have solved
the problem faster any other way!!


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

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 …
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 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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

632 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