Embed txt resource instead of fopen external.

Feb 7, 2011 at 11:59pm
I'm currently using fopen to load an external file and do what I need with the data inside, see below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Myfile = fopen ("text.txt", "r");
  if (!Myfile)
    {
      printf ("unable to read file: %s\n");
	  system ("pause");
      exit (0);
    }


  fseek (Myfile, 0, SEEK_END);
  file_size = ftell (Myfile);
  fseek (Myfile, 0, SEEK_SET);
  final_code = (const char*)calloc (1, file_size);
  fread ((void *) final_code, 1, file_size, Myfile);
  fclose (Myfile);


Works fine, however I'd like to just embed the text file as a resource included with the compiled console app, having some issues understanding how.

I tried adding the file as a resource and using what little I understand of FindResource to do this, but I couldn't properly figure out how to access the data, had issues with it saying I cannot convert from 'hrsrc' to 'file'.

After a while I just got mad and started over, could I get a code example on how this would be done properly? TIA!


Feb 8, 2011 at 1:19am
You need to create a "resource file".

The source code .rc file looks something like this:

1
2
HELLO_TXT  RCDATA  "hello.txt"
QUUX_TXT   RCDATA "foobiz\quux.txt"

You then use your resource compiler to turn that into a .res file or an .o or .obj file (as appropriate to your compiler -- meaning that you'll have to read some documentation about it).

Once done, you can refer to the resource by name.

1
2
3
4
5
6
HRSRC rsrc_hello_txt = FindResource( NULL, "HELLO_TXT", RT_RCDATA );
DWORD sizeof_hello_txt = SizeofResource( NULL, rsrc_hello_txt );
HGLOBAL g_hello_txt = LoadResource( NULL, rsrc_hello_txt );
const char* p_hello_txt = LockResource( g_hello_txt );
... /* mess with p_hello_txt[ 0..sizeof_hello_txt-1 ] here */
//all done. There is no need to UnlockResource() or FreeResource() in Win32 

Oh yeah, read more if you need to:
http://www.google.com/search?btnI=1&q=msdn+resource+file

Hope this helps.
Feb 8, 2011 at 4:05am
OK I think i'm on board with the concept, I made a resource file, added it to my VS project and referenced in the name. Assuming thats done correctly, how do I interface with my data now read into memory?

I'll just post the main() so you can get a better picture. There's a lot more functions to the program but I'll try and keep it short and relevant.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
main ()
{
  HANDLE process_list = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
  PROCESSENTRY32 proc;
  FILE *Myfile;
  int file_size;
  const char *final_code = 0;

  memset (&proc, 0, sizeof (proc));
  proc.dwSize = sizeof (PROCESSENTRY32);

  HRSRC rsrc_pcode_txt = FindResource( NULL, "IDR_RCDATA1", RT_RCDATA );
  DWORD sizeof_pcode_txt = SizeofResource( NULL, rsrc_pcode_txt );
  HGLOBAL g_pcode_txt = LoadResource( NULL, rsrc_pcode_txt );
  LPVOID p_pcode_txt = LockResource( g_pcode_txt );

  Myfile = p_pcode_txt; // Is this right for pulling my referenced data?

  fseek (Myfile, 0, SEEK_END);
  file_size = ftell (Myfile);
  fseek (Myfile, 0, SEEK_SET);
  final_code = (const char*)calloc (1, file_size);
  fread ((void *) final_code, 1, file_size, Myfile);
  fclose (Myfile);

  InjectCode("mFile.exe", iCode, iCodeEnd, final_code);
  return 0;
}


You can see what I was using to open the file and read into memory so it could be injected into a running process, how does what we've changed affect this, I'm unsure how to use the new data when the external file isn't required, what do I still need?
Last edited on Feb 8, 2011 at 4:07am
Feb 8, 2011 at 9:13pm
You'll notice that in my example I specifically got a pointer to char when I locked the resource. The data is in a character array... not in a file. You'll have to use string-handling techniques to read the data. sscanf() provides all the necessary machinery to treat it as if you were reading it from a file...

Good luck!
Topic archived. No new replies allowed.