0% found this document useful (0 votes)
22 views3 pages

Binder

This document contains C++ code to add resources like files to an executable. It takes in a filename and stub name as input, reads the file, adds it as a resource to the stub executable, and writes configuration data to a separate file.

Uploaded by

Rafael Sousa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Binder

This document contains C++ code to add resources like files to an executable. It takes in a filename and stub name as input, reads the file, adds it as a resource to the stub executable, and writes configuration data to a separate file.

Uploaded by

Rafael Sousa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <windows.h>
#include <fstream>
#include <stdio.h>
using namespace std;
bool configstarted = false;

void WriteToFile(char*data)
{
if (configstarted==false)
{
ofstream writer;
writer.open("config.txt", ios::out); //deletes file if it already exists
if(writer.is_open())
{
writer << data;
writer << ":";
writer.close();
configstarted=true;
}
}
else
{
ofstream writer;
writer.open("config.txt", ios::app);
writer << data;
writer << ":";
writer.close();

}
}

bool AddResource(char*filename, char*stub)


{
HANDLE hfile;
DWORD dwfilesize, dwbytesread;
LPBYTE lpbuffer;
hfile = CreateFile(filename, //name of file to create handle to
GENERIC_READ, //file opened for reading
0, //sharing mode- 0 means the file cannot be shared or
opened until handle is closed
NULL,
OPEN_EXISTING, //only opens file if it exists
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hfile==INVALID_HANDLE_VALUE)
{
cout << "\nERROR: File could not be opened\n";
return false; //file could not be opened

}
else
{
dwfilesize = GetFileSize(hfile, NULL);
lpbuffer = new BYTE[dwfilesize]; //create a buffer the size of the file
if (ReadFile(hfile, lpbuffer, dwfilesize, &dwbytesread, NULL) == FALSE)
//parameters = handle to file to be read, buffer to put data in, max num bytes to
read, num that receieves bytes read, optional)

{
cout << "\nERROR: Could not read file\n";
delete [] lpbuffer;
CloseHandle(hfile);
return false;
}
else
{
HANDLE hresource;
hresource = BeginUpdateResource(stub, FALSE); //file to update
resource, FALSE specifies not to delelete filename's existing resources
if (hresource==NULL)
{
cout << "\nERROR: Could not begin updating the resource\n";
delete [] lpbuffer;
CloseHandle(hfile);
return false;
}
else
{
if (UpdateResource(hresource, //handle created by
BeginUpdateResource
RT_RCDATA, //Resource type- raw data
filename, //creates resource ID
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPVOID) lpbuffer, //pointer to byte array
containing the file data
dwfilesize) == TRUE)
{
EndUpdateResource(hresource, FALSE); //This commits the changes
made by update resource
if(filename!="config.txt")
{WriteToFile(filename);}
delete [] lpbuffer;
CloseHandle(hfile);
return true;

}
else
{
EndUpdateResource(hresource, true); //DISCARDS the changes made
by UpdateResource b/c UpdateResource failed
delete [] lpbuffer;
CloseHandle(hfile);
return false;
}
}

}
}

int main()
{

char* source = new char[MAX_PATH];


char*stub = new char[MAX_PATH];
cout << "What is the name of the stub?\n";
cin.getline(stub, MAX_PATH);
cout << "To add a file, type its name. Otherwise, type 'q' to finalize the
output\n";
while ( cin.getline(source, MAX_PATH))
{
if (source[0]=='q')
{
AddResource("config.txt", stub);
break;
}
else
{
if (AddResource(source, stub)==true)
cout << source << " Added successfully!\n";
}
}
cin.get();
delete[] source;
delete[] stub;

return 0;
}

You might also like