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

原创 2002年08月12日 11:30:00
Title:       How to create .lib file when you have .dll and .h files onle.
Author:      Zhang Shenggang (China)
Email:       bub_zhang@wistron.com.cn
Environment: VC++ 6.0, Win2000 (note: no testing on Win9x and NT )
Keywords:    dll, def, import library
Level:       Intermediate
Description: An article on Microsoft .lib file and .dll file
Section      Miscellaneous
SubSection   General

Problem

Have you encountered this situation:
you have xxx.dll and xxx.h file, but you havn't 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 eg. @ordinal[NONAME],
entryname[=internalname], [DATA], [PRIVATE].
This article is devoted to this tech. I wish it can help you
Now let's go:

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 in .DEF file.

1. What is [PRIVATE]?

 ;xxx.def
 EXPORTS
  privatefun PRIVATE
It means that:
privatefun is only put into xxx.dll, but the symbol(stub) 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"

2. 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 '_'(undersocre), also, left name is more meaningful.

3. What is [DATA] ?

 ;xxx.def
 EXPORTS
  vcdata DATA
It means that:
vcdata is data, not function. You can use __declspec(dllimport)> to import it.

4. 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 /exports xxx.lib
(dll, obj, etc.) show 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

Others important

If the dll export its functions by ordinal, still you can ...

Simply you set a new name for the ordinal

; VCDLL_VB.def
EXPORTS
 fun3 @333 NONAME

corresponding

; MINIC.def
EXPORTS
 Minicfun3  @333

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

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

when you run Demo.exe, you will get an error dialog below.
Sorry, I'm using Windows 2000 for P.R.C. Simplified Chinese

Sample Image

Some tips

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

About author

Hello, I'm Zhang Shenggang, a master of Mathematics(about PDE).
I'm graduated from Fudan University, Shanghai, China. Currently,
I'm working for Wistron (Shanghai) InfoComm (original Acer, a company in Taiwan).
If you have some problems, please contact with me

Email:bub_zhang@wistron.com.cn

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

1. 原文地址:http://www.codeproject.com/KB/cpp/libfromdll.aspx 这篇讲解了DEF文件地结构,但附件中的工程编译不了。   2. Microso...
  • tangketan
  • tangketan
  • 2011年11月22日 14:19
  • 663

OCP-1Z0-053-V12.02-177题

177.When you create a backup control file, where is the resulting file written to? A. The database ...
  • rlhua
  • rlhua
  • 2013年11月07日 13:36
  • 5565

DLL导出lib文件和.h头文件

最近工作中遇到需要调用别人的动态链接库,但是只有个dll文件,别的一概没有,这可怎么办呢。还有不少dll导出的都是类,直接显式调用不太方便,最后综合网上的办法,自己再写两个工具,终于实现了将dll导出...
  • gyh198
  • gyh198
  • 2012年06月18日 14:16
  • 4481

.h .lib .dll的区别与使用

共有两种库: 一种是LIB包含了函数所在的DLL文件和文件中函数位置的信息(入口),代码由运行时加载在进程空间中的DLL提供,称为动态链接库dynamic linklibrary。 一种是LIB包含...
  • dhasuhf
  • dhasuhf
  • 2014年03月01日 15:23
  • 877

.h, lib和dll文件的功能和区别

简而言之 (1).h是头文件,lib和dll都是库文件。 (2)头文件包含声明,库文件则包含实现。 (3)lib是静态链接时使用的,如果采用静态链接库,则lib包含了实际执行代码和符号表等,无论...
  • chenwk891
  • chenwk891
  • 2013年10月25日 11:44
  • 1478

.h头文件 .lib库文件 .dll动态库文件之间的关系

.h头文件是编译时必须的,lib是链接时需要的,dll是运行时需要的。 附加依赖项的是.lib不是.dll,若生成了DLL,则肯定也生成 LIB文件。如果要完成源代码的编译和链接,有头文件和lib就够...
  • yusiguyuan
  • yusiguyuan
  • 2013年10月12日 11:56
  • 16973

git报错:Pull is not possible because you have unmerged files解决方法

在git pull的过程中,如果有冲突,那么除了冲突的文件之外,其它的文件都会做为staged区的文件保存起来。 重现: $ git pull A    Applications/C...
  • zxsted
  • zxsted
  • 2013年11月29日 12:10
  • 6475

ICPCCamp 2016 Day 2 - ZhejiangU Contest(Easy When You Know How-并查集+rmq)

题意:给一个长度为n的字符串和m个事实,每个事实是xi,yi,lix_i,y_i,l_i表示 sxi,sxi+1,…,sxi+li−1=syi,syi+1,…,syi+li−1s_{x_i},s_{...
  • nike0good
  • nike0good
  • 2016年02月24日 13:01
  • 618

Git:"Pull is not possible because you have unmerged files"

在git pull的过程中,如果有冲突,那么除了冲突的文件之外,其它的文件都会做为staged区的文件保存起来。 重现: $ git pull A    Applications/Commerc...
  • hirohrj123
  • hirohrj123
  • 2013年09月23日 16:13
  • 1603

you need to have ldconfig in your PATH env when enabling luajit

在安装阿里的OpenResty®(通过 Lua 扩展 NGINX 实现的可伸缩的 Web 平台)时出现如下错误: you need to have ldconfig in your PATH env...
  • leehbing
  • leehbing
  • 2017年08月31日 18:26
  • 433
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to create .lib file when you have .dll and .h files onle
举报原因:
原因补充:

(最多只允许输入30个字)