Solved

Function can't locate DLL function

Posted on 2000-03-06
8
157 Views
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.
0
Comment
Question by:edmonds101598
8 Comments
 
LVL 3

Expert Comment

by:msmits
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()...
0
 
LVL 22

Expert Comment

by:nietod
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.
0
 
LVL 22

Accepted Solution

by:
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

EXPORTS
   AddImage
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 4

Expert Comment

by:inpras
ID: 2591283
Hi
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
0
 
LVL 22

Expert Comment

by:nietod
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.
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
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.
0
 

Author Comment

by:edmonds101598
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?
RE    
0
 
LVL 22

Expert Comment

by:nietod
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

EXPORTS
  _function1@12=function1

(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

EXPORTS
 function1

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.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 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