
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/