Click here to Skip to main content
13,864,785 members
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 22 Jan 2019
Licenced CPOL

How to Embed Resources in a Static Library

, 22 Jan 2019
Rate this:
Please Sign up or sign in to vote.
How can a Static Library embed resources and other data so any application (even a Console based one) can use them.

See also my GitHub Repo.


It started with a need to release a Static library as a product and include some resources in it. That is usually impossible since a program that uses such Static library will use its own resources. I posted a question (which I answered after finding this solution). I realized that this solution deserves an article with source code. I have also posted a question about that possibility in Stack Overflow.

In another occasion I wanted my Static library to embed another file of any kind (can be an executable, DLL, photo, documents, etc.) and extract it during run-time.

Several Code Examples

The source consists of a Static Library project ("MH_EmbedDataLib") and a test Console application ("ConsoleApp"). Of course, if it works with a GUIless Console application, it will work with any other type, i.e., Win32, MFC, etc.

The MH_EmbedDataLib Library

The MH_EmbedDataLib library has 2 main functions:

Get Icon

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;

Provided that you converted an icon you wish to use into a static array of BYTE and named it s_byIconData, GetIcon loads the icon from this static data (s_byIconData).

The LookupIconIdFromDirectoryEx function is used to search through icon or cursor data for the icon or cursor that best fits the current display device.

Then CreateIconFromResourceEx is used to create the icon from resource bits describing it, generated by Bin2C.


void DisplayMessageWithIcon(LPWSTR message)
    //xmb.hInstanceIcon = (HINSTANCE)GetIcon();
    xmb.dwOptions |= XMSGBOXPARAMS::RightJustifyButtons;
    xmb.crBackground = RGB(255, 255, 255);
    xmb.crText = RGB(10, 135, 180);
    xmb.nIdIcon = 101;
    xmb.nTimeoutSeconds = 5;
        L"Code Project article by Michael Haephrati",
        MB_YESNO, &xmb);

This function displays a MessageBox showing the icon encapsulated in s_byIconData.

The Process

Select a photo you wish to use, or an image. A web site named ICO Convert provides the service of converting most image types into an .ico file. After you have an .ico file, open it to see that the conversion went well. Then convert it into an array of BYTE using Bin2C. Make sure you use the correct name for the static array. (in this case, we used s_byIconData). Then the MessageBox will display it. For the purpose of this article, I used a photo from the Nigara Falls (Canadian side of course...) shown below.

The result isn't great but demonstrate this concept.


The fancy Message Box used in the code of the article was developed by Hans Dietrich.

Creating Ad-Hoc Files from an Array

Another example that serves the purpose of this article is using the same array created by Bin2C for Ad-Hoc generation of a file during run-time.
bool Array2TempFile(char *arr, size_t len,WCHAR *file)
	HANDLE hFile = CreateFile(file, GENERIC_WRITE, // open for writing
		FILE_SHARE_WRITE, // share for writing
		NULL, // default security
		CREATE_ALWAYS, // create new file only
		// normal file archive and impersonate client
		NULL); // no attr. template

	DWORD bytes;
		WriteFile(hFile, &arr[0], len, &bytes, NULL);
		return true;

	return false;

I used this function to extract and install Kernel drivers from a Static library. Here is an example of such use.

are predefined.
int len;
// Extract .inf file for x86 Win 10
len = (int)sizeof(sgdrv86_10_inf);
Array2TempFile((char *)&sgdrv64_10_inf[0], len, INF_PATH);
// Extract .sys file for x86 Win 10
len = (int)sizeof(sgdrv86_10_sys);
Array2TempFile((char *)&sgdrv86_10_sys[0], len, SYS_PATH);


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Michael Haephrati
CEO Secured Globe, Inc.
United States United States
Michael Haephrati, CEO and co-founder of Secured Globe, Inc. Worked on many ventures starting from HarmonySoft, designing Rashumon, the first Graphical Multi-lingual word processor for Amiga computer. During 1995-1996 he worked as a Contractor with Apple at Cupertino.

You may also be interested in...


Comments and Discussions

QuestionHave you seen ... Pin
Richard MacCutchan28-Jan-19 1:18
protectorRichard MacCutchan28-Jan-19 1:18 
AnswerRe: Have you seen ... Pin
Michael Haephrati30-Jan-19 6:13
mvpMichael Haephrati30-Jan-19 6:13 
Questionhave you consider to post this as a tip? Pin
Nelek24-Jan-19 10:11
protectorNelek24-Jan-19 10:11 
AnswerRe: have you consider to post this as a tip? Pin
Michael Haephrati30-Jan-19 6:12
mvpMichael Haephrati30-Jan-19 6:12 
GeneralRe: have you consider to post this as a tip? Pin
Nelek30-Jan-19 20:21
protectorNelek30-Jan-19 20:21 
AnswerRe: have you consider to post this as a tip? Pin
Michael Haephrati31-Jan-19 0:39
mvpMichael Haephrati31-Jan-19 0:39 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190214.1 | Last Updated 22 Jan 2019
Article Copyright 2019 by Michael Haephrati
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid