Link to home
Start Free TrialLog in
Avatar of s_more
s_more

asked on

Calling function from lib files - no header files available

I've a function:

extern void  bld_min_cat(int);

that I would like to use in my C code from shared lib files.
I've path to shared lib files.

No header files are available - how to call this function from my code.
I'm using MS Visual Studio 2005.

I'm getting a "error LNK2019: unresolved external symbol" error.
Avatar of Infinity08
Infinity08
Flag of Belgium image

If the dll is properly installed on the system, you can simply add this prototype to the code where the function is used :

        extern void  bld_min_cat(int);

When linking, make sure to link against the .lib file that came with the dll.
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

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
Avatar of s_more
s_more

ASKER

I've this prototype in my code but I still get link error.

I'm linking .lib files that came with dll.

I added lib files using:

Tools->Options->Project and Solutions->VC++ Directories->Directories For Library Files
You'll need to link to the .lib in your project settings (linker settings) - ie. specify the .lib file, not just the directory it's in.

>> I'm linking .lib files

'files' plural ? There's usually just one (very small) .lib file for a dll - probably with the same name. It actually takes care of dynamically loading the dll for you by simply statically linking to it.
Avatar of s_more

ASKER

Infinity08,

I've dll's not lib file (I was under impression that I had lib files) that I need to add to use this function.
Where shall I include these dll's?

>> I've dll's not lib file (I was under impression that I had lib files) that I need to add to use this function.

Where does this DLL come from ? Did you build it yourself ? Download it from somewhere ?
In any case, you should be able to get the appropriate header files and .lib file from the source where you got it.

What dll is it ? (its name)
Avatar of s_more

ASKER

These are third  party applications that I'm trying to use function from.
Only information I get from documentation is:

*~*~*~*~*~*

The following shared libraries must be referenced when linking an executable that uses the function calls:
•      libpdl_nx.dll
•      libpdl_std_c.dll
•      gnu_regex.dll
•      libpdl_nx_int.dll

*~*~*~*~*~*~*

I searched the package and there are no *.h or *.lib files
I don't think I know this library, so I can't give you specific help.

Here's a general description of how to link to dlls from a Visual C++ project though :

        "Linking an Executable to a DLL" : http://msdn.microsoft.com/en-us/library/9yd93633(VS.71).aspx

If you cannot obtain the .lib file from the creators of the dll, consider using explicit linking (refer to the link above).
if you have only dll but not lib file you cannot call the function directly but have to go the way via LoadLibrary and GetProcAddress which already was mentioned above. but at least you need the prototype of the function you want to call. normally that information is given in a header file. most dlls already have a lib which has wrapper functions with same name as those of teh dll. then you can link against that lib and the linker would find the function in the wrapper lib. at runtime the wrapper function would call LoadLibrary and GetProcAdress and call the dll function via function pointer.

if you have no lib you could write the wrapper function yourself and add it to your project. the dllname should be the full name of the dll and the directory where the dll is in should be in PATH environment variable.




typedef void (*BLD_MIN_CAT_FPT)(int);
void  bld_min_cat(int i)
{
   static HMODULE hmod = LoadLibrary(dllname);
   if (hmod == NULL) return;
   BLD_MIN_CAT_FPT fp = GetProcAddress(hmod, "bld_min_cat");
   if (fp == NULL) return;
   fp(i);  // call function

}

Open in new window

I wouldn't call LoadLibrary every time you call the function ... It should be called only once (before the first call to the function). The same is true for GetProcAddress.

Also, don't forget to call FreeLibrary when you don't need the dll any more.
the LoadLibrary was called once because of static. the function pointer can be made static as well if performance is an issue. unloading the library only makes sense if the function wasnt needed all time.  for example when the function was called only in an initialisation part of your prog - then the LoadLibrary should be made in a separate function and the hmod a shared (global) variable. that way you can provide both load and unload functions with your wrapper.
typedef void (*BLD_MIN_CAT_FPT)(int); 
static HMODULE hmod = NULL;
BLD_MIN_CAT_FPT fp  = NULL;

void load_dll()
{
    if (hmod != NULL) return;
    hmod = LoadLibrary(dllname);  
    if (hmod != NULL)
    {
      fp = (BLD_MIN_CAT_FPT)GetProcAddress(hmod, "bld_min_cat"); 
      if (fp == NULL)
      {
          unload_dll();
      }
    }
}

void unload_dll()
{
    if (hmod == NULL) return;
    FreeLibrary(hmod);  
    hmod = NULL;
    fp   = NULL;
}

void  bld_min_cat(int i) 
{
   if (hmod == NULL)
       load_dll();
   if (fp == NULL) return; 
   fp(i);  // call function 
}

Open in new window

It remains an unnecessary overhead - if only for the checks you need to do every time. It also becomes difficult/tedious to manage if you need to use more than just that one function from the dll.
you may look for a .lib file for your dll. the above code is a workaround not a recommendation. on the other hand a few more functions wouldnt make the workaround more complex. only bigger.