Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hardcoding the resources in application

I have some code which shows a simple dialog box and handles user action (written using plain WinAPI).

// Display dialog and handle user action
LRESULT choice = DialogBoxParam(NULL, MAKEINTRESOURCE(AP_IDD_DIALOG), NULL, (DLGPROC)DialogCallback, NULL);

Is there any way to hardcode the resource file dialog.rc, which is used to build the dialog ?(I would like to get rid of .rc files and I'm pretty sure there is a way, yet I don't know what it is :)

Edit

Also, does someone have any ideas on converting existing .rc files into hardcoded resources? Is this possible?

like image 492
Yippie-Ki-Yay Avatar asked Sep 09 '26 18:09

Yippie-Ki-Yay


1 Answers

I'm surprised I couldn't find an existing app to do this sort of thing, enough hits on google with people trying to do this.

Ok, so the DLGTEMPLATE is a variable length blob of data, normally you let the dialog function pull it from the resource bundle for you, but instead you want to store it in your program.

You need to change your static lib to have a new function to decode some 'blob' back into the dlgtemplate, and you need to generate the blob. (or add the blob in your code without decoding which I don't want to think about right now)

The following code will give you the DLGTemplate data you need to imbed in your app. (cut from larger project)

HGLOBAL LoadResourceImpl(wchar_t *resource, wchar_t *type)
{
    HRSRC handle = FindResource(hInstance, resource,type);
    if (handle)
    {
        HGLOBAL hResource = LoadResource(hInstance, handle);
        if (hResource)
            return LockResource(hResource);  
    }
    return 0;
}

DLGTEMPLATE * LoadDialog(wchar_t *resource)
{
    return (DLGTEMPLATE *) LoadResourceImpl(resource,RT_DIALOG);
}

DLGTEMPLATE * LoadDialog(int resource)
{
    return (DLGTEMPLATE *) LoadResourceImpl(MAKEINTRESOURCE(resource),RT_DIALOG);
}

Make an app that includes your resource - use the appropriate LoadDialog to get the data.

Now "write out" that blob in a format to include in your app - step 1 - find out how much data there is by traversing the structure to find the total size including all the controls (control count is in DLGTEMPLATE::cdit)

step 2 - convert the data to something you can compile into your code - like HEX

Add to your static library a new 'HEX' to DLGTEMPLATE method and the hex string you made using the other app.

like image 118
Greg Domjan Avatar answered Sep 11 '26 09:09

Greg Domjan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!