Embed a resource to a static library (nothing is impossible)

I needed to embed a resource (icon) to a static library so we can deploy just the .lib and .h files with no need to include any graphics or .rc files.

I wanted my static library to be used by any application including such that don’t have any graphic user interface, i.e. Console applications, etc.

I posted a question in Stack Overflow and Code Project and the responses were: That’s not possible…

Here is the question I have posted:

Is there a way to embed resources (such as icons, dialogs) in a c++ (Win32 API) static library? My purpose is to embed an icon in the static library in a way that functions that use LoadIcon will work as if it was a normal .exe so the main application can only link to the static library and include a header file, with no requirement to add other files such as .rc files, or .ico files, etc. Clearly the main application who uses the static library doesn’t have this resource so LoadIcon will fail, however I was wondering if there is a workaround to make it work. A static array with the icon data can work as long as the standard API calls (such as LoadIcon) will work.

To explain further, the person who will be using the static library will only have 2 files: .lib and .h and will not have any .rc file


Comments that followed:

“It is not possible, you can stop looking. Consider a DLL project instead.

At Code Project I was pointed to the following Stack Overflow thread:

” I read them but we wish to deploy 2 files: .lib and .h. These solutions require deploying the .rc file and having whoever uses our static library to link / include it as well.”
“Then there is no solution from my point of view.
The SO thread covers it all.”

Here is the solution I have found after some research I found a way. Using my method, an icon can be used as an integral part of a static library and such library can be used by any type of application, including a console one (which doesn’t have any resource segment whatsoever).

1. Icon is converted to a static array of BYTE. bin2c can be used for that.

2. Data is converted into a HICON handle. Here is how I have done that:

HICON GetIcon()
   DWORD dwTmp;
   int offset;
   HANDLE hFile;
   HICON hIcon = NULL;
   offset = LookupIconIdFromDirectoryEx(s_byIconData, TRUE, 0, 0, LR_DEFAULTCOLOR);
   if (offset != 0)
      hIcon = CreateIconFromResourceEx(s_byIconData + offset, 0, TRUE, 0x00030000, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE);
   return hIcon;  
3. GetIcon is used instead of LoadIcon.
Instead of calling:

I call

m_hIcon = GetIcon()

To test it, I created a Console application and a new static library. I added to the static library the XMessageBox class which allows using a custom icon.
The Console application just calls a function located at the static library and the icon is displayed!

See also:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s