// Based on ExeExtractor written by Kaushal Malhotra (malhotrakaushal@mantraonline.com)
// Copyright (c) 2001
#include "stdafx.h"
#include ".\unwrap.h"
#include "ShellUtils.h"
#include "unzip.h"
#include "resource.h"
CUnwrap::CUnwrap(void)
{
}
CUnwrap::~CUnwrap(void)
{
}
BOOL CUnwrap::Init()
{
hResLoad = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_IDR_EXE_INSTALLER), "IDR_EXE");
hResData = LoadResource(AfxGetResourceHandle(), hResLoad);
data = (LPCSTR)LockResource(hResData);
prev_data = data;
// We should have properly loaded the resource
if(data == NULL || hResData == NULL)
return FALSE;
// project name
m_prjName = data;
data += CString(data).GetLength() + 1;
m_prjExeName = m_prjName + ".exe";
// web site caption
m_webCaption = data;
data += CString(data).GetLength() + 1;
// web site URL
m_webURL = data;
data += CString(data).GetLength() + 1;
// if caption is empty, then use url as caption
if(m_webURL.GetLength() && m_webCaption.GetLength() == 0)
m_webCaption = data;
m_winVer = *(DWORD *)data;
data += sizeof(DWORD);
// a double word in data contains number of files in resource
m_nFiles = *(DWORD *)data;
data += sizeof(DWORD);
return TRUE;
}
void CUnwrap::Close()
{
// We should have reached the end of resource by now
ASSERT((DWORD)(data - prev_data) == SizeofResource(AfxGetResourceHandle(),hResLoad));
FreeResource(hResData);
}
void CUnwrap::SetPath(CString path)
{
m_path = path;
}
void CUnwrap::SetShortcut(BOOL shortcut)
{
m_shortcut = shortcut;
}
void CUnwrap::SetStartup(BOOL startup)
{
m_startup = startup;
}
void CUnwrap::SetStartMenu(BOOL startmenu)
{
m_startmenu = startmenu;
}
DWORD CUnwrap::GetWinVer()
{
return m_winVer;
}
CString CUnwrap::GetPrjName()
{
return m_prjName;
}
CString CUnwrap::GetWebCaption()
{
return m_webCaption;
}
CString CUnwrap::GetWebURL()
{
return m_webURL;
}
void CUnwrap::Files()
{
for(DWORD nCtr = 0; nCtr < m_nFiles; ++nCtr)
{
// Data is pointing to file name, use it with directory selected by user to create the final file name
CString strFName = m_path + data;
char path[MAX_PATH];
char dir[MAX_PATH];
char drive[MAX_PATH];
char file[MAX_PATH];
char ext[MAX_PATH];
sprintf(path, "%s", strFName);
_splitpath(path, drive, dir, file, ext);
data += CString(data).GetLength() + 1;
// Data now contains file size in bytes followed by actual file
int ziplen = *(UINT *)data;
void *zipbuf = (void*)(data + sizeof(UINT));
// unzip our "file" from the buffer so that we can save as a file
HZIP hz;
ZIPENTRY ze;
hz = OpenZip(zipbuf, ziplen, 0);
GetZipItem(hz, 0, &ze);
CString baseDir;
baseDir.Format("%s%s", drive, dir);
SetUnzipBaseDir(hz, baseDir);
UnzipItem(hz, ze.index, ze.name);
CloseZip(hz);
// Data now contains file size in bytes followed by actual file
data = (data + *(UINT *)data) + sizeof(UINT);
CoInitialize(NULL);
// create the shortcuts that the user requested
CString fileName;
fileName.Format("%s%s", file, ext);
if(!m_prjExeName.CompareNoCase(fileName) )
{
if(m_shortcut)
CShellUtils::CreateShortcut(path, file, dir, file, CSIDL_DESKTOP);
if(m_startup)
CShellUtils::CreateShortcut(path, file, dir, file, CSIDL_STARTUP);
}
// create a start menu link for every .EXE file, all links will be in a
// directory having the name of the project
if(!stricmp(ext, ".exe"))
{
if(m_startmenu)
{
CString menuDir;
menuDir.Format("%s\\%s", m_prjName, file);
CShellUtils::CreateShortcut(path, menuDir, dir, file, CSIDL_PROGRAMS);
}
}
CoUninitialize();
// register DLLs
if(!stricmp(ext, ".dll") || !stricmp(ext, ".ocx"))
Register(strFName);
}
}
void CUnwrap::Register(CString dll)
{
BOOL rc = FALSE;
HMODULE hm = LoadLibrary(dll);
if(hm)
{
rc = GetProcAddress(hm, TEXT("DllRegisterServer") ) != NULL;
FreeLibrary(hm);
}
if(!rc)
return;
CString str;
str.Format("Regsvr32.exe /s \"" + dll + "\"");
WinExec(str, SW_SHOW);
}