Solved

Using fonctions of a dll

Posted on 1998-08-27
15
195 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  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 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 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.

910 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

17 Experts available now in Live!

Get 1:1 Help Now