[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

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
0
mconsidine
Asked:
mconsidine
1 Solution
 
jbalagopCommented:
does the DEF file have the name of the function you want to import?
0
 
nietodCommented:
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.)
0
 
mconsidineAuthor Commented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
rwilson032697Commented:
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.
0
 
mconsidineAuthor Commented:
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 ...
0
 
rwilson032697Commented:
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.
0
 
rwilson032697Commented:
Continued...

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

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

Cheers,

Raymond.
0
 
mconsidineAuthor Commented:
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
0

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now