Linking against exported functions in a DLL 
Author Message
 Linking against exported functions in a DLL

Hi folks,

  I'm sure this question has been answered 100
times over, but I'm having some problems so here
it goes again...

  I'm trying to write some extensions to PHP
using Visual C++ 6.0.  I've written some simple
methods, compiled them into a DLL, and
successfully installed and run them from within
PHP.  Now I'm trying to something a little more
advanced and I need to call some existing methods
within the DLLs that are distributed with PHP.
In this one example, I'm trying to call a method
php_body_write in the DLL php4ts.dll.  I've run
dumplib.exe on php4ts.dll and verified that it
actually exports the symbol php_body_write.  When
I compile and link my code, I get the classic
Link2001 error, saying it can't resolve the
symbol _php_body_write.

  So here's the basic question:  How do I tell
the linker that a specific symbol can be found in
an already existing DLL?  Using Borland's linker
I would create a .DEF file and insert an IMPORT
line which said something like

_php_body_write = php4ts.php_body_write

I've tried similar things in VC++ but it seems
that the IMPORT tag is not supported.  I've also
tried adding the DLL to the project, which
doesn't work.  I've tried making a second project
which just contains php4ts.dll and setting
dependencies.  I've also tried creating a .LIB
out of the php4ts.dll and adding that lib to list
of libraries that the linker uses.

That's about where I am now.  Please let me know
if you've run into this before and how you've
solved it.  Thanks very much in advance.

  Russ

Sent via Deja.com
http://www.*-*-*.com/



Sat, 19 Jul 2003 07:40:40 GMT  
 Linking against exported functions in a DLL
Since you don't have an export library, you need to load your dll
explicitly (with LoadLib) and then get the function (GetProcAddress)
into a func pointer. After that you can use it.


Sat, 19 Jul 2003 08:59:55 GMT  
 Linking against exported functions in a DLL
Greets,

    From your description of the problem, it sounds as if you have built
this DLL with a different vendor's compiler (such as Borland) and while your
exports are missing the leading underscore, the calling convention is
causing Visual C++ to want to prepend this and then look for the export.
You actually have two choices.  The other individual responding to your post
suggested LoadLibrary() and GetProcAddress().  This is one way to accomplish
what you need.

    The other, is a little more tricky, but easily accomplished as well.
Create a .DEF file with the exports from your DLL aliased specifically to
the values that Visual C++ wants.  (BTW, you can also do this with options
passed to the linker, but .DEF files are much more manageable and out in the
open.)

    Use the DUMPBIN (or Dependency Walker) utility to get a list of all the
exported symbol names and ordinal values, then create a .DEF file that looks
similar to the following:

LIBRARY MYDLL.DLL
EXPORTS

    Of course, the above is a contrived example.  You would use the
underscore prepended names Visual 'C' expects on the left hand side and the
names as they will be aliased (or exported from the DLL) on the right.  When
you have all the symbols you want in your .DEF file, use the VC++ LIB
utility to create an import library from the .DEF file and add it to your
project.  Note, be sure the calling conventions between your VC++ client
application and the DLL into which you are calling are the same.

Regards,

Joe


Quote:
> Hi folks,

>   I'm sure this question has been answered 100
> times over, but I'm having some problems so here
> it goes again...

>   I'm trying to write some extensions to PHP
> using Visual C++ 6.0.  I've written some simple
> methods, compiled them into a DLL, and
> successfully installed and run them from within
> PHP.  Now I'm trying to something a little more
> advanced and I need to call some existing methods
> within the DLLs that are distributed with PHP.
> In this one example, I'm trying to call a method
> php_body_write in the DLL php4ts.dll.  I've run
> dumplib.exe on php4ts.dll and verified that it
> actually exports the symbol php_body_write.  When
> I compile and link my code, I get the classic
> Link2001 error, saying it can't resolve the
> symbol _php_body_write.

>   So here's the basic question:  How do I tell
> the linker that a specific symbol can be found in
> an already existing DLL?  Using Borland's linker
> I would create a .DEF file and insert an IMPORT
> line which said something like

> _php_body_write = php4ts.php_body_write

> I've tried similar things in VC++ but it seems
> that the IMPORT tag is not supported.  I've also
> tried adding the DLL to the project, which
> doesn't work.  I've tried making a second project
> which just contains php4ts.dll and setting
> dependencies.  I've also tried creating a .LIB
> out of the php4ts.dll and adding that lib to list
> of libraries that the linker uses.

> That's about where I am now.  Please let me know
> if you've run into this before and how you've
> solved it.  Thanks very much in advance.

>   Russ

> Sent via Deja.com
> http://www.deja.com/



Sat, 19 Jul 2003 09:53:53 GMT  
 Linking against exported functions in a DLL
Joe -
  That worked beautifully!  Thanks so much!  I am once again happily
coding along.

  Russ



Quote:
> Greets,

>     From your description of the problem, it sounds as if you have
built
> this DLL with a different vendor's compiler (such as Borland) and
while your
> exports are missing the leading underscore, the calling convention is
> causing Visual C++ to want to prepend this and then look for the
export.
> You actually have two choices.  The other individual responding to
your post
> suggested LoadLibrary() and GetProcAddress().  This is one way to
accomplish
> what you need.

>     The other, is a little more tricky, but easily accomplished as
well.
> Create a .DEF file with the exports from your DLL aliased
specifically to
> the values that Visual C++ wants.  (BTW, you can also do this with
options
> passed to the linker, but .DEF files are much more manageable and out
in the
> open.)

>     Use the DUMPBIN (or Dependency Walker) utility to get a list of
all the
> exported symbol names and ordinal values, then create a .DEF file
that looks
> similar to the following:

> LIBRARY MYDLL.DLL
> EXPORTS

>     Of course, the above is a contrived example.  You would use the
> underscore prepended names Visual 'C' expects on the left hand side
and the
> names as they will be aliased (or exported from the DLL) on the
right.  When
> you have all the symbols you want in your .DEF file, use the VC++ LIB
> utility to create an import library from the .DEF file and add it to
your
> project.  Note, be sure the calling conventions between your VC++
client
> application and the DLL into which you are calling are the same.

> Regards,

> Joe



> > Hi folks,

> >   I'm sure this question has been answered 100
> > times over, but I'm having some problems so here
> > it goes again...

> >   I'm trying to write some extensions to PHP
> > using Visual C++ 6.0.  I've written some simple
> > methods, compiled them into a DLL, and
> > successfully installed and run them from within
> > PHP.  Now I'm trying to something a little more
> > advanced and I need to call some existing methods
> > within the DLLs that are distributed with PHP.
> > In this one example, I'm trying to call a method
> > php_body_write in the DLL php4ts.dll.  I've run
> > dumplib.exe on php4ts.dll and verified that it
> > actually exports the symbol php_body_write.  When
> > I compile and link my code, I get the classic
> > Link2001 error, saying it can't resolve the
> > symbol _php_body_write.

> >   So here's the basic question:  How do I tell
> > the linker that a specific symbol can be found in
> > an already existing DLL?  Using Borland's linker
> > I would create a .DEF file and insert an IMPORT
> > line which said something like

> > _php_body_write = php4ts.php_body_write

> > I've tried similar things in VC++ but it seems
> > that the IMPORT tag is not supported.  I've also
> > tried adding the DLL to the project, which
> > doesn't work.  I've tried making a second project
> > which just contains php4ts.dll and setting
> > dependencies.  I've also tried creating a .LIB
> > out of the php4ts.dll and adding that lib to list
> > of libraries that the linker uses.

> > That's about where I am now.  Please let me know
> > if you've run into this before and how you've
> > solved it.  Thanks very much in advance.

> >   Russ

> > Sent via Deja.com
> > http://www.deja.com/

Sent via Deja.com
http://www.deja.com/


Sat, 19 Jul 2003 10:54:13 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Linking error in calling a function exporting from a DLL

2. LINK.EXE linked to missing export MSDIS100.DLL

3. Exporting a C function from a DLL that exports C++ classes

4. Calling an exported function of a dll which are exported Stdcall calling convention

5. Regular DLL -VS- MFC Extension DLL and exporting functions

6. exporting functions from a static link library ?

7. C++ 4.0 doesn't work like C++ 2.0 for function exports and explicit linking

8. DLL Error - File is linked to missing export BSPTEST.EXE:_EdrCenterTextA@12

9. DLL can link export but can't find it to load

10. Common.exe linked to missing export kernel32.dll:InterlockedCompareExchange()

11. exported classes within a DLL with dynamical linked MFC

12. Link to missing export MFC42.DLL:6571

 

 
Powered by phpBB® Forum Software