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

📄 main.cpp

📁 不可多得的 windows mobile 软件安装程序生成工具Vc++ 源码。提供cab
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// EzSetup - an CE app install maker
// Copyright (C) 1998-2001 Scott Ludwig
// scottlu@eskimo.com
// http://www.eskimo.com/~scottlu
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// or visit http://www.gnu.org/copyleft/gpl.html.

#include <windows.h>
#include <stdio.h>
#include "property.h"
#include "resource.h"

bool FilePresent(char *pszFn)
{
	HANDLE h = CreateFile(pszFn, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (h == INVALID_HANDLE_VALUE)
		return false;
	CloseHandle(h);
	return true;
}

DWORD FileSize(char *pszFn)
{
	HANDLE h = CreateFile(pszFn, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (h == INVALID_HANDLE_VALUE)
		return 0;
	DWORD dw = GetFileSize(h, NULL);
	CloseHandle(h);
	return dw;
}

BYTE *GetResourcePtr(int idr, int *pcb)
{
	HRSRC hr = FindResource(NULL, MAKEINTRESOURCE(idr), MAKEINTRESOURCE(99));
	if (hr == NULL)
		return NULL;
	*pcb = SizeofResource(NULL, hr);
	HGLOBAL h = LoadResource(NULL, hr);
	return (BYTE *)LockResource(h);
}

bool WriteToFile(BYTE *pb, int cb, char *psz)
{
	// Create the file and write these bytes

	HANDLE h = CreateFile(psz, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (h == INVALID_HANDLE_VALUE)
		return false;
	int cbWritten = 0;
	if (!WriteFile(h, pb, cb, (DWORD *)&cbWritten, NULL) || cb != cbWritten)
		return false;
	CloseHandle(h);
	return true;
}

void GetTempFn(char *pszWorkingDir, char *pszFile)
{
	GetTempFileName(pszWorkingDir, "ezs", 0, pszFile);
}

#define kidrEula 5
#define kidrReadme 4
#define kidrIniFile 1
#define kidrFirstCab 2
#define kidrDirectory 2
#define kidtCEAPPMGRFILES 100
#define kidtCEAPPMGRFILENAMES 101
#define kidtTEXTFILES 102
#define kidtMISCSTRINGS 103

byte *MapFile(char *psz, int *pcb, HANDLE *phMap)
{
	*pcb = FileSize(psz);
	HANDLE hFile = CreateFile(psz, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE) {
		printf("Error reading in file %s!", psz);
		return NULL;
	}
	*phMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
	CloseHandle(hFile);
	if (*phMap == NULL) {
MapError:		
		printf("Error mapping view of file %s!", psz);
		return NULL;
	}
	byte *pb = (BYTE *)MapViewOfFile(*phMap, FILE_MAP_READ, 0, 0, 0);
	if (pb == NULL) {
		CloseHandle(*phMap);
		goto MapError;
	}
	return pb;
}

bool WriteFileToResource(char *pszFn, HANDLE h, int idt, int idr)
{
	int cb;
	HANDLE hMap;
	byte *pb = MapFile(pszFn, &cb, &hMap);
	if (pb == NULL)
		return false;
	if (!UpdateResource(h, MAKEINTRESOURCE(idt), MAKEINTRESOURCE(idr), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), pb, cb)) {
		printf("Error updating resource of temp file!", pszFn);
		UnmapViewOfFile(pb);
		CloseHandle(hMap);
		return false;
	}
	UnmapViewOfFile(pb);
	CloseHandle(hMap);
	return true;
}

bool WriteStringToResource(char *psz, HANDLE h, int idt, int idr)
{
	if (!UpdateResource(h, MAKEINTRESOURCE(idt), MAKEINTRESOURCE(idr), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), psz, strlen(psz) + 1)) {
		printf("Error updating resource of temp file!");
		return false;
	}
	return true;
}

void GetFilenameOnly(char *pszFnIn, char *pszFnOut)
{
	char szFullPath[MAX_PATH];
	_fullpath(szFullPath, pszFnIn, sizeof(szFullPath));
	char szDrive[_MAX_DRIVE];
	char szDir[MAX_PATH];
	char szExt[MAX_PATH];
	_splitpath(szFullPath, szDrive, szDir, pszFnOut, szExt);
	strcat(pszFnOut, szExt);
}

// Text files aren't zero extended, so we need to do this

bool ZeroExtendFile(char *pszDir, char *pszFnIn, char *pszFnOut)
{
	GetTempFn(pszDir, pszFnOut);
	int cb;
	HANDLE hMap;
	byte *pb = MapFile(pszFnIn, &cb, &hMap);
	if (pb == NULL) {
		printf("Error reading file %s!\n", pszFnIn);
		return false;
	}

	HANDLE h = CreateFile(pszFnOut, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (h == INVALID_HANDLE_VALUE)
		return false;
	int cbWritten = 0;
	if (!WriteFile(h, pb, cb, (DWORD *)&cbWritten, NULL) || cb != cbWritten) {
		UnmapViewOfFile(pb);
		CloseHandle(hMap);
WriteError:
		printf("Error writing to temporary file %s!\n", pszFnOut);
		CloseHandle(h);
		return false;
	}
	UnmapViewOfFile(pb);
	CloseHandle(hMap);
	byte b = 0;
	if (!WriteFile(h, &b, 1, (DWORD *)&cbWritten, NULL) || cbWritten != 1)
		goto WriteError;
	CloseHandle(h);
	return true;
}

bool WriteNewCESetup(UINT idr, char *pszDir, char *pszComponent, char *pszFnIni, char *pszFnReadme, char *pszFnEula, int cCabs, char aszFnCabs[20][MAX_PATH], char *pszFnExeT)
{
	// First write out the empty cesetup

	int cb;
	byte *pb = GetResourcePtr(idr, &cb);
	*pszFnExeT = 0;
	GetTempFn(pszDir, pszFnExeT);

	// Write to file

	if (!WriteToFile(pb, cb, pszFnExeT)) {
		printf("Error writing to temporary file %s!\n", pszFnExeT);
		return false;
	}

	// Write in the new resources

	HANDLE h = BeginUpdateResource(pszFnExeT, FALSE);
	if (h == NULL) {
		printf("Error opening temporary file %s!\n", pszFnExeT);
ExeCleanup:
		DeleteFile(pszFnExeT);
		return false;
	}

	// Write the .ini file

	if (!WriteFileToResource(pszFnIni, h, kidtCEAPPMGRFILES, kidrIniFile)) {
		EndUpdateResource(h, TRUE);
		goto ExeCleanup;
	}

	// Write the .ini filename	

	char szFn[MAX_PATH];
	GetFilenameOnly(pszFnIni, szFn);
	if (!WriteStringToResource(szFn, h, kidtCEAPPMGRFILENAMES, kidrIniFile)) {
		EndUpdateResource(h, TRUE);
		goto ExeCleanup;
	}

	// Write .cabs

	for (int i = 0; i < cCabs; i++) {
		if (!WriteFileToResource(&aszFnCabs[i][0], h, kidtCEAPPMGRFILES, kidrFirstCab + i)) {
			EndUpdateResource(h, TRUE);
			goto ExeCleanup;
		}

		GetFilenameOnly(&aszFnCabs[i][0], szFn);
		if (!WriteStringToResource(szFn, h, kidtCEAPPMGRFILENAMES, kidrFirstCab + i)) {
			EndUpdateResource(h, TRUE);
			goto ExeCleanup;
		}
	}

	// Write readme

	ZeroExtendFile(pszDir, pszFnReadme, szFn);
	if (!WriteFileToResource(szFn, h, kidtTEXTFILES, kidrReadme)) {
		EndUpdateResource(h, TRUE);
		DeleteFile(szFn);
		goto ExeCleanup;
	}
	DeleteFile(szFn);

	// Write eula

	ZeroExtendFile(pszDir, pszFnEula, szFn);
	if (!WriteFileToResource(szFn, h, kidtTEXTFILES, kidrEula)) {
		EndUpdateResource(h, TRUE);
		DeleteFile(szFn);
		goto ExeCleanup;
	}
	DeleteFile(szFn);

	// Write directory name to install (which is component name)

	if (!WriteStringToResource(pszComponent, h, kidtMISCSTRINGS, kidrDirectory)) {
		EndUpdateResource(h, TRUE);
		goto ExeCleanup;
	}

	// We're done

⌨️ 快捷键说明

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