分享
 
 
 

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

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

The Code Project

BODY, P, TD { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt }

H2,H3,H4,H5 { color: #ff9900; font-weight: bold; }

H2 { font-size: 13pt; }

H3 { font-size: 12pt; }

H4 { font-size: 10pt; color: black; }

PRE { BACKGROUND-COLOR: #FBEDBB; FONT-FAMILY: "Courier New", Courier, mono; WHITE-SPACE: pre; }

CODE { COLOR: #990000; FONT-FAMILY: "Courier New", Courier, mono; }

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

Download source and demo project - 34.1 Kb

, etc) --

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

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有