Have you ever wondered to know what are the things a library (.Lib) is exporting? Sometimes, a library can export hidden functions that are not given to you on a header file, or did you ever wanted to verify the function syntax against the header files, given to you with a LIB file? If could be a mismatch. In that case, here is the solution for you.
This simple tool can go through the LIB file and show the functions it exports, and it can generate a header (.h) file based on the finding. It is much useful for Visual C++ developers. There are some limitations, for that see under the heading "Limitations".
You could have seen some junk characters in place of any C++ function. Somewhere like in Depends (a tool that comes with the Visual Studio, showing the functions inside a DLL). This is called "Name mangling". The function in the junk format is called "Decorated name". C++ compilers encode the names of symbols in C++ programs to include type information in the name. So later, the linker can check the exact function and ensure type-safe linking. C compilers don't need this since only one function can exist in the entire executable linkage. However, in C++, there can exist several functions with the same name in different classes and with different parameters. Therefore, C++ needs this to resolve the function names.
In here, I'm exploiting that feature of the compiler. What the tool does here is gather the decorated names in the library (.Lib) file and decode into undecorated names. This undecorated form is the form that exists in our normal header files.
Microsoft™ Visual C++ compiler encodes the names of symbols in C++ programs in this fashion. This is not published by Microsoft™. It is one of their compiler secrets. However, in some cases, you will need the undecorated form. For those purposes, Microsoft™ has supplied a few APIs to decode. That comes in "Debug Help Library" (DbgHelp.DLL).
C++ decorated symbols starts with a question '
?' mark. On processing the LIB, if you gather the symbols starting with '
?' and ending with the
NULL character, you can get the decorated name in whole. Feed this to the
UnDecorateSymbolName function, a function in the "Debug Help Library", and you will get the undecorated name. That is it. For example, if you feed:
you will get:
<PRE lang=mc++>int __cdecl MyFunc(char)
C symbols are not like their counterpart C++. It always starts with "
__imp__" mark in front of an exported function. It doesn't provide parameter type specification. It provides only the number of bytes in the parameters after the '
@' mark. For example, the function:
<PRE lang=mc++>int __stdcall func (int a, double b)
will look like this after encoding:
Therefore, in C LIBs, it is not possible to get the header files out. Here only the function names can be retrieved, i.e., by gathering "
__imp__" mark to the '
@' character. That's what the tool does.
Here I have encapsulated the LIB file symbol extraction in to a class called
CLibContent. When passed with the LIB file path, it extracts and stores the decorated and undecorated names in an array of strings which can be later retrieved. Here is the code that depicts the core extraction:
HANDLE hLibFile = CreateFile("C:\Libfiles\adme.lib",
nRet = ReadFile(hLibFile,&szChar,32,&dwBytes,NULL);
if((szChar == '?')||(bCollect))
szBuff[dwBuffPos] = szChar;
bCollect = true;
dwBytes = strlen(szChar);
nRet = ExtractCSymbol(szChar,dwBytes);
i += dwBytes;
if((szChar == 0)&&(bCollect))
szBuff[dwBuffPos] = szChar;
nRet = ExtractCppSymbol(szBuff, dwBuffPos);
dwBuffPos = 0;
bCollect = false;
The following function decodes the CPP style decorated names:
int CLibContent::ExtractCppSymbol(char *szDecoratedName, DWORD dwLen)
nRet = UnDecorateSymbolName(szDecoratedName, szFunc, 512,
This following function decodes the C style decorated names:
int CLibContent::ExtractCSymbol(char *szDecoratedName, DWORD dwLen)
int i = 0;
dwBuffPos = 0;
i += strlen(C_STYLE);
szFunc[dwBuffPos] = szDecoratedName[i];
if(szFunc[dwBuffPos] == '@')
szFunc[dwBuffPos] = 0;
Points of Interest
While doing this, I happened to experiment on BSC SDK (Browser Toolkit). It's a useful SDK when you want to browse the BSC (browser information) file produced by VC++. VC++ IDE uses this file to point out the function or variable definition when you right click and click the "Go to Definition of ..." menu item. Using this SDK, you can get the minute details of symbols used in your project. A function exists in this toolkit to get undecorated symbols from BSC file specific decorated names. Kind of
UnDecorateSymbolName function :)
When thinking of LIBs, it seems very simple and pictures all are in the same category. It is not like that. Every compiler and every version of the compiler may produce different types of LIBs and name mangle. If you see in this context, this tool will/may not work on the LIBs produced by a compiler other than VC++ 6.0.
In the exported header file, the functions exported belonging to a class will not appear in a class. It will appear more like a complete function. Another thing is, the C function parameters cannot be retrieved or exported to a header file.
If you want to know VC++ decoration format, check out this site. It provides the VC++ 6.0 name-mangling format.
Importantly note that this format can be changed without any notice. This tool also I'm not sure will work on VC++ 7.0 LIB files. It would be helpful if someone can test and tell me. I tested this on LIBs produced by eVC++ 3.0, eVC++4.0 and VC++ 6.0 compilers.
Hope you find this useful. Thanks.