Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 745
  • Last Modified:

Linking DLLs without .LIB files

we have a dll which we need to link to our c program... the problem is, we've lost the .lib file that goes with it... we know each function definition, but we are having problems with the linker since we dont have the library file to include in the linker parameter.

is there a way around this? or is there a way wherein we could rebuild the library file from the dll?

i don't know if my question is stupid... but i am fairly new with C.

thanks!
0
nasia
Asked:
nasia
1 Solution
 
MDarlingCommented:
You're question is not stupid.  It is certainly possible.

You can load the DLL using LoadLibrary and then use GetProcAddress to get the function entry points to call.

You can use the M$ Depends program to view the exported names of the functions in your dll.

Regards,
Mike.

0
 
hide_inCommented:
if you know how the function declare, you can do as follows.

suppose there is a export api of the dll as
"int GetMyName(char *pOut, int nsize)", then
you can do as follows:

typedef int (__stdcall *FPGetMyName)(char *, int);

FPGetMyName GetMyName = NULL;
HMODULE hDll = LoadLibrary("your dll file name");
char buf[256] = "";
if (NULL != hDll)
{
  GetMyName = (FPGetMyName)GetProcAddress("GetMyName");
  if (NULL != GetMyName)
  {
     // then , you can call the function
     GetMyName(buf, sizeof(buf) - 1));
  }

  FreeLibrary(hDll);
}
0
 
nasiaAuthor Commented:
Mike,

I did what you said and used LoadLibrary and GetProcAddress...

the problem is when i try to build my dll, i get the following error:

LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/hsmsei.exe : fatal error LNK1120: 1 unresolved externals

please help.
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
msmitsCommented:
You can also use implib on the DLL to create the lib file to link to your appication.
0
 
MDarlingCommented:
What do you mean you're building the DLL?

If you're building the DLL then you can produce a lib file.

(If using M$ VC check "Project Settings|Link Tab" and make sure "Doesn't produce .LIB" is not checked.

If you're building the app that uses the DLL then it looks like it is missing a main function.

Regards,
Mike.

0
 
nasiaAuthor Commented:
sorry if i got you guys confused...
i am building a dll which uses another dll...

0
 
jkrCommented:
What compiler do you have? For VC++, the following should be an option:

Step.1 - Get the list of functions. 'dumpbin.exe /exports thedll.dll' will
display the exports along with any lingering decorations.

Step.2 - Make a .def file for the dll. It should look like:

EXPORTS
DllFunction1
DllFunction2
DllFunction3

Step.3 - use the MSVC LIB.EXE command line tool to make a .lib using only
the .def

C:\Some Path>lib /def:mydef.def

Step.4 - Now, you can link. But you still can't compile. For that you need
the function prototypes. For this you will need to know:

1. The paramters passed to each function.
2. The calling convention used. (For msvc one of _stdcall or _cdecl).

The header file will then contain lines like:

EXTERN_C INT WINAPI ADllFuncThatReturnsAnInt(INT nParam1,VOID* pvPAram2);

"WINAPI" is a neater Win32 way of saying "__stdcall". The "EXTERN_C" ensures
that all C++ decoration is removed from generated function names.

Include the header file in any c/cpp files that want to call the functions
(duh!). Add the .lib to the project.

One last note re: .def exported dll functions. A lib file made this way
probably would not recognise functions exported using __declspec(dllexport).

A .def file exported function is simply named in the dll as: "TheFunction"
__declspec functions are called either "_TheFunction" or "_TheFunction@4" -
the 1st name indicates __cdecl - the 2nd indicates __stdcall (the number
after the '@' indicates the number of bytes that the function cleans up off
of the stack - divide that number by 4 to get the number of parameters).

0
 
nasiaAuthor Commented:
jkr,

it worked! thanks!

mike,

your solution works also...
just forgot to fix some project settings...
0
 
nasiaAuthor Commented:
mike,

gave the points to jkr... his solution was more favorable in my case. thanks!
0
 
MDarlingCommented:
No problem.  Glad you got it going!

Regards,
Mike.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now