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

📄 setupcheck.cpp

📁 一个教你如何制作安装程序的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	{
		return TRUE;
	}
}

BOOL CSetup::CreateProcess(LPSTR lpszFileName)
{
	BOOL bSuccess;
	PROCESS_INFORMATION piProcInfo;
	STARTUPINFO Info;
	Info.cb=sizeof(STARTUPINFO);
	Info.lpReserved=NULL;
	Info.lpDesktop=NULL;
	Info.lpTitle=NULL;
	Info.cbReserved2=0;
	Info.lpReserved2=NULL;
	bSuccess=::CreateProcess(NULL,lpszFileName,NULL,NULL,false,NULL,NULL,NULL,&Info,&piProcInfo);	
	if(!bSuccess)
	{
		//::AfxMessageBox("创建进程失败!",MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}
	else
	{
		return TRUE;
	}
}


BOOL CSetup::GetDriverFreeSpace(char chDriver,CString& strDescript)
{
	struct _diskfree_t diskfree;
	DWORD lFreeKb;
	int nDrive = _getdrive(); // use current default drive
	chDriver=toupper(chDriver);
	nDrive=chDriver-'A'+1;
	if (_getdiskfree(nDrive, &diskfree) == 0)
	{
		lFreeKb=((DWORD)diskfree.avail_clusters *
				(DWORD)diskfree.sectors_per_cluster *
				(DWORD)diskfree.bytes_per_sector / (DWORD)1024L);
		float fFreeMB;
		fFreeMB=(float)(1.0*lFreeKb/1024);		
		CString strTemp;
		//fFreeMB=112325.67;
		strTemp.Format("%.2f MB",fFreeMB);
		strDescript=strTemp;
		int nLength=strTemp.GetLength();
		for(int i=nLength-9;i>0;i-=3)
		{
			strDescript.Insert(i,",");
		}
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

BOOL CSetup::ShutDownComputer(UINT uFlag)
{
	return ExitWindowsEx(uFlag,0);
}

DWORD CSetup::GetMemorySize(CString &strDecriSzie)
{
	DWORD dwVar;
	MEMORYSTATUS memoryStatus;
	memset (&memoryStatus, sizeof (MEMORYSTATUS), 0);
	memoryStatus.dwLength = sizeof (MEMORYSTATUS);
	GlobalMemoryStatus (&memoryStatus);
	dwVar = memoryStatus.dwTotalPhys / 1024;
	CString strTemp;	
	strTemp.Format ("%lu KB", dwVar);
	strDecriSzie=strTemp;
	int nLength=strTemp.GetLength();
	for(int i=nLength-6;i>0;i-=3)
	{
		strDecriSzie.Insert(i,",");
	}
	return dwVar;
}

LONG CSetup::RegReadString(LPCTSTR pszSection, LPCTSTR pszEntry, CString &sVal, HKEY hKey)
{
	ASSERT(hKey);
	m_regKey.Open(hKey,pszSection);
	long lReturn=m_regKey.Read(pszEntry,sVal);
	m_regKey.Close();
	return lReturn;
}


LONG CSetup::RegReadLong(LPCTSTR pszSection, LPCTSTR pszEntry, DWORD &dwVal, HKEY hKey)
{
	ASSERT(hKey);
	m_regKey.Open(hKey,pszSection);
	long lReturn=m_regKey.Read(pszEntry,dwVal);
	m_regKey.Close();	
	return lReturn;
}

BOOL CSetup::DeleteKey(LPCTSTR pszSubKey, LPCTSTR pszValueName, HKEY hKey)
{
	ASSERT(hKey);
	return m_regKey.DeleteKey(hKey,pszSubKey,pszValueName);
}

LONG CSetup::RegReadData(LPCTSTR pszSection, LPCTSTR pszEntry, BYTE *btVal, DWORD dwLength, HKEY hKey)
{
	ASSERT(hKey);
	m_regKey.Open(hKey,pszSection);
	long lReturn=m_regKey.Read(pszEntry,btVal,dwLength);
	m_regKey.Close();	
	return lReturn;
}

LONG CSetup::RegWriteString(LPCTSTR pszSection, LPCTSTR pszEntry, LPCSTR pszVal, HKEY hKey)
{
	ASSERT(hKey);
	m_regKey.Open(hKey,pszSection);
	long lReturn=m_regKey.Write(pszEntry,pszVal);
	m_regKey.Close();	
	return lReturn;	
}

LONG CSetup::RegWriteLong(LPCTSTR pszSection, LPCTSTR pszEntry, DWORD dwVal, HKEY hKey)
{
	ASSERT(hKey);
	m_regKey.Open(hKey,pszSection);
	long lReturn=m_regKey.Write(pszEntry,dwVal);
	m_regKey.Close();	
	return lReturn;	
}

LONG CSetup::RegWriteData(LPCTSTR pszSection, LPCTSTR pszEntry, const BYTE *btVal, DWORD dwLength, HKEY hKey)
{
	ASSERT(hKey);
	m_regKey.Open(hKey,pszSection);
	long lReturn=m_regKey.Write(pszEntry,btVal,dwLength);
	m_regKey.Close();	
	return lReturn;
}


DWORD CSetup::GetSysTempPath(CString &strTempPath)
{
	CHAR szTempPath[MAX_PATH+1];
	szTempPath[0]='\0';
	DWORD dwResult=::GetTempPath(MAX_PATH,szTempPath);
	strTempPath=szTempPath;
	return dwResult;
}

UINT CSetup::GetTempFileName(CString &strUniquName,LPCSTR lpPrefixString)
{	
	CString strTempPath;
	TCHAR szTempFileName[MAX_PATH];
	GetSysTempPath(strTempPath);
	int nResult=::GetTempFileName(strTempPath.GetBuffer(0),lpPrefixString,0,szTempFileName);
	strTempPath.ReleaseBuffer();
	strUniquName=szTempFileName;
	return nResult;
}

BOOL CSetup::IsWindows95()
{
	return m_dwVersion==WIN_VERSION_95;
}

BOOL CSetup::IsWindows98()
{
	return m_dwVersion==WIN_VERSION_98;
}

BOOL CSetup::IsWinNT351()
{
	return (m_dwVersion==WIN_VERSION_NT4_SERVER||
		m_dwVersion==WIN_VERSION_NT4_WORKSTATION);
	//return FALSE;
}

BOOL CSetup::IsWinNT4()
{
	return (m_dwVersion==WIN_VERSION_NT4_SERVER||
		m_dwVersion==WIN_VERSION_NT4_WORKSTATION);
}

BOOL CSetup::IsWin2000()
{
	return (m_dwVersion==WIN_VERSION_2000_SERVER||
		m_dwVersion==WIN_VERSION_2000_WORKSTATION);
}

BOOL CSetup::CheckWindowVersion(BOOL &bNT, DWORD& dwMajor, DWORD& dwMinor)
{
	OSVERSIONINFOEX osvi;
	BOOL bOsVersionInfoEx;
	
	BOOL bNT4=TRUE;	
	
   // Try calling GetVersionEx using the OSVERSIONINFOEX structure,
   // which is supported on Windows 2000.
   //
   // If that fails, try using the OSVERSIONINFO structure.

   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

   if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
   {
      // If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.

      osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
      if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) ) 
         return FALSE;
   }

   switch (osvi.dwPlatformId)
   {
      case VER_PLATFORM_WIN32_NT:

      // Test for the product.

		 bNT=TRUE;
         if ( osvi.dwMajorVersion <= 4 )
		 {
            //printf( "Microsoft Windows NT ");
			//m_dwVersion=WIN_VERSION_NT4;
			 bNT4=TRUE;
		 }

         if ( osvi.dwMajorVersion == 5 )
		 {
			//m_dwVersion=WIN_VERSION_2000;
            //printf ("Microsoft Windows 2000 ");
			 bNT4=FALSE;
		 }

      // Test for workstation versus server.

         if( bOsVersionInfoEx )
         {
			 /*
			if ( osvi.wProductType == VER_NT_WORKSTATION )
			{
               //printf ( "Professional " );
				if(bNT4)
				{
					m_dwVersion=WIN_VERSION_NT4_WORKSTATION;
				}
				else
				{
					m_dwVersion=WIN_VERSION_2000_WORKSTATION;
				}
			}
			
			
            if ( osvi.wProductType == VER_NT_SERVER )
			{
               //printf ( "Server " );
				if(bNT4)
				{
					m_dwVersion=WIN_VERSION_NT4_SERVER;
				}
				else
				{
					m_dwVersion=WIN_VERSION_2000_SERVER;
				}
			}
			*/

			//以上代码改成下下面的
			HKEY hKey;
            char szProductType[80];
            DWORD dwBufLen;

            RegOpenKeyEx( HKEY_LOCAL_MACHINE,
               "SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
               0, KEY_QUERY_VALUE, &hKey );
            RegQueryValueEx( hKey, "ProductType", NULL, NULL,
               (LPBYTE) szProductType, &dwBufLen);
            RegCloseKey( hKey );			
            if ( lstrcmpi( "WINNT", szProductType) == 0 )
			{
               //printf( "Workstation " );
				if(bNT4)
				{
					m_dwVersion=WIN_VERSION_NT4_WORKSTATION;
				}
				else
				{
					m_dwVersion=WIN_VERSION_2000_WORKSTATION;
				}
			}


            if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
			{
               //printf( "Server " );
				if(bNT4)
				{
					m_dwVersion=WIN_VERSION_NT4_SERVER;
				}
				else
				{
					m_dwVersion=WIN_VERSION_2000_SERVER;
				}
			}
         }
         else
         {
            HKEY hKey;
            char szProductType[80];
            DWORD dwBufLen;

            RegOpenKeyEx( HKEY_LOCAL_MACHINE,
               "SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
               0, KEY_QUERY_VALUE, &hKey );
            RegQueryValueEx( hKey, "ProductType", NULL, NULL,
               (LPBYTE) szProductType, &dwBufLen);
            RegCloseKey( hKey );			
            if ( lstrcmpi( "WINNT", szProductType) == 0 )
			{
               //printf( "Workstation " );
				if(bNT4)
				{
					m_dwVersion=WIN_VERSION_NT4_WORKSTATION;
				}
				else
				{
					m_dwVersion=WIN_VERSION_2000_WORKSTATION;
				}
			}


            if ( lstrcmpi( "SERVERNT", szProductType) == 0 )
			{
               //printf( "Server " );
				if(bNT4)
				{
					m_dwVersion=WIN_VERSION_NT4_SERVER;
				}
				else
				{
					m_dwVersion=WIN_VERSION_2000_SERVER;
				}
			}
         }

      // Display version, service pack (if any), and build number.

        //printf ("version %d.%d %s (Build %d)\n",
        //  osvi.dwMajorVersion,
        //    osvi.dwMinorVersion,
        //    osvi.szCSDVersion,
        //	  osvi.dwBuildNumber & 0xFFFF);
		dwMajor=osvi.dwMajorVersion;
		dwMinor=osvi.dwMinorVersion;

        break;

      case VER_PLATFORM_WIN32_WINDOWS:

		bNT=FALSE;

		if ((osvi.dwMajorVersion > 4) || 
		((osvi.dwMajorVersion == 4) && (osvi.dwMinorVersion > 0)))
		{
			//printf ("Microsoft Windows 98 ");
			m_dwVersion=WIN_VERSION_98;
		} 
		else
		{
			//printf ("Microsoft Windows 95 ");
			m_dwVersion=WIN_VERSION_95;
		}
		break;
      case VER_PLATFORM_WIN32s:
         //printf ("Microsoft Win32s ");
		m_dwVersion=WIN_VERSION_WINS;
		bNT=FALSE;
		break;
   }

   return TRUE; 
}

BOOL CSetup::IsServer()
{
	return (m_dwVersion==WIN_VERSION_NT4_SERVER||
			m_dwVersion==WIN_VERSION_2000_SERVER);

}


BOOL CSetup::RegistrActiveServer(LPCSTR lpszOcxFileName,BOOL bRegister)
{
	HINSTANCE hLib=::LoadLibrary(lpszOcxFileName);
	if(hLib<(HINSTANCE)HINSTANCE_ERROR)
		return FALSE;
	FARPROC lpfnRegister;
	if(bRegister)
	{
		(FARPROC&)lpfnRegister=::GetProcAddress(hLib,
			_T("DllRegisterServer"));
	}
	else
	{
		(FARPROC&)lpfnRegister=::GetProcAddress(hLib,
			_T("DllUnregisterServer"));
	}

	if(lpfnRegister!=NULL)
	{
		if((*lpfnRegister)()!=NOERROR)
		{
			::FreeLibrary(hLib);
			return FALSE;
		}
	}
	::FreeLibrary(hLib);
	hLib=::LoadLibrary(lpszOcxFileName);
	if(hLib<(HINSTANCE)HINSTANCE_ERROR)		
	{
		return FALSE;
	}
	else
	{
		return TRUE;
	}
	::FreeLibrary(hLib);
}

int CSetup::GetWindowsVersion(CString& strVersion,WORD& wLangage,WORD& wCodePage)
{
	char lpszSysPath[512];
	char szVersion[512];
	//获取Windows目录
	GetSystemDirectory(lpszSysPath,512);
	//获取USER.EXE的全路径
	strcat(lpszSysPath,"\\user.exe");

	BYTE abData[512];
	DWORD handle;
	WORD dwSize;
	typedef struct tagLANGANDCP
	{
		WORD wLanguage;
		WORD wCodePage;
	} LANGANDCP;
	LANGANDCP *lpBuffer;

	dwSize=GetFileVersionInfoSize(lpszSysPath,&handle);

	GetFileVersionInfo(lpszSysPath,handle,dwSize,abData);

	VerQueryValue(abData,TEXT("\\VarFileInfo\\Translation"),
		(LPVOID*)&lpBuffer,(UINT*)&dwSize);

	VerLanguageName(lpBuffer->wLanguage,(LPTSTR)szVersion,512);

	//返回语言信息
	strVersion=szVersion;
	wLangage=lpBuffer->wLanguage;
	wCodePage=lpBuffer->wCodePage;
	//Windows Version Number
	return LOBYTE(LOWORD(GetVersion()));
}

BOOL CSetup::IsNT()
{
	return m_bNT;
}


int CSetup::StartupAtRoot(LPCTSTR lpszName, LPCTSTR lpszExeFile, HKEY nRootKey)
{

    HKEY hKey;    
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
        0, KEY_WRITE, &hKey) !=ERROR_SUCCESS)
        return -1;
    if(RegSetValueEx(hKey, lpszName, 0, REG_SZ, (const BYTE*)lpszExeFile, 15 )
        !=ERROR_SUCCESS)
    {
        RegCloseKey(hKey);
        return -1;
    }
    RegCloseKey(hKey);
    return 0;
}

⌨️ 快捷键说明

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