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

Expert Comment

Comment Utility
does the DEF file have the name of the function you want to import?
LVL 22

Expert Comment

Comment Utility
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

Comment Utility
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 ..."?

LVL 12

Expert Comment

Comment Utility
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*);


How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline


Author Comment

Comment Utility
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
Comment Utility
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

Comment Utility

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

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



Author Comment

Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

12 Experts available now in Live!

Get 1:1 Help Now