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

📄 setupcheck.cpp

📁 一个教你如何制作安装程序的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// SetupCheck.cpp: implementation of the CSetup class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Setup.h"
#include "SetupCheck.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


CSetup::CSetup()
{		
	CheckWindowVersion(m_bNT,m_dwWinVerMajor,m_dwWinVerMinor);
}

CSetup::~CSetup()
{
	
}

BOOL CSetup::SetupCheckIEVersion(int& MajorVersion,int& MinorVersion)
{
	
	HKEY hKey;
	DWORD rv;

	char sVersion[128];	
	DWORD dwDataType;	
	DWORD dwLength;

	dwLength=sizeof(sVersion);
	memset(sVersion,'\0',sizeof(sVersion));	

	rv=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                    "Software\\Microsoft\\Internet Explorer\\URL Compatibility",
                     0,
                     KEY_READ,
                     &hKey);

    if(rv==ERROR_SUCCESS)
	{
				 
		RegQueryValueEx(hKey,
				  "Version",
				  NULL,
				  &dwDataType,
				  (LPBYTE)sVersion,
				  &dwLength);		
		RegCloseKey(hKey);			
	}
	else
	{
		return FALSE;
	}

	CString str(sVersion);
	int n=str.Find(".",0);
	CString strMajorVersion,strMinorVersion;
	strMajorVersion=str.Left(n);
	strMinorVersion=str.Right(str.GetLength()-n-1);	
	MajorVersion=atoi(strMajorVersion.GetBuffer(6));
	MinorVersion=atoi(strMinorVersion.GetBuffer(6));
	return TRUE;	
}



BOOL CSetup::CopyFile(LPCSTR lpszSrc, LPCSTR lpszDec,DWORD dwFlag)
{
	SHFILEOPSTRUCT lpFileOp;
	lpFileOp.hwnd=AfxGetMainWnd()->GetSafeHwnd();
	lpFileOp.wFunc=FO_COPY;
	lpFileOp.pFrom=lpszSrc;
	lpFileOp.pTo=lpszDec;
	lpFileOp.fFlags=dwFlag;
	lpFileOp.fAnyOperationsAborted=FALSE;
	lpFileOp.hNameMappings =NULL;
	lpFileOp.lpszProgressTitle =NULL;
	int rval=SHFileOperation(&lpFileOp);
	CString strMsg;	
	if(rval==0)
	{
		
		if(lpFileOp.fAnyOperationsAborted==TRUE)
		{	
			strMsg="复制文件";
			strMsg+=lpszSrc;
			strMsg += "操作被取消!";
			::AfxMessageBox(strMsg,MB_OK);
			return FALSE;
		}
		else
		{
			::AfxMessageBox("复制文件操作成功!",MB_OK);			
			return TRUE;
		}
	}
	else
	{
		strMsg="复制文件从";
		strMsg+=lpszSrc;
		strMsg+=" 到 ";
		strMsg+=lpszDec;
		strMsg+=" 失败!";
		::AfxMessageBox(strMsg,MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}
}

BOOL CSetup::DeleteFile(LPCSTR lpszFileName)
{

	SHFILEOPSTRUCT lpFileOp;
	lpFileOp.hwnd=AfxGetMainWnd()->GetSafeHwnd();
	lpFileOp.wFunc=FO_DELETE;
	lpFileOp.pFrom=lpszFileName;
	lpFileOp.pTo=NULL;
	lpFileOp.fFlags=FOF_SILENT|FOF_NOCONFIRMATION;
	lpFileOp.fAnyOperationsAborted=FALSE;
	lpFileOp.hNameMappings =NULL;
	lpFileOp.lpszProgressTitle =NULL;
	int rval=SHFileOperation(&lpFileOp);
	CString strMsg;	
	if(rval==0)
	{
		
		if(lpFileOp.fAnyOperationsAborted==TRUE)
		{	
			strMsg="删除文件";
			strMsg+=lpszFileName;
			strMsg += "操作被取消!";
			::AfxMessageBox(strMsg,MB_OK);
			return FALSE;
		}
		else
		{
			::AfxMessageBox("删除文件操作成功!",MB_OK);			
			return TRUE;
		}
	}
	else
	{
		strMsg="删除文件 ";
		strMsg+=lpszFileName;		
		strMsg+=" 失败!";
		::AfxMessageBox(strMsg,MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}
}

BOOL CSetup::GetWindowsPath(CString &strWinPath, CString &strWinSysPath)
{
	char Path[MAX_PATH+1];		
	if(!GetWindowsDirectory(Path,MAX_PATH))
		return FALSE;	
	strWinPath=Path;
	if(GetSystemDirectory(Path,MAX_PATH))
		return FALSE;	
	strWinSysPath=Path;
	return TRUE;
}

BOOL CSetup::GetProgramGroupPath(CString& strPath)
{
	int nCSIDIndex=CSIDL_PROGRAMS;
	char Path[MAX_PATH+1];
	LPITEMIDLIST pidl;
	LPMALLOC pShellMalloc;
	if(SUCCEEDED(SHGetMalloc(&pShellMalloc)))
	{
		if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,
			nCSIDIndex,&pidl)))
		{
			if(SHGetPathFromIDList(pidl, Path))
			{			
				strPath=Path;
				return TRUE;
			}
			else//SHGetPathFromIDList(pidl, Path))
			{
				return FALSE;
			}
			pShellMalloc->Free(pidl);
		}
		else//SHGetSpecialFolderLocation
		{
			return FALSE;
		}
		pShellMalloc->Release();
	}
	else//SUCCEEDED(SHGetMalloc(&pShellMalloc)))
	{
		return FALSE;
	}
}

BOOL CSetup::GetDesktopPath(CString &strPath)
{
	int nCSIDIndex=CSIDL_DESKTOPDIRECTORY;
	char Path[MAX_PATH+1];
	LPITEMIDLIST pidl;
	LPMALLOC pShellMalloc;
	if(SUCCEEDED(SHGetMalloc(&pShellMalloc)))
	{
		if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,
			nCSIDIndex,&pidl)))
		{
			if(SHGetPathFromIDList(pidl, Path))
			{			
				strPath=Path;
				return TRUE;
			}
			else//SHGetPathFromIDList(pidl, Path))
			{
				return FALSE;
			}
			pShellMalloc->Free(pidl);
		}
		else//SHGetSpecialFolderLocation
		{
			return FALSE;
		}
		pShellMalloc->Release();
	}
	else//SUCCEEDED(SHGetMalloc(&pShellMalloc)))
	{
		return FALSE;
	}
}

BOOL CSetup::GetStartupPath(CString &strPath)
{
	int nCSIDIndex=CSIDL_STARTUP;
	char Path[MAX_PATH+1];
	LPITEMIDLIST pidl;
	LPMALLOC pShellMalloc;
	if(SUCCEEDED(SHGetMalloc(&pShellMalloc)))
	{
		if(SUCCEEDED(SHGetSpecialFolderLocation(NULL,
			nCSIDIndex,&pidl)))
		{
			if(SHGetPathFromIDList(pidl, Path))
			{			
				strPath=Path;
				return TRUE;
			}
			else//SHGetPathFromIDList(pidl, Path))
			{
				return FALSE;
			}
			pShellMalloc->Free(pidl);
		}
		else//SHGetSpecialFolderLocation
		{
			return FALSE;
		}
		pShellMalloc->Release();
	}
	else//SUCCEEDED(SHGetMalloc(&pShellMalloc)))
	{
		return FALSE;
	}
}

//建立多层目录
BOOL CSetup::CreatePath(LPCSTR lpszFullPathName)
{	
	CString dd;
	dd=lpszFullPathName;
	HANDLE		fFile;		// File Handle
	WIN32_FIND_DATA	fileinfo;	// File Information Structure
	CStringArray	m_arr;		// CString Array to hold Directory Structures
	BOOL tt;			// BOOL used to test if Create Directory was successful
	int x1 = 0;			// Counter
	CString tem = "";		// Temporary CString Object

	fFile = FindFirstFile(dd,&fileinfo);

	// if the file exists and it is a directory
	if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
	{
		//  Directory Exists close file and return
		FindClose(fFile);
		return FALSE;
	}
	m_arr.RemoveAll();

	for(x1 = 0; x1 < dd.GetLength(); x1++ )		// Parse the supplied CString Directory String
	{									
		if(dd.GetAt(x1) != '\\')		// if the Charachter is not a \ 
			tem += dd.GetAt(x1);		// add the character to the Temp String
		else
		{
			m_arr.Add(tem);			// if the Character is a \ 
			tem += "\\";			// Now add the \ to the temp string
		}
		if(x1 == dd.GetLength()-1)		// If we reached the end of the String
			m_arr.Add(tem);
	}

	// Close the file
	FindClose(fFile);

	// Now lets cycle through the String Array and create each directory in turn
	for(x1 = 1; x1 < m_arr.GetSize(); x1++)
	{
		tem = m_arr.GetAt(x1);
		tt = CreateDirectory(tem,NULL);

		// If the Directory exists it will return a false
		if(tt)
			SetFileAttributes(tem,FILE_ATTRIBUTE_NORMAL);
		// If we were successful we set the attributes to normal
	}
	//  Now lets see if the directory was successfully created
	fFile = FindFirstFile(dd,&fileinfo);

	m_arr.RemoveAll();
	if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
	{
		//  Directory Exists close file and return
		FindClose(fFile);
		return TRUE;
	}
	else
	{
		// For Some reason the Function Failed  Return FALSE
		FindClose(fFile);
		return FALSE;
	}
}

BOOL CSetup::AddToStartup(LPCSTR lpszFullFilePath, BOOL bRunOnce)
{
	HKEY hRegKey=NULL;
	CString str;
	CString strFileName;
	strFileName=lpszFullFilePath;

	if( bRunOnce )
	{
		str=_T("Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce");
	}
	else
	{
		str=_T("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
	}

	if(RegOpenKey(HKEY_LOCAL_MACHINE, str, &hRegKey) != ERROR_SUCCESS) 
	{		
		::AfxMessageBox("设置注册表失败!",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}//不能打开注册表
	else
	{
		_splitpath(lpszFullFilePath,NULL,NULL,str.GetBufferSetLength(MAX_PATH+1),NULL);
		//strFileName.ReleaseBuffer();
		str.ReleaseBuffer();
		if(::RegSetValueEx( hRegKey,str,0,REG_SZ,(CONST BYTE *)lpszFullFilePath,strFileName.GetLength() )!=ERROR_SUCCESS)
		{
			::AfxMessageBox("设置注册表失败!",MB_OK|MB_ICONEXCLAMATION);
			return FALSE;
		}//设置值失败	
		else
		{		
		::AfxMessageBox("设置注册表成功!",MB_OK);
		return TRUE;
		}
	}

}

BOOL CSetup::DeleteFolder(LPCSTR lpzsFolderName, DWORD dwDelFlags)
{
	//char FromFileName[80]="\0";
	//strcpy(FromFileName,lpzsFolderName);
	//strcat(FromFileName,"\0");

	SHFILEOPSTRUCT lpFileOp;
	lpFileOp.hwnd=AfxGetMainWnd()->GetSafeHwnd();
	lpFileOp.wFunc=FO_DELETE;
	lpFileOp.pFrom=lpzsFolderName;
	lpFileOp.pTo=NULL;
	lpFileOp.fFlags=FOF_NOCONFIRMATION|FOF_SILENT ;
	lpFileOp.fAnyOperationsAborted=FALSE;
	lpFileOp.hNameMappings =NULL;
	lpFileOp.lpszProgressTitle =NULL;
	int rval=SHFileOperation(&lpFileOp);
	if(rval==0)
	{
		if(lpFileOp.fAnyOperationsAborted==TRUE)
		{
			::AfxMessageBox("删除目录操作被取消!",MB_OK);
			return FALSE;
		}
		else
		{
			::AfxMessageBox("删除目录操作成功!",MB_OK);
			return TRUE;
		}
	}
	else
	{
		::AfxMessageBox("删除目录操作失败!",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}
}

BOOL CSetup::CreateShortCut(const CString strSrcPath, const CString strPathLink)
{
	BOOL bRet = FALSE;
	IShellLink* psl;

	if(SUCCEEDED( CoCreateInstance(CLSID_ShellLink,NULL,CLSCTX_INPROC_SERVER,
		IID_IShellLink,(LPVOID*) &psl))
					)//成功
	{
		IPersistFile* ppf;
		psl->SetPath(strSrcPath);
		psl->SetDescription("Shortcut");
		psl->SetShowCmd(SW_SHOW);

		if (SUCCEEDED(psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf)))
		{

			WORD wsz[MAX_PATH];
			MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, strPathLink, 
				-1, wsz,MAX_PATH);
			if ( SUCCEEDED ( ppf->Save(wsz, TRUE) ) )
				bRet = TRUE;
			ppf->Release();
		}
		psl->Release();
	}
	return bRet;
}

BOOL CSetup::CreateShortCutOnProgramGroup(LPCSTR lpszFullPathName,LPCSTR lpszLinkName)
{
	CString str;
	if(!GetProgramGroupPath(str))
		return FALSE;
	str+=lpszLinkName;
	if(CreateShortCut(lpszFullPathName,str))
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

BOOL CSetup::CreateShortCutOnDesktop(LPCSTR lpszFullPathName, LPCSTR lpszLinkName)
{
	CString str;
	if(!GetDesktopPath(str))
		return FALSE;
	str+=lpszLinkName;
	if(CreateShortCut(lpszFullPathName,str))
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

BOOL CSetup::ShellExecuteEx(LPCSTR lpsFileName,LPCSTR lpszParam, LPCSTR lpszMode)
{
	SHELLEXECUTEINFO Info;
	memset (&Info, 0, sizeof(Info)) ;
	Info.cbSize=sizeof (Info) ;
	Info.lpVerb=lpszMode ;
	Info.lpFile=lpsFileName;
	Info.lpParameters=lpszParam;
	Info.fMask=SEE_MASK_NOCLOSEPROCESS ;
	Info.nShow=SW_SHOWDEFAULT ;

	if (! ::ShellExecuteEx (&Info))
	{
		//::AfxMessageBox("创建进程失败!",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}
	else

⌨️ 快捷键说明

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