Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / C++
Article

How to create .lib file when you only have .dll and .h files

Rate me:
Please Sign up or sign in to vote.
4.54/5 (17 votes)
12 Aug 20022 min read 222.1K   2K   82   12
An article on Microsoft .lib file and .dll file

The Problem

Have you encountered this situation: you have xxx.dll and xxx.h file, but you do not have xxx.lib, while you don't want to use LoadLibrary("xxx.dll"); you want to implicitly link xxx.lib, then you can call  the functions in the xxx.dll smoothly. Also this article illustrates some concepts about .DEF file e.g. @ordinal[NONAME], entryname[=internalname], [DATA], [PRIVATE]. This article is devoted to these topics. I wish it can help you. Now let's go.

DEF Files

Before we go, I will show something about .DEF. The syntax for an export definition is:

entryname[=internalname] [@ordinal[NONAME]] [DATA] [PRIVATE]

You can refer to my source code which illustrates how to use that in .DEF files.

What is [PRIVATE]?

;xxx.def
EXPORTS
    privatefun PRIVATE

It means that: privatefun is only put into xxx.dll, but the symbol (stub) is not corresponding xxx.lib. So, when you implicitly link your exe with xxx.lib, if you call privatefun(); you will get LNK2001 : unresolved external symbol "symbol"

What is entryname[=internalname] ?

;xxx.def
EXPORTS
    LIBcdeclfun = cdeclfun

It means that: LIBcdeclfun is an alias of cdeclfun, note that Visual Basic can't accept '_' (underscore), also, left name is more meaningful.

What is [DATA] ?

;xxx.def
EXPORTS
    vcdata DATA

It means that: vcdata is data, not function. You can use __declspec(dllimport) to import it.

What is [@ordinal[NONAME]] ?

;xxx.def
EXPORTS
    fun3 @333 NONAME

It means that: fun3 only exports with ordinal, not function name. But you can in another yyy.def exports it with the same ordinal, moreover, you can indicate a function name for this ordinal:

;xxx.def
EXPORTS
    Minicfun3 @333

Note : You can use "\VC98\Bin\dumpbin.exe /exports xxx.lib" (or dll, obj, etc.) to see the export section in PE file.

How to do it?

There are 3 projects in INIT workspace, "Demo", "MINIC", "VCDLL_VB" respectively. "Demo.exe" depends on "MINIC.lib" and "VCDLL_VB.dll".

// a piece of VCDLL_VB.cpp
extern "C" void cdeclfun()
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    CString str;
    str.LoadString(IDS_STRING1);
    AfxMessageBox(str);

}
extern "C" void __stdcall fun()
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    TCHAR chDLLName[MAX_PATH];
    memset(chDLLName,0,sizeof(chDLLName));
    GetModuleFileName(AfxGetInstanceHandle(),
        chDLLName,MAX_PATH);
    AfxMessageBox(chDLLName);
}
extern "C" int __stdcall fun2()
{
    return 3;
}
extern "C" long __stdcall fun3(long a)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    CString str;
    str.LoadString(IDS_STRING2);
    return (long)AfxMessageBox(str);
}
extern "C" void __stdcall privatefun()
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    AfxMessageBox(_T("call into VCDLL_VB!privatefun"));
}
int vcdata=12345;
; VCDLL_VB.def : Declares the module parameters for the DLL.

LIBRARY      "VCDLL_VB"
DESCRIPTION  'VCDLL_VB Windows Dynamic Link Library'

EXPORTS
    fun
    fun2
    fun3    @333 NONAME
    LIBcdeclfun=cdeclfun
    vcdata DATA
    privatefun  PRIVATE
// a piece of MINIC.cpp
extern "C" void LIBcdeclfun()
{
}
extern "C" void __stdcall fun()
{
}
extern "C" int __stdcall fun2()
{
    return 3;
}
extern "C" long __stdcall Minicfun3(long a)
{
    return a;
}
extern "C" void __stdcall Minic(int b)
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    AfxMessageBox(_T("CString in MINIC"));
}
int vcdata=6789;
; MINIC.def : Declares the module parameters for the DLL.

LIBRARY      "VCDLL_VB"
DESCRIPTION  'MINIC Windows Dynamic Link Library'

EXPORTS
    ; Explicit exports can go here
    fun
    fun2
    Minicfun3   @333
    vcdata DATA
    LIBcdeclfun
    Minic

Other important stuff

If the dll export its functions by ordinal, still you can call it. Simply you set a new name for the ordinal

; VCDLL_VB.def
EXPORTS
    fun3 @333 NONAME

and a corresponding  one as in

; MINIC.def
EXPORTS
    Minicfun3 @333

In addition, you can export your function. The compiler and linker don't complain, but the Operating System's loader will complain.

; MINIC.def
EXPORTS
    Minic ;This function isn't existing in original .dll

when you run Demo.exe, you will get an error dialog below, if you uncomment the lines as suggested in the source code.

Image 1

Conclusion

As you known, MFCxxx.dll exports its functions by ordinal, which can save much space. There is an article in MSDN about Q131313 HOWTO: Create 32-bit Import Libraries Without .OBJs or Source. You can email me at  bub_zhang@wistron.com.cn

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Oracle2k16-Jan-12 12:29
Oracle2k16-Jan-12 12:29 
GeneralAutomatic method with "dumpbin", "lib", "sed" and "nmake" Pin
Holy Av20-Aug-10 11:59
Holy Av20-Aug-10 11:59 
GeneralRe: Automatic method with "dumpbin", "lib", "sed" and "nmake" Pin
TRK8-Dec-11 22:17
TRK8-Dec-11 22:17 
GeneralSeems a bit confusing... Pin
Like2Byte18-Feb-10 7:50
Like2Byte18-Feb-10 7:50 
Generallib Files Pin
Andre Trollip31-Oct-05 0:22
Andre Trollip31-Oct-05 0:22 
GeneralUseless Pin
Alan Malan18-Oct-05 10:31
Alan Malan18-Oct-05 10:31 
GeneralRe: Useless Pin
BBurgess26-Mar-06 18:19
BBurgess26-Mar-06 18:19 
GeneralRe: Useless Pin
Anthony Steer20-May-08 20:17
Anthony Steer20-May-08 20:17 
QuestionBut how do you make the .lib file? Pin
bsonnenberg10-Jan-03 6:51
bsonnenberg10-Jan-03 6:51 
AnswerRe: But how do you make the .lib file? Pin
Hans Dietrich10-Jan-03 7:36
mentorHans Dietrich10-Jan-03 7:36 
Yes, this article does leave out some steps. But if you look at KB article
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B131313
it will become clear.

Best wishes,
Hans
QuestionAre you a chinese? Pin
Anonymous21-Oct-02 20:08
Anonymous21-Oct-02 20:08 
GeneralVery Good!! Pin
WREY13-Aug-02 12:47
WREY13-Aug-02 12:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.