Dump an executable to disk from resources

0

Hello, I'm doing a program that adds an application in resources and downloads it to disk. in principle it compiles well but does not dump my application on disk .. I was checking and basically doing the correct thing is to add it to resources and then write the problem file I suppose you are here since you do not write it to me:

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

The code will be as follows: I will have the file with my resource resources.rc:

#include <windows.h>
#include "resource.h"

ELEXE RCDATA "Example1.exe"

resource.h:

#ifndef RESOURCE_H_INCLUDED
#define RESOURCE_H_INCLUDED

#define ELEXE "ELEXE"

#endif // RESOURCE_H_INCLUDED

main.cpp:

#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;
}

Errors returned:

Different sizes, why ?:

    
asked by Perl 13.02.2017 в 12:18
source

1 answer

1

I can not write comments, I'm missing points. I put it in response, although, technically , it is not.

First, you should check if the correct size was obtained for the resource:

int size=SizeofResource(NULL,res);

if( !size )
  return 122; // Arbitrario. -> ERROR_INSUFFICIENT_BUFFER

The same, for LoadResource( ) :

HGLOBAL hRes=LoadResource(NULL,res);
  if( !hRes )
    return 122; // Arbitrario. -> ERROR_INSUFFICIENT_BUFFER

Then, you should check what happened when writing:

if( !WriteFile(hFile,pRes,size,&bytesWritten,NULL) )
  return GetLastError();

At least, you can find out what's going on.

Do it, edit the question, and now we will see what happens: -)

EDITO

The only thing that occurs to me is that you remove

if( !WriteFile(hFile,pRes,size,&bytesWritten,NULL) )
  return GetLastError();

and put it in a loop, if you write weird ; my experience with Windows is limitadilla . I'm sorry I could not be more helpful: - (

BOOL ret;
while( ( ret = WriteFile(hFile,pRes,size,&bytesWritten,NULL) ) && ( size != bytesWritten ) );

if( !ret )
  return GetLastError();
    
answered by 13.02.2017 / 12:39
source