Solved

Using fonctions of a dll

Posted on 1998-08-27
15
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

688 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