Link to home
Start Free TrialLog in
Avatar of mconsidine
mconsidine

asked on

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

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 :
  HINSTANCE glibMyLib = NULL;
  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
helpful

Thanks in advance...
Matt
Avatar of jbalagop
jbalagop

does the DEF file have the name of the function you want to import?
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.)
Avatar of mconsidine

ASKER

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

Regards,
Matt
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*);

Cheers,

Raymond.
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
Delphi:
************************ Start ************************************
library My_LIB;
   function MyFunc1( COM: INTEGER; var L1, L2: DOUBLE):        INTEGER; stdcall;
   begin
   end;

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

   procedure MyFunc3( R1, D1: DOUBLE); stdcall;
   begin
   end;
exports
   MyFunc1     index 1,
   MyFunc2  index 2,
   MyFunc3     index 3;
begin
end.
**************************** 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");
and
    FreeLibrary(gLibDLL);

These don't seem to be where the problem is ...
ASKER CERTIFIED SOLUTION
Avatar of rwilson032697
rwilson032697

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Continued...

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

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

Cheers,

Raymond.
Ok, here's what ended up working :

HINSTANCE      gLibMyDLL = NULL;

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

Regards,
Matt