Solved

Linking DLLs without .LIB files

Posted on 2002-04-24
10
540 Views
Last Modified: 2012-06-27
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
Comment
Question by:nasia
10 Comments
 
LVL 3

Expert Comment

by:MDarling
ID: 6965462
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
 
LVL 1

Expert Comment

by:hide_in
ID: 6967781
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
 

Author Comment

by:nasia
ID: 6968067
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
 
LVL 3

Expert Comment

by:msmits
ID: 6968346
You can also use implib on the DLL to create the lib file to link to your appication.
0
 
LVL 3

Expert Comment

by:MDarling
ID: 6968439
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:nasia
ID: 6970198
sorry if i got you guys confused...
i am building a dll which uses another dll...

0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 6970254
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
 

Author Comment

by:nasia
ID: 6970569
jkr,

it worked! thanks!

mike,

your solution works also...
just forgot to fix some project settings...
0
 

Author Comment

by:nasia
ID: 6970586
mike,

gave the points to jkr... his solution was more favorable in my case. thanks!
0
 
LVL 3

Expert Comment

by:MDarling
ID: 6970732
No problem.  Glad you got it going!

Regards,
Mike.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

762 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

20 Experts available now in Live!

Get 1:1 Help Now