Click here to Skip to main content
15,881,828 members
Articles / Mobile Apps

A Tool to View a LIB

Rate me:
Please Sign up or sign in to vote.
4.90/5 (55 votes)
15 Jan 20055 min read 288.3K   17K   134   45
Useful tool to view functions in a library (.LIB) file and export them to a header (.H) file

Sample Image - LibView.jpg

Introduction

Have you ever wondered what 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 want 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".

Background

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.

Name Mangling

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:

?MyFunc@@YAHD@Z

you will get:

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:

MC++
int __stdcall func (int a, double b)

will look like this after encoding:

__imp__func@12

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.

Core Code

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:

C++
...
...
    HANDLE hLibFile = CreateFile("C:\Libfiles\adme.lib",
                GENERIC_READ,
                FILE_SHARE_READ|FILE_SHARE_WRITE,
                NULL,
                OPEN_EXISTING,
                0,
                NULL);

...
...
    for(i=0;i<dwFileSize;i++)
    {
        SetFilePointer(hLibFile,i,0,FILE_BEGIN);

        nRet = ReadFile(hLibFile,&szChar,32,&dwBytes,NULL);
        if(!nRet)
            return GetLastError();
        
        if((szChar[0] == '?')||(bCollect))
        {// the symbol is C++ type collect it
            szBuff[dwBuffPos] = szChar[0];
            dwBuffPos++;
            bCollect = true;
        }
        else if(!memcmp(szChar,C_STYLE,7))
        // check if it is C style (ie. "__imp__")
        {
            dwBytes = strlen(szChar);
            nRet = ExtractCSymbol(szChar,dwBytes);
            // if it is succ then 0 else non zero

            if(!nRet)    // if failed then pass on to next char
                i += dwBytes; // if passed skip the string
        }

        if((szChar[0] == 0)&&(bCollect))
        { // finish and pass the buffer to decode
            szBuff[dwBuffPos] = szChar[0];
            dwBuffPos++;

            nRet = ExtractCppSymbol(szBuff, dwBuffPos);
            
            dwBuffPos = 0;
            bCollect = false;
        }
    }
...
...

The following function decodes the CPP style decorated names:

C++
int CLibContent::ExtractCppSymbol(char *szDecoratedName, DWORD dwLen)
{
    char szFunc[512];
    int nRet;
    
    nRet = UnDecorateSymbolName(szDecoratedName, szFunc, 512, 
                     UNDNAME_COMPLETE|UNDNAME_32_BIT_DECODE);
    if(!nRet)
        return GetLastError(); // failure

    if(!memcmp(szDecoratedName,szFunc,nRet)) 
        return ERROR_INVALID_DATA;
        // since it is not decoded, this cud b some junk

    if(!memcmp("`string'",szFunc,nRet))
    // this cud b imported functions and constants
        return ERROR_INVALID_DATA; // so v don't need this

    //storing strings in a array and returning nothing else
    return  m_cMemStore.Add(szDecoratedName,szFunc);    
}

This following function decodes the C style decorated names:

C++
int CLibContent::ExtractCSymbol(char *szDecoratedName, DWORD dwLen)
{
    char szFunc[512];
    DWORD dwBuffPos;
    int i = 0;
    dwBuffPos = 0;

    if(memcmp(szDecoratedName,C_STYLE,7))
        return  ERROR_INVALID_DATA;

    i += strlen(C_STYLE);
    
    for(;i<dwLen;i++)
    {
        szFunc[dwBuffPos] = szDecoratedName[i];
        
        if(szFunc[dwBuffPos] == '@')
        {
            szFunc[dwBuffPos] = 0;
            
            //storing strings in a array nothing else
            m_cMemStore.Add(szDecoratedName, szFunc); 

            return ERROR_SUCCESS;
        }

        dwBuffPos++;
    }

    return  ERROR_INVALID_DATA;;    
}

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. :)

Limitations

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.

Conclusion

If you want to know VC++ decoration format, check out this site. It provides the VC++ 6.0 name-mangling format.

It is important to 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!

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalit is running Pin
Southmountain24-Sep-20 14:37
Southmountain24-Sep-20 14:37 
Questionhangs on 3mb LIB file in win10 Pin
Patrik Johansson19-Dec-15 14:29
Patrik Johansson19-Dec-15 14:29 
GeneralThanks for sharing Pin
Ashok Kumar RV3-Apr-15 1:20
Ashok Kumar RV3-Apr-15 1:20 
GeneralRe: Thanks for sharing Pin
naesten12-Aug-15 20:23
naesten12-Aug-15 20:23 
GeneralMy vote of 1 Pin
michal50017-May-14 6:34
michal50017-May-14 6:34 
Questionstack overflow Pin
ytfrdfiw31-Oct-13 21:02
ytfrdfiw31-Oct-13 21:02 
General[My vote of 1] Build fails, run fails on medium size libraries, prefer dumpbin Pin
pentaxf12-Jun-13 9:37
pentaxf12-Jun-13 9:37 
Generalgreat Pin
omendezmorales25-May-12 3:35
omendezmorales25-May-12 3:35 
SuggestionC Symbols don't import correctly. Here is the fix. Pin
MuThink13-Mar-12 1:02
MuThink13-Mar-12 1:02 
GeneralGreat Article Pin
ShaneMcDonald21-Jun-10 15:39
ShaneMcDonald21-Jun-10 15:39 
GeneralPDB's are crashing Pin
glitteringsound8-Feb-10 3:55
glitteringsound8-Feb-10 3:55 
QuestionWhen it is required to rebuild a project, which has a static lib? Pin
hebeh23-Jul-09 1:03
hebeh23-Jul-09 1:03 
AnswerRe: When it is required to rebuild a project, which has a static lib? Pin
Ramanan.T23-Jul-09 13:15
Ramanan.T23-Jul-09 13:15 
GeneralRe: When it is required to rebuild a project, which has a static lib? Pin
hebeh26-Jul-09 23:01
hebeh26-Jul-09 23:01 
GeneralWorks for me Pin
Member 9427191-Nov-08 4:41
Member 9427191-Nov-08 4:41 
GeneralThanks! Pin
jasho441-Oct-08 22:18
jasho441-Oct-08 22:18 
QuestionCant compile tool on Visual C++ 2005 Pin
mattywix1-Jul-08 0:53
mattywix1-Jul-08 0:53 
Cannot open include file: 'afxmsg_.h': No such file or directory


------ Build started: Project: LibView, Configuration: Debug Win32 ------
Compiling...
StdAfx.cpp
c:\program files\sdk2003\include\mfc\afxwin.h(1227) : fatal error C1083: Cannot open include file: 'afxmsg_.h': No such file or directory
Creating browse information file...
Microsoft Browse Information Maintenance Utility Version 8.00.50727
Copyright (C) Microsoft Corporation. All rights reserved.
BSCMAKE: error BK1506 : cannot open file '.\debug\LibView.sbr': No such file or directory
Build log was saved at "file://c:\Documents and Settings\mwicks\Desktop\LibView\Debug\BuildLog.htm"
LibView - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Dead | X|
GeneralYou got my 5 Pin
Paul M Watt15-May-08 7:14
mentorPaul M Watt15-May-08 7:14 
GeneralFYI... Name length exceeding szBuff size Pin
RushJob4-Jan-07 0:42
RushJob4-Jan-07 0:42 
GeneralMemory leaks Pin
Laurent Regnier25-Sep-06 5:56
professionalLaurent Regnier25-Sep-06 5:56 
GeneralJust some thought... Pin
Jun Du13-Jul-06 4:27
Jun Du13-Jul-06 4:27 
GeneralHelped me solve a problem Pin
phatmann8-Mar-06 8:22
phatmann8-Mar-06 8:22 
GeneralExcellent tool - helped out a great deal.... Pin
Spitfed27-Jan-06 1:10
Spitfed27-Jan-06 1:10 
GeneralThis does not work with VC 7.1 Pin
Giroo29-Nov-05 15:22
Giroo29-Nov-05 15:22 
QuestionWhy Import function used by lib is also appeared? Pin
vitiluck9-Nov-05 15:59
vitiluck9-Nov-05 15:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.