Extract Resources from EXE file

Feb 13, 2017 at 3:46pm
I am making one program that it will have one executable since resources and it will extract on my disk. The code is compiled fine but ever extract or write my executable file.
The code is the follow:
I will have first a resouces file called resources.rc:

1
2
3
4
#include <windows.h>
    #include "resource.h"
    
    ELEXE RCDATA "Example1.exe"


resource.h:

1
2
3
4
5
6
#ifndef RESOURCE_H_INCLUDED
    #define RESOURCE_H_INCLUDED
    
    #define ELEXE ""
    
    #endif // RESOURCE_H_INCLUDED 


main.cpp:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <windows.h>
#include "resource.h"

using namespace std;

int main()
{

    string exe = "ELEXE";
    HRSRC res=FindResource(NULL,exe.c_str(),RT_RCDATA);

    if(res==NULL)
         cout << GetLastError();
         cout << "\n";

    int size=SizeofResource(NULL,res);

    if( !size )
        cout << 122; // Arbitrario. -> ERROR_INSUFFICIENT_BUFFER
        cout << "\n";

    HGLOBAL hRes=LoadResource(NULL,res);

    if( !hRes )
        cout << 122; // Arbitrario. -> ERROR_INSUFFICIENT_BUFFER
        cout << "\n";

    unsigned char *pRes=(unsigned char *)LockResource(hRes);


    HANDLE hFile=CreateFile("C:/Users/android/Desktop/pi.exe",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);

    if(hFile==INVALID_HANDLE_VALUE)
         cout << GetLastError();
         cout << "\n";

    DWORD bytesWritten = size;

    WriteFile(hFile,pRes,size,&bytesWritten,NULL);

    if( !WriteFile(hFile,pRes,size,&bytesWritten,NULL) )
        cout << GetLastError();
        cout << "\n";

    CloseHandle(hFile);


    ShellExecute(HWND_DESKTOP,NULL,"C:/Users/android/Desktop/pi.exe",NULL,NULL,SW_SHOWNORMAL);
    system("pause");
    return 0;
}

    

The result of my compilation:
https://i.stack.imgur.com/SggQH.png

The executable Example1 is included in my Program `Executable2` so the size will be the double. However, when i click my executable `Example2` never write me my resource in this case i'm calling it `"pi.exe"`:

https://i.stack.imgur.com/Tn

My problem is: why never write me my executable in the `path`? WriteFile never write me the file (`WriteFile Function`). How i could change the code to get to write sucessfully my executable file.

Modificated - Output of my console:
https://i.stack.imgur.com/xBrlv.png
Last edited on Feb 13, 2017 at 5:59pm
Feb 13, 2017 at 5:07pm
It's praiseworthy that you check the return value like if(res==NULL) return GetLastError();, but when you just end main with them you don't see what's wrong. Much better to print them to the console.
Feb 13, 2017 at 6:00pm
i put the output https://i.stack.imgur.com/xBrlv.png seem's to have null values.
Last edited on Feb 13, 2017 at 6:14pm
Feb 13, 2017 at 6:35pm
Error 1814 means
The specified resource name cannot be found in the image file.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms681386(v=vs.85).aspx
which is now wonder since you make it an empty string in resource.h #define ELEXE "" .
Topic archived. No new replies allowed.