// Setup.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
BOOL InitApplication(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
void FillInPropertyPage(PROPSHEETPAGE* pPSP, int nDlg, LPSTR pszProc, DLGPROC pfnDlgProc);
int DoWizard(HWND hwndOwner, HINSTANCE hInst);
BOOL APIENTRY PP1Proc(HWND hDlg, UINT message, UINT wParam, LONG lParam);
BOOL APIENTRY PP2Proc(HWND hDlg, UINT message, UINT wParam, LONG lParam);
LONG APIENTRY MainWndProc(HWND hWnd, UINT message, UINT wParam, LONG lParam);
BOOL APIENTRY WhichINIProc(HWND hDlg, UINT message, UINT wParam, LONG lParam);
int PreInstall(HWND hList);
int Install(LPCTSTR lpszWhich);
TCHAR g_szWhichIni[MAX_PATH];
HINSTANCE g_hInst = NULL;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
g_hInst = hInstance;
int nIni = PreInstall(NULL);
if (nIni > 1)
{
HWND hParent = NULL;
if (IDOK != DialogBox(g_hInst, MAKEINTRESOURCE(IDD_WHICHINI), hParent, WhichINIProc))
return 1;
}
else if (0 == nIni)
return 1;
DoWizard(NULL, hInstance);
return 0;
}
int PreInstall(HWND hList)
{
TCHAR szError[1024];
WIN32_FIND_DATA fd;
HANDLE h = FindFirstFile(_T("*.ini"), &fd);
if (INVALID_HANDLE_VALUE == h)
{
LoadString(g_hInst, IDS_INI_REQUIRED, szError, 1024);
MessageBox(NULL, szError, NULL, MB_OK|MB_ICONERROR);
return 0;
}
_tsplitpath(fd.cFileName, NULL, NULL, g_szWhichIni, NULL);
if (hList != NULL)
ListBox_AddString(hList, g_szWhichIni);
for (int n = 1; FindNextFile(h, &fd); n++)
{
_tsplitpath(fd.cFileName, NULL, NULL, g_szWhichIni, NULL);
if (hList != NULL)
ListBox_AddString(hList, g_szWhichIni);
wsprintf(szError, _T("%s\n"), fd.cFileName);
OutputDebugString(szError);
}
FindClose(h);
return n;
}
BOOL APIENTRY WhichINIProc(HWND hDlg, UINT message, UINT wParam, LONG lParam)
{
HWND hList = GetDlgItem(hDlg, IDC_LIST1);
switch (message)
{
case WM_INITDIALOG:
PreInstall(hList);
ListBox_SetCurSel(hList, 0);
break;
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED)
{
if (LOWORD(wParam) == IDOK)
{
ListBox_GetText(hList, ListBox_GetCurSel(hList), g_szWhichIni);
EndDialog(hDlg, LOWORD(wParam));
}
else if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
}
}
break;
default:
return FALSE;
}
return TRUE;
}
int Install(LPCTSTR lpszWhich)
{
// find the .EXE for installation
HKEY hkSvc;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows CE Services"), 0, KEY_READ, &hkSvc))
{
TCHAR szError[1024];
LoadString(g_hInst, IDS_NOACTIVESYNC, szError, 1024);
MessageBox(NULL, szError, NULL, MB_OK|MB_ICONERROR);
return 1;
}
TCHAR szInstallDir[MAX_PATH];
DWORD dw = MAX_PATH;
if (RegQueryValueEx(hkSvc, _T("InstalledDir"), NULL, NULL, (LPBYTE) szInstallDir, &dw))
{
TCHAR szError[1024];
LoadString(g_hInst, IDS_NOACTIVESYNC, szError, 1024);
MessageBox(NULL, szError, NULL, MB_OK|MB_ICONERROR);
return 1;
}
TCHAR szInstallCmd[MAX_PATH];
_tcscpy(szInstallCmd, szInstallDir);
_tcscat(szInstallCmd, _T("\\CeAppMgr.exe "));
// Find the module
TCHAR szFileName[MAX_PATH];
if (! GetModuleFileName(NULL, szFileName, MAX_PATH))
{
//GetLastError();
//FormatMessage();
return 0;
}
LPTSTR psz = _tcsrchr(szFileName, _T('\\'));
TCHAR szCurDir[MAX_PATH];
_tcsncpy(szCurDir, szFileName, psz - szFileName + 1);
szCurDir[psz - szFileName + 1] = 0;
// save the destination .INI file name
_tcscat(szInstallDir, _T("\\"));
_tcscat(szInstallDir, lpszWhich);
int n = _tcslen(szInstallDir);
szInstallDir[++n] = 0;
if (! CreateDirectory(szInstallDir, NULL))
{
DWORD dwError = GetLastError();
if (dwError != ERROR_ALREADY_EXISTS)
{
//FormatMessage()
return 1;
}
}
//
// Double null terminate destination
//
TCHAR szFrom[MAX_PATH];
_tcscpy(szFrom, szCurDir);
_tcscat(szFrom, g_szWhichIni);
_tcscat(szFrom, _T(".*.*"));
n = _tcslen(szFrom) + 1;
// double null terminate
szFrom[n] = 0;
//
// Copy the ftpView*.* files (ftpView.ini, ftpView_MIPS.cab, etc)
//
SHFILEOPSTRUCT shfo;
shfo.hwnd = NULL;
shfo.wFunc = FO_COPY;
shfo.pFrom = szFrom;
shfo.pTo = szInstallDir;
shfo.fFlags = FOF_FILESONLY | FOF_SIMPLEPROGRESS | FOF_NOCONFIRMATION;
shfo.fAnyOperationsAborted;
shfo.hNameMappings;
shfo.lpszProgressTitle = _T("Copying installation files...");
// copy the files to an alternate location
if (SHFileOperation(&shfo))
{
// this functions shows any error it encounters, just return
return 1;
}
// run the installation from it's new location
TCHAR szIni[MAX_PATH];
_tcscpy(szIni, "\"");
_tcscat(szIni, szInstallDir);
_tcscat(szIni, _T("\\"));
_tcscat(szIni, lpszWhich);
_tcscat(szIni, _T(".ini"));
_tcscat(szIni, "\"");
_tcscat(szInstallCmd, szIni);
WinExec(szInstallCmd, SW_SHOW);
return 0;
}
//
//
// FUNCTION: FillInPropertyPage(PROPSHEETPAGE *, int, LPSTR, LPFN)
//
// PURPOSE: Fills in the given PROPSHEETPAGE structure
//
// COMMENTS:
//
// This function fills in a PROPSHEETPAGE structure with the
// information the system needs to create the page.
//
void FillInPropertyPage(PROPSHEETPAGE* pPSP, int nDlg, LPSTR pszProc, DLGPROC pfnDlgProc)
{
pPSP->dwSize = sizeof(PROPSHEETPAGE);
pPSP->dwFlags = 0;
pPSP->hInstance = g_hInst;
pPSP->pszTemplate = MAKEINTRESOURCE(nDlg);
pPSP->pszIcon = NULL;
pPSP->pfnDlgProc = pfnDlgProc;
pPSP->pszTitle = pszProc;
pPSP->lParam = 0;
}
//
//
// FUNCTION: CreateWizard(HWND)
//
// PURPOSE: Create the Wizard control.
//
// COMMENTS:
//
// This function creates the wizard property sheet.
//
int DoWizard(HWND hwndOwner, HINSTANCE hInst)
{
PROPSHEETPAGE psp[2];
PROPSHEETHEADER psh;
FillInPropertyPage( &psp[0], IDD_PROPPAGE_START, TEXT("Welcome"), PP1Proc);
FillInPropertyPage( &psp[1], IDD_PROPPAGE_LICENSE, TEXT("License"), PP2Proc);
psh.dwSize = sizeof(PROPSHEETHEADER);
psh.dwFlags = PSH_PROPSHEETPAGE | PSH_WIZARD | PSH_NOAPPLYNOW;
psh.hwndParent = hwndOwner;
psh.pszCaption = (LPSTR) TEXT("Setup Wizard");
psh.nPages = sizeof(psp) / sizeof(PROPSHEETPAGE);
psh.nStartPage = 0;
psh.ppsp = (LPCPROPSHEETPAGE) &psp;
return (PropertySheet(&psh));
}
//
// FUNCTION: PP1Proc(HWND, UINT, UINT, LONG)
//
// PURPOSE: Processes messages for "Addaptability to Change" page
//
// MESSAGES:
//
// WM_INITDIALOG - intializes the page
// WM_NOTIFY - processes the notifications sent to the page
// WM_COMMAND - saves the id of the choice selected
//
//
BOOL APIENTRY PP1Proc(HWND hDlg, UINT message, UINT wParam, LONG lParam)
{
HWND hRadio = GetDlgItem(hDlg, IDC_DISAGREE);
switch (message)
{
case WM_INITDIALOG:
{
char szBuf[4096];
char szBuf2[4096];
SetFocus(hRadio);
SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
GetWindowText( GetDlgItem(hDlg, IDC_STATIC_APP), szBuf, 4096 );
wsprintf(szBuf2, szBuf, g_szWhichIni, g_szWhichIni);
SetWindowText( GetDlgItem(hDlg, IDC_STATIC_APP), szBuf2 );
}
return FALSE;
case WM_COMMAND:
break;
case WM_NOTIFY:
switch (((NMHDR FAR *) lParam)->code)
{
case PSN_KILLACTIVE:
SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
break;
case PSN_RESET:
SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
break;
case PSN_SETACTIVE:
PropSheet_SetWizButtons(GetParent(hDlg), PSWIZB_NEXT);
break;
default:
return FALSE;
}
break;
default:
return FALSE;
}
return TRUE;
}
//
// FUNCTION: PP2Proc(HWND, UINT, UINT, LONG)
//
// PURPOSE: Processes messages for "Addaptability to Change" page
//
// MESSAGES:
//
// WM_INITDIALOG - intializes the page
// WM_NOTIFY - processes the notifications sent to the page
// WM_COMMAND - saves the id of the choice selected
//
//
BOOL APIENTRY PP2Proc(HWND hDlg, UINT message, UINT wParam, LONG lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
char szLicense[MAX_PATH];
wsprintf(szLicense, "%s.License.txt", g_szWhichIni);
HANDLE hFile = CreateFile(szLicense, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_FLAG_SEQUENTIAL_SCAN, NULL);
DWORD dwSize = GetFileSize(hFile, NULL);
if (NULL != hFile && 0 < dwSize)
{
DWORD dwRead;
BYTE* pData = (BYTE*) LocalAlloc(LPTR, dwSize);
ReadFile(hFile, pData, dwSize, &dwRead, NULL);
SetWindowText(GetDlgItem(hDlg, IDC_LICENSE), (char*)pData);
LocalFree((HLOCAL) pData);
}
else
{
TCHAR sz[4096];
LoadString((HINSTANCE)::GetWindowLong(hDlg, GWL_HINSTANCE),
IDS_CHARITY, sz, 4096);
SetWindowText(GetDlgItem(hDlg, IDC_LICENSE), sz);
}
}
break;
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED)
{
if (LOWORD(wParam) == IDC_DISAGREE)
PropSheet_SetWizButtons(GetParent(hDlg), PSWIZB_BACK);
else if (LOWORD(wParam) == IDC_AGREE)
PropSheet_SetWizButtons(GetParent(hDlg), PSWIZB_BACK | PSWIZB_FINISH);
}
break;
case WM_NOTIFY:
switch (((NMHDR FAR *) lParam)->code)
{
case PSN_KILLACTIVE:
SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
break;
case PSN_RESET:
SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
break;
case PSN_SETACTIVE:
PropSheet_SetWizButtons(GetParent(hDlg), PSWIZB_BACK);
break;
case PSN_WIZFINISH:
Install(g_szWhichIni);
break;
case PSN_WIZBACK: // nothing
default:
return FALSE;
}
break;
default:
return FALSE;
}
return TRUE;
}