Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Linking DLLs without .LIB files

Posted on 2002-04-24
10
Medium Priority
?
599 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

730 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