NO

Author Topic: Adding .exe as Custom Resource. Confused about NameId = unsigned int = 1 Only !  (Read 3797 times)

EdPellesC99

  • Guest


Hi, I am trying to understand what is going on with my NameId in my Resource.rc file.

Here is my main source, .h, and .rc.

I found the base code in a forum and modified it slightly.

=========
.rc file contents
=========

#include "Resource.h"


ID_Ed1    RCDATA    "C:\\WINDOWS\\NOTEPAD.EXE"

============================================

=========
Resource.h file contents
=========

#define ID_Ed1  1

============================================


=========
main source file
=========

#include <windows.h>
#include <stdio.h>
#include "Resource.h"
int main(int argC, char *argV[])
{
   // Get pointer and size to resource
   HRSRC hRes = FindResource(0, MAKEINTRESOURCE(ID_Ed1), RT_RCDATA);
   HGLOBAL hMem = LoadResource(0, hRes);
   void *pMem = LockResource(hMem);
   
   DWORD size = SizeofResource(0, hRes);
   
   // Write it to a file
   char *fname = "test.exe";
   FILE *f = fopen(fname, "wb");
   fwrite(pMem, size, ID_Ed1, f);
   fclose(f);
   
   // Execute it
   system(fname);
   return 0;
}

============================================


   Now Why MUST I define [#define ID_Ed1  1]  ID_Ed1 as the unsigned integer 1.

Though another number will allow me to build, the build main.exe will crash on my computer when I run it.

Use the number 1 and no problem.

I would really like to know why !!

What if I were trying to embed more than one binary as a custom resource (of type RT_RCDATA)?

I have read myself silly at MSDN, I really cannot find a whole lot of info/examples on using the RT_RCDATA type.


Help !!!

Tx, Ed





Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
From help:
size_t fwrite(const void * restrict src, size_t size, size_t num, FILE * restrict stream);

So use this:
Code: [Select]
fwrite(pMem, size, 1, f);
May the source be with you

EdPellesC99

  • Guest

   Hi Timo !

   That was it! So....

   fwrite(pMem, size, 1, f);
   // 1 is Number of elements of 'size' bytes to write.
                         (since pMem is one element of 'size' bytes, 1 is the num of elements you will write)


  In the original code, the forum poster used "1" as the NameId. He did not have a resource.h file.

  So I did not look closely at the line:
             fwrite(pMem, size, 1, f); (his original), I did a search and replace 1 for ID_Ed1.

   Thanks Timo, I do not know how long it would have taken me to suspect that line. I was even
wondering if it was my computer..... because I have had a lot of problems (I have been fixing lately).

   Thanks again,

    Ed