Link to home
Start Free TrialLog in
Avatar of mehd
mehd

asked on

Using fonctions of a dll

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.
Avatar of diakov
diakov

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.
ASKER CERTIFIED SOLUTION
Avatar of agreen
agreen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ooopss..
PrintText = (void *) GetProcAddress( MyDll, "PrintText" );

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.
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.
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.
Avatar of mehd

ASKER

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.
Avatar of mehd

ASKER

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.
if you're #include-ing <windows.h> then it should define these types/function prototypes, mehd
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.

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.
Avatar of mehd

ASKER

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.

Try this:

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

..

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

..

  temp = PortIn(0x3f8);
Avatar of mehd

ASKER

Well, I have to write :  

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

to make it compile and link successfully. Do you know why?
 
Avatar of mehd

ASKER

Thank you all for your comments and solution.