Thread: SizeofResource not returning right size

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    SizeofResource not returning right size

    Im trying to lock and write the icon resource, that is in module itself, to a file.
    Its an icon.ico file 16x16 with size 1150 bytes and SizeofResource returns only 20 bytes.

    GetLastError returns "The operation completed successfully". and the icon is displayed as expected.

    resource.rc
    Code:
    #define IDI_MYICON   101
    
    IDI_MYICON   ICON  "myicon.ico"
    The function:
    Code:
    void Resource()
    {
        FILE *file;
        HRSRC hRes = NULL;
        HGLOBAL hResLoaded;
        LPVOID lpResLock = NULL;
        char* cbuf;
        DWORD sizeOfRes;
        int i;
        hRes = FindResource(NULL,
                                MAKEINTRESOURCE(IDI_MYICON),
                                RT_GROUP_ICON);
        
        if(hRes != NULL)
        {
            hResLoaded = LoadResource(NULL, hRes);
    
            if(hResLoaded != NULL)
            {
                sizeOfRes = SizeofResource(NULL, hRes);
                cout << "Size = "<< sizeOfRes << endl;
                cbuf = (char*) calloc (sizeOfRes, sizeof(char));
                strcpy(cbuf,(char*)LockResource(hResLoaded));
    
                if(sizeOfRes != 0)
                {
                    file = fopen("myfile.txt", "wb");
                    if(file != NULL)
                    {
                        for(i = 0; i < sizeOfRes; i++)
                        {
                            fwrite(&cbuf[i],sizeof(char),1,file);
                        }
                        fclose(file);
                    }
                }
            }
            free(cbuf);
            UnlockResource(hResLoaded);
            FreeResource(hResLoaded);
        }
    }
    Last edited by Ducky; 10-28-2010 at 07:11 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I think you are getting the size of a GRPICONDIR struct (holding an array of pointers to the image data you want).

    Have a read here.

    Icons
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you very much Novocain!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with arrays
    By chilledmilk in forum C++ Programming
    Replies: 1
    Last Post: 05-06-2010, 10:32 PM
  2. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  3. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 06:14 PM
  4. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  5. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM