Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8035599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:21:26+00:00 2026-06-05T02:21:26+00:00

This is my first attempt at using a resource file. I have seen a

  • 0

This is my first attempt at using a resource file. I have seen a lot of answers that apply to C# but not C. Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:21:26+00:00Added an answer on June 5, 2026 at 2:21 am

    Assuming you mean the method used by Sysinternals and others to carry the drivers or needed DLLs (or even the x64 version of the program itself) in the resource section of a program (e.g. Sysinternals’ Process Explorer), using Microsoft Visual C you can use this code:

    BOOL ExtractResTo(HINSTANCE Instance, LPCTSTR BinResName, LPCTSTR NewPath, LPCTSTR ResType)
    {
        BOOL bResult = FALSE;
        HRSRC hRsrc;
        if(hRsrc = FindResource(HMODULE(Instance), BinResName, ResType))
        {
            HGLOBAL hGlob
            if(HGLOBAL hGlob = LoadResource(Instance, hRsrc))
            {
                DWORD dwResSize = SizeofResource(Instance, hRsrc);
                HANDLE hFileWrite = CreateFile(NewPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0);
                if(hFileWrite != INVALID_HANDLE_VALUE)
                __try
                {
                    DWORD dwSizeWritten = 0;
                    bResult = (WriteFile(hFileWrite, LockResource(hGlob), dwResSize, &dwSizeWritten, NULL) && (dwSizeWritten == dwResSize));
                }
                __finally
                {
                    CloseHandle(hFileWrite);
                }
            }
        }
        return bResult;
    }
    

    This saves the given resource (BinResName) of resource type (ResType) from the module (e.g. a DLL) Instance to file NewPath. Obviously if your C doesn’t understand __try and __finally you’ll have to adjust the code accordingly.

    Taken from here (in SIDT.rar) and adjusted for C. The code is under the liberal BSD license according to the website.

    Now if you wanted to get the pointer to the data (ppRes) and its size (pwdResSize):

    BOOL GetResourcePointer(HINSTANCE Instance, LPCTSTR ResName, LPCTSTR ResType, LPVOID* ppRes, DWORD* pdwResSize)
    {
        // Check the pointers to which we want to write
        if(ppRes && pdwResSize)
        {
            HRSRC hRsrc;
            // Find the resource ResName of type ResType in the DLL/EXE described by Instance
            if(hRsrc = FindResource((HMODULE)Instance, ResName, ResType))
            {
                HGLOBAL hGlob;
                // Make sure it's in memory ...
                if(hGlob = LoadResource(Instance, hRsrc))
                {
                    // Now lock it to get a pointer
                    *ppRes = LockResource(hGlob);
                    // Also retrieve the size of the resource
                    *pdwResSize = SizeofResource(Instance, hRsrc);
                    // Return TRUE only if both succeeded
                    return (*ppRes && *pdwResSize);
                }
            }
        }
        // Failure means don't use the values in *ppRes and *pdwResSize
        return FALSE;
    }
    

    Call like this:

    LPVOID pResource;
    DWORD pResourceSize;
    if(GetResourcePointer(hInstance, _T("somename"), _T("sometype"), &pResource, &pResourceSize))
    {
        // use pResource and pResourceSize
        // e.g. store into a string buffer or whatever you want to do with it ...
    }
    

    Note, this also works for resources with integer IDs. You can detect these by using the macro IS_INTRESOURCE.

    The resource script, e.g. myresources.rc, itself is trivial:

    #include <winnt.rh>
    "somename" "sometype" "path\to\file\to\embed"
    

    RCDATA instead of sometype is a reasonable choice

    #include <winnt.rh> // for RCDATA to be known to rc.exe
    "somename" RCDATA "path\to\file\to\embed"
    

    … in which case you would adjust the above call to be:

    GetResourcePointer(hInstance, _T("somename"), RT_RCDATA, &pResource, &pResourceSize)
    

    Complete example making use of GetResourcePointer above. Let’s say you have a pointer variable char* buf and you know your resource is actual text, then you need to make sure that it is zero-terminated when used as a string, that’s all.

    Resource script:

    #include <winnt.rh>
    
    "test" RCDATA "Test.txt"
    

    The code accessing it

    char* buf = NULL;
    LPVOID pResource;
    DWORD pResourceSize;
    if(GetResourcePointer(hInstance, _T("test"), RT_RCDATA, &pResource, &pResourceSize))
    {
        if(buf = calloc(pResourceSize+1, sizeof(char)))
        {
            memcpy(buf, pResource, pResourceSize);
            // Now use buf
            free(buf);
        }
    }
    

    Unless, of course you meant to simply link a .res file which works by passing it to the linker command line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Although I am not new to Python, this is my first attempt at using
this if my first attempt at using streaming for WCF, and I am struggling
I'm writing an application using DDD techniques. This is my first attempt at a
Because this is my first attempt at an extension method that seems quite useful
This is my first attempt at using BufferStrategy and I'd really appreciate some hints.
This is my first attempt at using Github's shared collaboration model to work on
This is my first attempt at using Backbone.js, so I decided to make a
First, let me just mention that this is my first attempt at a from-the-ground-up
This is my first rails app that is not an example out of a
This is my first attempt at using the Google Data API, and I'm getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.