Solved

Function can't locate DLL function

Posted on 2000-03-06
8
156 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

  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 …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

929 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now