Single click self extracting .exe in c++?

I would like to build a self extracting .exe in Visual Studios c++ 2012.
Does anyone have a working solution for VS2012?

I checked projects like LZMA (7-zip SDK), but it doesn't seem to mention self extracting '.exe' anywhere.
http://ck.kolivas.org/apps/lrzip/lrzip-0.615/lzma/lzma.txt

I check NSIS and inno-setup, but those product don't seem to offer 1 click install.

I checked 'Chilkat v9.4.0' but that is not free!
http://www.chilkatforum.com/questions/2354/how-to-write-a-self-extractor-for-the-windows-os


Those codeproject.com files are so outdated that they give me a lot of errors in VS2012:
www.codeproject.com/Articles/4221/Adding-and-extracting-binary-resources
www.codeproject.com/Articles/7053/Pure-WIN32-Self-Extract-EXE-Builder
closed account (13bSLyTq)
Hi,

I found this snippet to extract a resource(BINARY) into a file. In which case we can call it "self-extracting".

Code:
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <Windows.h>

#ifndef _waxie_binary_resource_h_
#define _waxie_binary_resource_h_

#include <string>

class BinRes  
{
public:
	BinRes();
	virtual ~BinRes();

public:
	static void ExtractBinResource( std::string strCustomResName, int nResourceId, std::string strOutputName);

private:
	static std::string getAppLocation();

};

#endif 

#include <string>
#include <fstream>
#include <iostream>


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


BinRes::BinRes()
{

}

BinRes::~BinRes()
{

}

void BinRes::ExtractBinResource( std::string strCustomResName, int nResourceId, std::string strOutputName )
{
	HGLOBAL hResourceLoaded;		// handle to loaded resource 
	HRSRC hRes;						// handle/ptr. to res. info. 
	char *lpResLock;				// pointer to resource data 
	DWORD dwSizeRes;
	std::string strOutputLocation;
	std::string strAppLocation; 

	// lets get the app location
	strAppLocation = getAppLocation();
	strOutputLocation = strAppLocation += "\\";
	strOutputLocation += strOutputName;
	
	// find location of the resource and get handle to it
	hRes = FindResource( NULL, MAKEINTRESOURCE(nResourceId), strCustomResName.c_str() );
	
	// loads the specified resource into global memory. 
	hResourceLoaded = LoadResource( NULL, hRes ); 

	// get a pointer to the loaded resource!
	lpResLock = (char*)LockResource( hResourceLoaded ); 

	// determine the size of the resource, so we know how much to write out to file!  
	dwSizeRes = SizeofResource( NULL, hRes );

	std::ofstream outputFile(strOutputLocation.c_str(), std::ios::binary);

	outputFile.write((const char*)lpResLock, dwSizeRes);
	outputFile.close();
}


// retrieves the full path and file name for our executable file 
std::string BinRes::getAppLocation()
{
	TCHAR szPathName[128];
	std::string strPath;

	GetModuleFileName(NULL, szPathName, 128);

	strPath = szPathName;
	
	int slashPos = strPath.rfind('\\');


	if(slashPos == strPath.npos)
		throw "Unable to get exe location";

	strPath = strPath.substr(0, slashPos);
	
	return strPath;
}
Topic archived. No new replies allowed.