Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 1998-10-25
Medium Priority
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 ..."?

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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 210 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

715 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