⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setup.c

📁 加密硬盘、分区、虚拟盘的程序源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Copyright (C) 2004 TrueCrypt Team, truecrypt.org
   This product uses components written by Paul Le Roux <pleroux@swprofessionals.com> */

#include "TCdefs.h"
#include <SrRestorePtApi.h>

#define MAX_PASSWORD 

#include "apidrvr.h"
#include "dlgcode.h"
#include "dismount.h"
#include "../common/resource.h"

#include "resource.h"

#include "dir.h"
#include "setup.h"

#include <sys\types.h>
#include <sys\stat.h>

#pragma warning( disable : 4201 )
#pragma warning( disable : 4115 )

#include <shlobj.h>

#pragma warning( default : 4201 )
#pragma warning( default : 4115 )

char dlg_file_name[TC_MAX_PATH];
BOOL bUninstall = FALSE;
BOOL bDone = FALSE;

HMODULE SystemRestoreDll = 0;

BOOL
StatDeleteFile (char *lpszFile)
{
	struct __stat64 st;

	if (_stat64 (lpszFile, &st) == 0)
		return DeleteFile (lpszFile);
	else
		return TRUE;
}

BOOL
StatRemoveDirectory (char *lpszDir)
{
	struct __stat64 st;

	if (_stat64 (lpszDir, &st) == 0)
		return RemoveDirectory (lpszDir);
	else
		return TRUE;
}

HRESULT
CreateLink (char *lpszPathObj, char *lpszArguments,
	    char *lpszPathLink)
{
	HRESULT hres;
	IShellLink *psl;

	/* Get a pointer to the IShellLink interface.  */
	hres = CoCreateInstance (&CLSID_ShellLink, NULL,
			       CLSCTX_INPROC_SERVER, &IID_IShellLink, &psl);
	if (SUCCEEDED (hres))
	{
		IPersistFile *ppf;

		/* Set the path to the shortcut target, and add the
		   description.  */
		psl->lpVtbl->SetPath (psl, lpszPathObj);
		psl->lpVtbl->SetArguments (psl, lpszArguments);

		/* Query IShellLink for the IPersistFile interface for saving
		   the shortcut in persistent storage.  */
		hres = psl->lpVtbl->QueryInterface (psl, &IID_IPersistFile,
						    &ppf);

		if (SUCCEEDED (hres))
		{
			WORD wsz[TC_MAX_PATH];

			/* Ensure that the string is ANSI.  */
			MultiByteToWideChar (CP_ACP, 0, lpszPathLink, -1,
					     wsz, TC_MAX_PATH);

			/* Save the link by calling IPersistFile::Save.  */
			hres = ppf->lpVtbl->Save (ppf, wsz, TRUE);
			ppf->lpVtbl->Release (ppf);
		}
		psl->lpVtbl->Release (psl);
	}
	return hres;
}

void
GetProgramPath (HWND hwndDlg, char *path)
{
	ITEMIDLIST *i;
	HRESULT res;

	if (nCurrentOS == WIN_NT && IsDlgButtonChecked (hwndDlg, IDC_ALL_USERS))
        res = SHGetSpecialFolderLocation (hwndDlg, CSIDL_COMMON_PROGRAMS, &i);
	else
        res = SHGetSpecialFolderLocation (hwndDlg, CSIDL_PROGRAMS, &i);

	SHGetPathFromIDList (i, path);
}


void
StatusMessage (HWND hwndDlg, char *head, char *txt)
{
	char szTmp[TC_MAX_PATH];
	sprintf (szTmp, head, txt);
	SendMessage (GetDlgItem (hwndDlg, IDC_FILES), LB_ADDSTRING, 0, (LPARAM) szTmp);
		
	SendDlgItemMessage (hwndDlg, IDC_FILES, LB_SETTOPINDEX, 
		SendDlgItemMessage (hwndDlg, IDC_FILES, LB_GETCOUNT, 0, 0) - 1, 0);
}

void
RegMessage (HWND hwndDlg, char *txt)
{
	StatusMessage (hwndDlg, "Adding registry entry %s", txt);
}

void
CopyMessage (HWND hwndDlg, char *txt)
{
	StatusMessage (hwndDlg, "Copying %s", txt);
}

void
RemoveMessage (HWND hwndDlg, char *txt)
{
	StatusMessage (hwndDlg, "Removing %s", txt);
}

void
ServiceMessage (HWND hwndDlg, char *txt)
{
	StatusMessage (hwndDlg, "Service %s", txt);
}

void
IconMessage (HWND hwndDlg, char *txt)
{
	StatusMessage (hwndDlg, "Adding icon %s", txt);
}

int CALLBACK
BrowseCallbackProc(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData) 
{
	switch(uMsg) {
	case BFFM_INITIALIZED: 
	{
	  /* WParam is TRUE since we are passing a path.
	   It would be FALSE if we were passing a pidl. */
	   SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)pData);
	   break;
	}

	case BFFM_SELCHANGED: 
	{
		char szDir[TC_MAX_PATH];

	   /* Set the status window to the currently selected path. */
	   if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szDir)) 
	   {
		  SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);
	   }
	   break;
	}

	default:
	   break;
	}

	return 0;
}

BOOL
BrowseFiles2 (HWND hwndDlg, char* lpszTitle, char* lpszFileName)
{
	BROWSEINFO bi;
	LPITEMIDLIST pidl;
	LPMALLOC pMalloc;
	BOOL bOK  = FALSE;

	if (SUCCEEDED(SHGetMalloc(&pMalloc))) 
	{
		ZeroMemory(&bi,sizeof(bi));
		bi.hwndOwner = hwndDlg;
		bi.pszDisplayName = 0;
		bi.lpszTitle = lpszTitle;
		bi.pidlRoot = 0;
		bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT /*| BIF_EDITBOX*/;
		bi.lpfn = BrowseCallbackProc;
		bi.lParam = (LPARAM)lpszFileName;

		pidl = SHBrowseForFolder(&bi);
		if (pidl!=NULL) 
		{
			if (SHGetPathFromIDList(pidl,lpszFileName)==TRUE) 
			{
				bOK = TRUE;
			}

			pMalloc->lpVtbl->Free(pMalloc,pidl);
			pMalloc->lpVtbl->Release(pMalloc);
		}
	}

	return bOK;
}


void
LoadLicense (HWND hwndDlg)
{
	FILE *fp;

	fp = fopen ("Setup Files\\license.txt", "rb");

	if (fp == NULL)
		return;
	else
	{
		long x;

		fseek (fp, 0, SEEK_END);
		x = ftell (fp);
		rewind (fp);

		if (x > 0)
		{
			char *tmp = malloc (x + 1);
			long z;

			if (tmp == NULL)
				goto exit;
			z = (long) fread (tmp, 1, x, fp);
			if (z != x)
			{
				free (tmp);
				goto exit;
			}
			else
			{
				int i;
				tmp[x] = 0;

				//// Remove single CRLFs
				//for (i = 0; i < x - 3; i++)
				//{
				//	if (tmp[i] == 0xd && tmp[i+2] == 0xd)
				//		i += 4;

				//	if (tmp[i] == 0xd && tmp[i+2] != 0xd)
				//	{
				//		tmp[i] = tmp[i+1] = ' ';
				//	}
				//}

				SendMessage (GetDlgItem (hwndDlg, IDC_LICENSE), WM_SETFONT, (WPARAM) hFixedFont, (LPARAM) 0);
				SetWindowText (GetDlgItem (hwndDlg, IDC_LICENSE), tmp);

				free (tmp);
			}
		}
	}

      exit:
	fclose (fp);
}

BOOL
DoFilesInstall (HWND hwndDlg, char *szDestDir, BOOL bUninstallSupport)
{


	char *szFiles[]=
	{
		"ATrueCrypt.exe", "ATrueCrypt Format.exe",
		"Alicense.txt", "ATrueCrypt User Guide.pdf",
		"WTrueCrypt Setup.exe", "STrueCryptService.exe", "Dtruecrypt.sys",
		"Itruecrypt.vxd"
	};

	char szTmp[TC_MAX_PATH];
	BOOL bOK = TRUE;
	int i;

	if (bUninstall == TRUE)
		bUninstallSupport = FALSE;

	for (i = 0; i < sizeof (szFiles) / sizeof (szFiles[0]); i++)
	{
		BOOL bResult, bSlash;
		char szDir[TC_MAX_PATH];
		int x;

		if (bUninstallSupport == FALSE && strstr (szFiles[i], "TrueCrypt Setup") != 0)
			continue;

		if (*szFiles[i] == 'A')
			strcpy (szDir, szDestDir);
		else if (*szFiles[i] == 'S')
			GetSystemDirectory (szDir, sizeof (szDir));
		else if (*szFiles[i] == 'I')
		{
			GetSystemDirectory (szDir, sizeof (szDir));

			x = strlen (szDestDir);
			if (szDestDir[x - 1] == '\\')
				bSlash = TRUE;
			else
				bSlash = FALSE;

			if (bSlash == FALSE)
				strcat (szDir, "\\");

			strcat (szDir, "IOSUBSYS");
		}
		else if (*szFiles[i] == 'D')
		{
			GetSystemDirectory (szDir, sizeof (szDir));

			x = strlen (szDestDir);
			if (szDestDir[x - 1] == '\\')
				bSlash = TRUE;
			else
				bSlash = FALSE;

			if (bSlash == FALSE)
				strcat (szDir, "\\");

			strcat (szDir, "Drivers");
		}
		else if (*szFiles[i] == 'W')
			GetWindowsDirectory (szDir, sizeof (szDir));

		x = strlen (szDestDir);
		if (szDestDir[x - 1] == '\\')
			bSlash = TRUE;
		else
			bSlash = FALSE;

		if (bSlash == FALSE)
			strcat (szDir, "\\");

		if ((*szFiles[i] == 'D' || *szFiles[i] == 'S') && nCurrentOS != WIN_NT)
			continue;

		if (*szFiles[i] == 'I' && nCurrentOS == WIN_NT)
			continue;

		sprintf (szTmp, "%s%s", szDir, szFiles[i] + 1);

		if (bUninstall == FALSE)
			CopyMessage (hwndDlg, szTmp);
		else
			RemoveMessage (hwndDlg, szTmp);

		if (bUninstall == FALSE)
		{
			bResult = CopyFile (szFiles[i] + 1, szTmp, FALSE);
			if (!bResult)
			{
				char s[256];
				sprintf (s, "Setup Files\\%s", szFiles[i] + 1);
				bResult = CopyFile (s, szTmp, FALSE);
			}
		}
		else
		{
			bResult = StatDeleteFile (szTmp);
		}

		if (bResult == FALSE)
		{
			LPVOID lpMsgBuf;
			DWORD dwError = GetLastError ();
			char szTmp2[700];

			FormatMessage (
					      FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
					      NULL,
					      dwError,
				 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),	/* Default language */
					      (char *) &lpMsgBuf,
					      0,
					      NULL
				);


			if (bUninstall == FALSE)
				sprintf (szTmp2, "The installation of '%s' has failed. %s Do you want to continue with the Install?",
					 szTmp, lpMsgBuf);
			else
				sprintf (szTmp2, "The uninstallation of '%s' has failed. %s Do you want to continue with the Uninstall?",
					 szTmp, lpMsgBuf);

			LocalFree (lpMsgBuf);

			if (MessageBox (hwndDlg, szTmp2, lpszTitle, MB_YESNO | MB_ICONHAND) != IDYES)
				return FALSE;
		}

	}

	return bOK;
}

BOOL
DoRegInstall (HWND hwndDlg, char *szDestDir, BOOL bInstallType, BOOL bUninstallSupport)
{
	char szDir[TC_MAX_PATH], *key;
	HKEY hkey = 0;
	BOOL bSlash, bOK = FALSE;
	DWORD dw;
	int x;

	strcpy (szDir, szDestDir);
	x = strlen (szDestDir);
	if (szDestDir[x - 1] == '\\')
		bSlash = TRUE;
	else
		bSlash = FALSE;

	if (bSlash == FALSE)
		strcat (szDir, "\\");

	if (nCurrentOS == WIN_NT)
	{
		/* 9/9/99 FIX This code should no longer be needed as we use
		   the "services" api to install the driver now, rather than
		   setting the registry by hand */

		/* Install device driver */
		key = "SYSTEM\\CurrentControlSet\\Services\\truecrypt";
		RegMessage (hwndDlg, key);
		if (RegCreateKeyEx (HKEY_LOCAL_MACHINE,
				    key,
				    0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &dw) != ERROR_SUCCESS)
			goto error;

		dw = 1;
		if (RegSetValueEx (hkey, "Type", 0, REG_DWORD, (BYTE *) & dw, 4) != ERROR_SUCCESS)
			goto error;

		dw = 1;
		if (RegSetValueEx (hkey, "Start", 0, REG_DWORD, (BYTE *) & dw, 4) != ERROR_SUCCESS)
			goto error;

		dw = 1;
		if (RegSetValueEx (hkey, "ErrorControl", 0, REG_DWORD, (BYTE *) & dw, 4) != ERROR_SUCCESS)
			goto error;

		if (RegSetValueEx (hkey, "Group", 0, REG_SZ, (BYTE *) "Primary disk", 13) != ERROR_SUCCESS)
			goto error;

		RegCloseKey (hkey);
		hkey = 0;
	}

	if (bInstallType == TRUE)
	{
		char szTmp[TC_MAX_PATH];

		key = "SOFTWARE\\Classes\\TrueCryptVolume";
		RegMessage (hwndDlg, key);
		if (RegCreateKeyEx (HKEY_LOCAL_MACHINE,
				    key,
				    0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &dw) != ERROR_SUCCESS)
			goto error;

		sprintf (szTmp, "TrueCrypt Volume", szDir);
		if (RegSetValueEx (hkey, "", 0, REG_SZ, (BYTE *) szTmp, strlen (szTmp) + 1) != ERROR_SUCCESS)
			goto error;

		RegCloseKey (hkey);
		hkey = 0;

		key = "SOFTWARE\\Classes\\TrueCryptVolume\\DefaultIcon";
		RegMessage (hwndDlg, key);
		if (RegCreateKeyEx (HKEY_LOCAL_MACHINE,
				    key,
				    0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &dw) != ERROR_SUCCESS)
			goto error;

		sprintf (szTmp, "%sTrueCrypt.exe,1", szDir);
		if (RegSetValueEx (hkey, "", 0, REG_SZ, (BYTE *) szTmp, strlen (szTmp) + 1) != ERROR_SUCCESS)
			goto error;

		RegCloseKey (hkey);
		hkey = 0;

		key = "SOFTWARE\\Classes\\TrueCryptVolume\\Shell\\open\\command";
		RegMessage (hwndDlg, key);
		if (RegCreateKeyEx (HKEY_LOCAL_MACHINE,
				    key,
				    0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, &dw) != ERROR_SUCCESS)
			goto error;

		sprintf (szTmp, "\"%sTrueCrypt.exe\" /v \"%%1\"", szDir );
		if (RegSetValueEx (hkey, "", 0, REG_SZ, (BYTE *) szTmp, strlen (szTmp) + 1) != ERROR_SUCCESS)
			goto error;

		RegCloseKey (hkey);
		hkey = 0;

		key = "SOFTWARE\\Classes\\.tc";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -