中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Visual C++ > DLL
Howtocreate.libfilewhenyouhave.dlland.hfilesonle
作者:bub_zhang 时间:2001-10-08 10:07 出处:互联网 责编:chinaitpower
              摘要:Howtocreate.libfilewhenyouhave.dlland.hfilesonle
The Code Project
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
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有