August 22, 2011

magpielover magpielover
Lab Rat
17 posts

[SOLVED]Cannot resolve the symbols from a custom dll

 

Hello,
I am very close to tear all my hairs out. I have been trying to resolve the functions from my custom dll but so far, no luck.

Here is the template I use:

  1. QLibrary library("E:/QT_works/myDll.dll");
  2.  
  3. if(library.load())
  4.     qDebug()<<"Loading done";
  5.  
  6.  
  7.  typedef void (*MyPrototype)();
  8.  MyPrototype myFunction = (MyPrototype) myLib.resolve("myFunction");
  9.  if (myFunction)
  10.      myFunction();

Loading is successful. However, symbols cannot be resolved.
I used debugger and after the myLib.resolve step, the following error appears on the window where name and values are shown;

errorString “Cannot resolve symbol “myFunction” in E:/QT_works/myDll.dll: The specified procedure could not be found.” QString

I also used

  1. HINSTANCE hDLLFeX;
  2.               hDLLFeX=LoadLibrary(L"myDll.dll");
  3.               if(hDLLFeX)
  4.                   qDebug()<<"LOAD SUCCESSFUL";
  5.  
  6.              MyPrototype myFunction = (MyPrototype)  GetProcAddress(hDLLFeX, "myFunction");

Both return a null pointer.
It’s obvious that I cannot find the function declared in the library source file, but I guess I used correct syntax when declaring my function. What I used was

  1. extern "C" {
  2. ...
  3. void myFunction(int,int)
  4.  
  5. }

It’s good to mention that the same dll was used by someone else earlier in VS 6.0.

I also downloaded DependencyWalker and check the names of the functions inside myDll.dll. I noticed something which seemed to be interesting. The name of the function which dependencywalker shows is _Z8myFunctionPd, _Z8 in front and Pd at the end of the function. I guess it shouldn’t be a problem.
I am not sure if I should resolve the symbols by the name shown in dependencywalker or the original name declared in extern “C”.

I am hoping someone might be able to help me.

Thanks

8 replies

August 22, 2011

Gerolf Gerolf
Robot Herder
3287 posts

Hi magpielover,

I see no extort statement, so where do you export the function from the dll? Which compiler was used for creation of the dll?

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 22, 2011

magpielover magpielover
Lab Rat
17 posts

I am using the below in the common API.h file.

  1. #ifndef FEX_API_H
  2. #define FEX_API_H
  3.  
  4. #ifdef WIN32
  5. #ifndef DLLEXPORT
  6. #define DLLEXPORT __declspec(dllexport)
  7. #endif
  8. #endif
  9.  
  10. extern "C" {
  11.  
  12. ...
  13. ....
  14. typedef int DLLEXPORT (*MyPrototype )(double*);
  15.  
  16. }

August 22, 2011

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

You could use nm or dumpbin to see if your symbols are exported correctly.

August 22, 2011

gedd gedd
Lab Rat
36 posts

You can also try depends
Depends [dependencywalker.com]

 Signature 

Gedd

August 22, 2011

cincirin cincirin
Ant Farmer
462 posts

with

  1.  typedef int DLLEXPORT (*MyPrototype )(double*)
you export a defined type (function pointer), not a real function.

August 22, 2011

Gerolf Gerolf
Robot Herder
3287 posts

you would need the following:

  1. #ifndef FEX_API_H
  2. #define FEX_API_H
  3.  
  4. #ifdef WIN32
  5. #    ifndef DLLEXPORT
  6. #        if CREATE_DLL
  7. #            define MYDLLAPI __declspec(dllexport)
  8. #        else
  9. #            define MYDLLAPI __declspec(dllimport)
  10. #        endif
  11. #    endif
  12. #endif
  13.  
  14. extern "C" {
  15.     ...
  16.     void MYDLLAPI myFunction(int,int);
  17.  
  18. }

CREATE_DLL would be a macro defined inside your dll project but nowhere else.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 22, 2011

magpielover magpielover
Lab Rat
17 posts

Hello,
I used identifiers in Qlibrary template so sorry for the mistake I made. I have found the solution.
I had directly converted the code from VS 6.0 so .def file is used to add the export directive to the object file. However, in the newer versions of compilers such as MinGW and VS 11, __declspec(dllexport) is used to add the export directive automatically. I just added declared the functions with __declspec(dllexport) keyword and problem is solved.

This was enough.

  1. #ifdef WIN32
  2. #ifdef FEX_CNBCDLL
  3. #define DLLEXPORT __declspec(dllexport)
  4. #else
  5. #define DLLEXPORT __declspec(dllimport)
  6. #endif
  7. #else
  8. #define DLLEXPORT
  9. #endif

Thank you all guys. I really appreciate the helpful comments.

August 23, 2011

mlong mlong
Robot Herder
1551 posts

Be sure and add [Solved] to the beginning of the thread title if the issue is solved. Thanks!

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

 
  ‹‹ Why it can debug but i couldn’t run it ...      Linking SMFL Library to Qt on Windows machine ››

You must log in to post a reply. Not a member yet? Register here!