• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

Function can't locate DLL function

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
edmonds101598
Asked:
edmonds101598
1 Solution
 
msmitsCommented:
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
 
nietodCommented:
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
 
nietodCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
inprasCommented:
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
 
nietodCommented:
>> 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
 
AlexVirochovskyCommented:
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
 
edmonds101598Author Commented:
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
 
nietodCommented:
>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now