Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

cant get resources to work with mingw

im trying to make raw binary resources be in my end executable, but i cant get it to work.

i've got a proof-of-concept that shows my attempt,

i've made the icon be my file icon.ico, and it does work, the executable has it as its icon.

in main.c, it simply tries to use FindResource(), LoadResource() and LockResource() to read it.
however it fails at FindResource() with the error message

"findresource <1813>: The specified resource type cannot be found in the image file."

1813 is from GetLastError()
the message is from FormatMessage()


I have tried combinations of some things, but nothing helped.


resources.rc
[code]
#include "resources.h"

ID_ICON ICON "icon.ico"

ID_MY_RESOURCE RC_DATA "my_res.txt"
[/code]

resources.h
[code]

#ifndef _RESOURCES_H_
#define _RESOURCES_H_

#define ID_ICON 0
#define ID_MY_RESOURCE 1

#endif
[/code]

main.c
[code]

#include
#include

#include
#include "resources.h"

char*
make_error_string(int error) {

char fbuf[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
fbuf,
sizeof(fbuf),
NULL);

static char result[256];
snprintf(result, sizeof(result), "<%d>: %s", error, fbuf);
return result;
}

int
main(void) {

HRSRC rsrc = FindResource(NULL, MAKEINTRESOURCE(ID_MY_RESOURCE), RT_RCDATA);
if(!rsrc)
return printf("findresource %s
", make_error_string(GetLastError()));

HGLOBAL glo = LoadResource(NULL, rsrc);
if(!glo)
return printf("loadresource %s
", make_error_string(GetLastError()));

int size = SizeofResource(NULL, rsrc);
if(size == 0)
return printf("sizeofresource %s
", make_error_string(GetLastError()));

char* data = LockResource(glo);
if(!data)
return printf("lockresource %s
", make_error_string(GetLastError()));

int f = 0;
while(f < size) {
printf("[%d] = 0x%x = '%c'
", f, data[f] & 0xff, data[f] & 0xff);
f++;
}

return EXIT_SUCCESS;
}
[/code]

build.bat
[code]
@echo off
windres resources.rc resources.o
gcc -o main.exe main.c resources.o
pause
[/code]

any ideas on how to make it work?

Comments

  • gdbgdb Posts: 4Member
    Good morning,

    Compiled and linked your code with one of my compilers,
    namely the Code::Blocks 8.02 and everything went all
    right, except for one line I had to modify for
    "char* data = (char *)LockResource(glo);".

    Created an icon myself for the "icon.ico" and a text file
    for "my_res.txt".

    After that, the compiler did its job seamlessly: an exe
    was verily produced, only the icon wasn't loaded and the
    error message was:

    findresource <1812>: The specified image file did not
    contain a resource section.

    Process returned 78 (0x4E)

    Mind the error found was 1812, not 1813 as you stated.

    However, making another project and compiling and linking
    your code as a Windows application, the icon was loaded
    nicely, but the exe didn't run at all.

    Best,

    Geraldo


    : im trying to make raw binary resources be in my end executable, but
    : i cant get it to work.
    :
    : i've got a proof-of-concept that shows my attempt,
    :
    : i've made the icon be my file icon.ico, and it does work, the
    : executable has it as its icon.
    :
    : in main.c, it simply tries to use FindResource(), LoadResource() and
    : LockResource() to read it.
    : however it fails at FindResource() with the error message
    :
    : "findresource <1813>: The specified resource type cannot be found in
    : the image file."
    :
    : 1813 is from GetLastError()
    : the message is from FormatMessage()
    :
    :
    : I have tried combinations of some things, but nothing helped.
    :
    :
    : resources.rc
    : [code]:
    : #include "resources.h"
    :
    : ID_ICON ICON "icon.ico"
    :
    : ID_MY_RESOURCE RC_DATA "my_res.txt"
    : [/code]:
    :
    : resources.h
    : [code]:
    :
    : #ifndef _RESOURCES_H_
    : #define _RESOURCES_H_
    :
    : #define ID_ICON 0
    : #define ID_MY_RESOURCE 1
    :
    : #endif
    : [/code]:
    :
    : main.c
    : [code]:
    :
    : #include
    : #include
    :
    : #include
    : #include "resources.h"
    :
    : char*
    : make_error_string(int error) {
    :
    : char fbuf[256];
    : FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
    : NULL,
    : error,
    : MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
    : fbuf,
    : sizeof(fbuf),
    : NULL);
    :
    : static char result[256];
    : snprintf(result, sizeof(result), "<%d>: %s", error, fbuf);
    : return result;
    : }
    :
    : int
    : main(void) {
    :
    : HRSRC rsrc = FindResource(NULL, MAKEINTRESOURCE(ID_MY_RESOURCE), RT_RCDATA);
    : if(!rsrc)
    : return printf("findresource %s
    ", make_error_string(GetLastError()));
    :
    : HGLOBAL glo = LoadResource(NULL, rsrc);
    : if(!glo)
    : return printf("loadresource %s
    ", make_error_string(GetLastError()));
    :
    : int size = SizeofResource(NULL, rsrc);
    : if(size == 0)
    : return printf("sizeofresource %s
    ", make_error_string(GetLastError()));
    :
    : char* data = LockResource(glo);
    : if(!data)
    : return printf("lockresource %s
    ", make_error_string(GetLastError()));
    :
    : int f = 0;
    : while(f < size) {
    : printf("[%d] = 0x%x = '%c'
    ", f, data[f] & 0xff, data[f] & 0xff);
    : f++;
    : }
    :
    : return EXIT_SUCCESS;
    : }
    : [/code]:
    :
    : build.bat
    : [code]:
    : @echo off
    : windres resources.rc resources.o
    : gcc -o main.exe main.c resources.o
    : pause
    : [/code]:
    :
    : any ideas on how to make it work?
    :
Sign In or Register to comment.