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