Function can't locate DLL function

Posted on 2000-03-06
Last Modified: 2010-04-02
Why doesn't this calling function successfully invoke this DLL?

The calling function:

  Void Foo() {
     typedef void (*MYPROC)(double, double, unsigned char*, unsigned char*,
     unsigned char*);
     HINSTANCE hinstLib;
     MYPROC ProcAdd;
     hinstLib = LoadLibrary("E:\\Source\\C\\ImageAdd");
     ProcAdd = (MYPROC) GetProcAddress(hinstLib, "AddImage");
     (ProcAdd) (DGain, DThreshold, CGray, CColor, COutput);

The DLL:
  extern "C" __declspec(dllexport) void AddImage(double DGain, double DThreshold, unsigned char* CGray, unsigned char* CColor, unsigned char* COutput);
  extern "C" __declspec(dllexport) int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*);
  int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)  {
     return 1;
  void AddImage(double DGain, double DThreshold, unsigned char* CGray, unsigned char* CColor, unsigned char* COutput) {

We compiled in Borland CBuilder4.  It appears that the "GetProcAddress" function does not successfully return a pointer to  the "AddImage" function in the DLL, leaving "ProcAdd" with a null value.  Is something wrong with the DLL?

Robert Edmonds, Jr.
Question by:edmonds101598
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: 2589626
The rules tell you to always check the result of you function calls. You should check the hinstLib and ProcAdd after you call the functions to give them values.

It may be that you forgot to add the '.dll' extension in your call to LoadLibrary()...
LVL 22

Expert Comment

ID: 2589759
First check that the DLL exports functions with the right names.  The extern "C" will remove the C++ name decoration, but it might not remove the "standard call" decoration.  This is an "@" followed by some digits that express the size of the procedure's parameters.  If that is the problem, you will need to use a module defintion file (.def) to remove the standard call decoration.  Note you can use dumpbin.exe to view the exports from the DLL.
LVL 22

Accepted Solution

nietod earned 600 total points
ID: 2589767
If you don't have dumpbin, you can use quickview to see the exports.  Just right click on the DLL and in the explorrer and choose "quick view".  This will show lots of info about the DLL including the names of all the exported functions.  if the AddImage function name ends in "@xxxx" your problem is the standard call decoration.  You can then just specify this decoration as part of the name specified to GetProcAddress() or you can use a module definition file to remove the decoration.  

To remove the decoration with the module definition file you would create a text file with the name of the library and a .DEF extension and add it to the project, the contents would be like

LIBRARY Libraryname

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 2591283
U don't need a .def file if the function type is extern "C".
if this E:\\resource\\C.... is a network path then I think that is the problem
copy it to UR local disk and try again
check for return value of Load library.
if this all doen't make success then try loading it statically (just to check whether the exe is recognizing the dll or not).

Hope this helps
LVL 22

Expert Comment

ID: 2591453
>> U don't need a .def file if the function
>> type is extern "C".
It depends on the compiler/linker and the calling convetion, but in many cases you do need the .def.  Read why.

>> if this E:\\resource\\C.... is a network path
>> then I think that is the problem
Even if it is a network path it isn't a problem.
LVL 14

Expert Comment

ID: 2591752
edmonds, in what compliler are you made
DLL? BCB don't like VC Dll: must special metod, as nietod wrote. Better make it with BC , make LIB file,
add in you project and you haven't problems.

Author Comment

ID: 2592967
Nietod hit it on the head.   Although Quickview was inexplicably not installed on my machine, when I went across the hall to try it I discovered immediately that, even with extern "C", something somewhere in the process was appending a "_" to the front of the function names, for no apparent reason other than to delay us a day and a half.  Quickview is a definite project-saver, we'll use it to check every DLL we create.  One question about the .def files, if we employ one, should the specified function name then be viewable in quickview?
LVL 22

Expert Comment

ID: 2593047
>> something somewhere in the process was
>> appending a "_" to the front of the function names
Forgot about that.  That is also part of the standard call decoration.  I'm surprised you didn't see a @xxx at the end to.  But all this varies from compiler to compiler.

>> no apparent reason other than to
>> delay us a day
Also to make it hard to use the competition's compilers and tools.

Actually I hav no idea why this decoration is done.  There may be a good reason.  I can't image what, althought the @xxx decoration protects you to some extang from linking to code that has differenent parameters.

>> One question about the .def files, if we
>> employ one, should the specified function
>> name then be viewable in quickview?
Yes it will still be viewable.  The .def file can be used to rename the name of a exported function.  ie. if a function was exported as "_function1@12".  you can change the name to just "function1" using


(The name you change it to could be anything, just so long as it is unique-actually, that might not even be required.  So you could use it to change the name to function2 if you wanted.)   Now that is how the docs say to use it.  However, it seems that in many cases (this is linker specifc) you only need to have the name without decoration to get it exported without decoration, like


is enough to remove the decoration.  

Also VC has a linker command-line option (/export) that allows you to do this without using a .def file.  BCB 4 may also have this same option.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

630 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