How to create .lib file when you have .dll and .h Files ONLE

zhaozj2021-02-16  49

REL = "stylesheet" type = text / css href = "http://www.codeproject.com/styles/global.css">

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

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 annother yyy.def exports it with the same order,

Moreover, you can indeicate 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 chDLNAME [MAX_PATH];

MEMSET (chDLNAME, 0, SIZEOF (chDLNAME);

GetModuleFileName (AfxGetInstanceHandle (), chDLNAME, MAX_PATH;

AfxMessageBox (chDLNAME);

}

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 order

; 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 know, 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 Sourceabout 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

转载请注明原文地址:https://www.9cbs.com/read-25022.html

New Post(0)