Solved

Using fonctions of a dll

Posted on 1998-08-27
15
203 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

786 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