Solved

Using fonctions of a dll

Posted on 1998-08-27
15
183 Views
Last Modified: 2010-04-10
Hello,
I want to use functions of a dll that I have not written. When I compile my program, the linker (TLINK32) returns me the following error message :

unreasolved external 'PortIn(Short)' referenced from module portp.cpp.

where PortIn(Short) is the exact prototype of the fonction of the dll I need to use in my program portp.cpp.

I use borland C++ 4.5 and a makefile to compile and link all my files, under windows95. I am looking for any kind of help, because I am in a hurry.
0
Comment
Question by:mehd
  • 5
  • 3
  • 3
  • +2
15 Comments
 
LVL 8

Expert Comment

by:diakov
ID: 1171312
I see you whant to statically link to the .dll library. You have to include a lib file for this .dll library in your project. The .lib file is generated from TLIB.exe, as far as I remember. Check again which utility to use.
You have to define the function call as an external.
The other way is to link to a .dll is to use LoadLibrary and then GetProcAddr to obtain a pointer to the function implementation.

Success.
0
 
LVL 4

Accepted Solution

by:
agreen earned 50 total points
ID: 1171313
For example, the MYDLL.DLL contains function PrintText(int x, int y, char* text):

BOOL (__stdcall *PrintText)(int x, int y, char* text);
HINSTANCE      MyDll;

....

  MyDll = LoadLibrary("MYDLL.DLL");
  PrintText = (void *) GetProcAddress( hInetLib, "PrintText" );

....

  PrintText(0, 0, "Hello World");

0
 
LVL 4

Expert Comment

by:agreen
ID: 1171314
Ooopss..
PrintText = (void *) GetProcAddress( MyDll, "PrintText" );

0
 
LVL 2

Expert Comment

by:warmcat
ID: 1171315
Under VC 4 & 5 at least, when you create your DLL, a .lib file is generated along with the DLL, in the same directory.  This is called an 'import library', because apps that wish to use the DLL can add the import library to their project - just like you add C or C++ files in file view - and gain access to the exported functions in the way you're trying to do.  The only requirement, as I recall, is that the DLL is somewhere in your %PATH% when you execute your app.
0
 
LVL 8

Expert Comment

by:diakov
ID: 1171316
or the dll is in the windows directory, or in the same directory the module loading it is. But the guy is using Borland C++ and the dll is not his. He has to make the .lib file in order to link it. In VC the utility is LIB, I think.
0
 
LVL 2

Expert Comment

by:warmcat
ID: 1171317
doh, diakov, you are quite right about it not being his dll.  My comment was correct but irrelevent.  In that case agreen's method is going to be simpler if the number of exports is reasonably low.
0
 

Author Comment

by:mehd
ID: 1171318
Thanks for all your comments and answer.

To agreen : You tell me to use the fonctions  LoadLibrary and  GetProcAddress and the type HINSTANCE. As my doc is very poor, I do not know which include is needed.  

More generally, I have created a .def file from my dll (with IMPDEF) and then an import library (with IMPLIB). These 3 files are in the directory of my main module. I include the import library on the command line of TLINK32. And it gives me the eroor message I have mentioned in my question. I searched in the doc what options I had missed, but found none.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:mehd
ID: 1171319
Thanks for all your comments and answer.

To agreen : You tell me to use the fonctions  LoadLibrary and  GetProcAddress and the type HINSTANCE. As my doc is very poor, I do not know which include is needed.  

More generally, I have created a .def file from my dll (with IMPDEF) and then an import library (with IMPLIB). These 3 files are in the directory of my main module. I include the import library on the command line of TLINK32. And it gives me the eroor message I have mentioned in my question. I searched in the doc what options I had missed, but found none.
0
 
LVL 2

Expert Comment

by:warmcat
ID: 1171320
if you're #include-ing <windows.h> then it should define these types/function prototypes, mehd
0
 
LVL 1

Expert Comment

by:Bonev
ID: 1171321
There are two (if I don't miss any) options for this kind of problem:
1. Use LoadLibrary/GetProcAddress as agreen proposed.
2. Use static link with generated .LIB file. If you get "unresolved external" error, that means:
a) in case of C++ functions: your compiler is not the same as that used for the building of the library. C++ compilers perform function name mangling in different way.
b) in case of C functions: there is an option for "generating underscores". If your compiler generates, but the other one not, and vice versa, you will get that message.

0
 
LVL 1

Expert Comment

by:Bonev
ID: 1171322
If you are in a hurry, better try agreen's approach, because playing with function prototypes and compiler/linker options will take you more time.
0
 

Author Comment

by:mehd
ID: 1171323
I have two other points to ask you agreen:
1_ What are the meaning and use of BOOL?
2_ Your function does not return anything. If my function return a char, what modification should I do to your model? I tried several ones, none worked.

0
 
LVL 4

Expert Comment

by:agreen
ID: 1171324
Try this:

CHAR (__stdcall *PortIn)(short port);
HINSTANCE MyDll;
CHAR temp;

..

  MyDll = LoadLibrary("MYDLL.DLL");
  PortIn = (void *) GetProcAddress( MyDll, "PortIn" );

..

  temp = PortIn(0x3f8);
0
 

Author Comment

by:mehd
ID: 1171325
Well, I have to write :  

PortIn = (char(__stdcall *)(short)) GetProcAddress( MyDll, "PortIn" );

to make it compile and link successfully. Do you know why?
 
0
 

Author Comment

by:mehd
ID: 1171326
Thank you all for your comments and solution.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

705 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

18 Experts available now in Live!

Get 1:1 Help Now