Example #1
0
bool IsWinVerEqual(WORD OsVer)
{
	OSVERSIONINFOEXW osvi = {sizeof(osvi), HIBYTE(OsVer), LOBYTE(OsVer)};
	DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_EQUAL), VER_MINORVERSION, VER_EQUAL);
	BOOL ibIsWinOrHigher = VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask);
	return (ibIsWinOrHigher != FALSE);
}
Example #2
0
bool IsUserAdmin()
{
	// No need to show any "Shield" on XP or 2k
	_ASSERTE(_WIN32_WINNT_VISTA==0x600);
	OSVERSIONINFOEXW osvi = {sizeof(osvi), HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA)};
	DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL);
	if (!VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask))
		return false;

	BOOL b;
	SID_IDENTIFIER_AUTHORITY NtAuthority = {SECURITY_NT_AUTHORITY};
	PSID AdministratorsGroup;
	b = AllocateAndInitializeSid(
			&NtAuthority,
			2,
			SECURITY_BUILTIN_DOMAIN_RID,
			DOMAIN_ALIAS_RID_ADMINS,
			0, 0, 0, 0, 0, 0,
			&AdministratorsGroup);

	if (b)
	{
		if (!CheckTokenMembership(NULL, AdministratorsGroup, &b))
		{
			b = FALSE;
		}

		FreeSid(AdministratorsGroup);
	}

	return (b ? true : false);
}
Example #3
0
VERSIONHELPERAPI
IsWindowsServer()
{
	OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0,{ 0 }, 0, 0, 0, VER_NT_WORKSTATION };
	DWORDLONG        const dwlConditionMask = VerSetConditionMask(0, VER_PRODUCT_TYPE, VER_EQUAL);

	return !VerifyVersionInfoW(&osvi, VER_PRODUCT_TYPE, dwlConditionMask);
}
Example #4
0
// Only 5.x family (Win2k, WinXP, Win 2003 server)
bool IsWin5family()
{
	static int ibIsWin5fam = 0;
	if (!ibIsWin5fam)
	{
		OSVERSIONINFOEXW osvi = {sizeof(osvi), 5, 0};
		DWORDLONG const dwlConditionMask = VerSetConditionMask(0, VER_MAJORVERSION, VER_EQUAL);
		ibIsWin5fam = VerifyVersionInfoW(&osvi, VER_MAJORVERSION, dwlConditionMask) ? 1 : -1;
	}
	return (ibIsWin5fam == 1);
}
Example #5
0
void DebugVersionTest()
{
	DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL);

	_ASSERTE(_WIN32_WINNT_WIN7==0x601);
	OSVERSIONINFOEXW osvi7 = {sizeof(osvi7), HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7)};
	bool bWin7 = VerifyVersionInfoW(&osvi7, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask) != 0;

	_ASSERTE(_WIN32_WINNT_VISTA==0x600);
	OSVERSIONINFOEXW osvi6 = {sizeof(osvi6), HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA)};
	bool bWin6 = VerifyVersionInfoW(&osvi6, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask) != 0;

	OSVERSIONINFOW osv = {sizeof(OSVERSIONINFOW)};
	GetVersionExW(&osv);
	bool bVerWin7 = ((osv.dwMajorVersion > 6) || ((osv.dwMajorVersion == 6) && (osv.dwMinorVersion >= 1)));
	bool bVerWin6 = (osv.dwMajorVersion >= 6);

	_ASSERTE(bWin7 == bVerWin7);
	_ASSERTE(bWin6 == bVerWin6);
}
Example #6
0
/**
 * g_win32_check_windows_version:
 * @major: major version of Windows
 * @minor: minor version of Windows
 * @spver: Windows Service Pack Level, 0 if none
 * @os_type: Type of Windows OS
 *
 * Returns whether the version of the Windows operating system the
 * code is running on is at least the specified major, minor and
 * service pack versions.  See MSDN documentation for the Operating
 * System Version.  Software that needs even more detailed version and
 * feature information should use the Win32 API VerifyVersionInfo()
 * directly.
 *
 * Successive calls of this function can be used for enabling or
 * disabling features at run-time for a range of Windows versions,
 * as per the VerifyVersionInfo() API documentation.
 *
 * Returns: %TRUE if the Windows Version is the same or greater than
 *          the specified major, minor and service pack versions, and
 *          whether the running Windows is a workstation or server edition
 *          of Windows, if specifically specified.
 *
 * Since: 2.44
 **/
gboolean
g_win32_check_windows_version (const gint major,
                               const gint minor,
                               const gint spver,
                               const GWin32OSType os_type)
{
  OSVERSIONINFOEXW osverinfo;
  gboolean test_os_type;
  const DWORDLONG conds = gwin32condmask (gwin32condmask (gwin32condmask (0, VER_MAJORVERSION), VER_MINORVERSION), VER_SERVICEPACKMAJOR);

  memset (&osverinfo, 0, sizeof (OSVERSIONINFOEXW));
  osverinfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEXW);
  osverinfo.dwPlatformId = VER_PLATFORM_WIN32_NT;
  osverinfo.dwMajorVersion = major;
  osverinfo.dwMinorVersion = minor;
  osverinfo.wServicePackMajor = spver;

  switch (os_type)
    {
      case G_WIN32_OS_WORKSTATION:
        osverinfo.wProductType = VER_NT_WORKSTATION;
        test_os_type = TRUE;
        break;
      case G_WIN32_OS_SERVER:
        osverinfo.wProductType = VER_NT_SERVER;
        test_os_type = TRUE;
        break;
      default:
        test_os_type = FALSE;
        break;
    }

  if (test_os_type)
    return VerifyVersionInfoW (&osverinfo,
                               VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_PRODUCT_TYPE,
                               gwin32condmask (conds, VER_PRODUCT_TYPE));
  else
    return VerifyVersionInfoW (&osverinfo,
                               VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR,
                               conds);
}
Example #7
0
bool IsWin10()
{
	static int ibIsWin10 = 0;
	if (!ibIsWin10)
	{
		#define _WIN32_WINNT_WIN10 0x604
		OSVERSIONINFOEXW osvi = {sizeof(osvi), HIBYTE(_WIN32_WINNT_WIN10), LOBYTE(_WIN32_WINNT_WIN10)};
		DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL);
		ibIsWin10 = VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask) ? 1 : -1;
	}
	return (ibIsWin10 == 1);
}
Example #8
0
// Only 5.x family (Win2k, WinXP, Win 2003 server)
bool IsWin5family()
{
	static int ibIsWin5fam = 0;
	if (!ibIsWin5fam)
	{
		// Don't use IsWinVerEqual here - we need to compare only major version!
		OSVERSIONINFOEXW osvi = {sizeof(osvi), 5, 0};
		DWORDLONG const dwlConditionMask = VerSetConditionMask(0, VER_MAJORVERSION, VER_EQUAL);
		ibIsWin5fam = VerifyVersionInfoW(&osvi, VER_MAJORVERSION, dwlConditionMask) ? 1 : -1;
	}
	return (ibIsWin5fam == 1);
}
Example #9
0
//Verify version of system(Greater than Windows Vista)
	BOOL WINAPI IsGreaterThanVista(void)
	{
		std::shared_ptr<OSVERSIONINFOEXW> OSVI(new OSVERSIONINFOEXW());
		memset(OSVI.get(), 0, sizeof(OSVERSIONINFOEXW));
		DWORDLONG dwlConditionMask = 0;

	//Initialization
		ZeroMemory(OSVI.get(), sizeof(OSVERSIONINFOEXW));
		OSVI->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
		OSVI->dwMajorVersion = 6U; //Greater than Windows Vista.
		OSVI->dwMinorVersion = 0;

	//System Major version > dwMajorVersion
		VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_GREATER);
		if (VerifyVersionInfoW(OSVI.get(), VER_MAJORVERSION, dwlConditionMask))
			return TRUE;

	//Sytem Major version = dwMajorVersion and Minor version > dwMinorVersion
		VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);
		VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_GREATER);
		return VerifyVersionInfoW(OSVI.get(), VER_MAJORVERSION|VER_MINORVERSION, dwlConditionMask);
	}
Example #10
0
// Only Windows 2000
bool IsWin2kEql()
{
	static int ibIsWin2K = 0;
	if (!ibIsWin2K)
	{
		OSVERSIONINFOEXW osvi = {sizeof(osvi), HIBYTE(_WIN32_WINNT_WIN2K), LOBYTE(_WIN32_WINNT_WIN2K)};
		DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0,
			VER_MAJORVERSION, VER_EQUAL),
			VER_MINORVERSION, VER_EQUAL);
		ibIsWin2K = VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) ? 1 : -1;;
	}
	return (ibIsWin2K == 1);
}
Example #11
0
 inline bool IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
 {
	OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0, { 0 }, 0, 0 };
	DWORDLONG const dwlConditionMask = VerSetConditionMask(
		VerSetConditionMask(
		VerSetConditionMask(
		0, VER_MAJORVERSION, VER_GREATER_EQUAL),
		VER_MINORVERSION, VER_GREATER_EQUAL),
		VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
	osvi.dwMajorVersion = wMajorVersion;
	osvi.dwMinorVersion = wMinorVersion;
	osvi.wServicePackMajor = wServicePackMajor;
	return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
 }
Example #12
0
// Mostly taken from the Windows 8.1 SDK's VersionHelpers.h.
static bool IsWindowsVistaOrGreater()
{
	OSVERSIONINFOEXW osvi = {sizeof(osvi), 0, 0, 0, 0, {0}, 0, 0};

	osvi.dwMajorVersion = HIBYTE(_WIN32_WINNT_VISTA);
	osvi.dwMinorVersion = LOBYTE(_WIN32_WINNT_VISTA);
	osvi.wServicePackMajor = 0;

	DWORDLONG majorversionmask = VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL);
	DWORDLONG versionmask = VerSetConditionMask(majorversionmask, VER_MINORVERSION, VER_GREATER_EQUAL);
	DWORDLONG mask = VerSetConditionMask(versionmask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);

	return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, mask) != FALSE;
}
Example #13
0
bool IsHwFullScreenAvailable()
{
	if (IsWindows64())
		return false;

	// HW FullScreen was available in Win2k & WinXP (32bit)
	_ASSERTE(_WIN32_WINNT_VISTA==0x600);
	OSVERSIONINFOEXW osvi = {sizeof(osvi), HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA)};
	DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL);
	if (VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask))
		return false; // Vista or higher - not available
	else
		return true;
}
Example #14
0
BOOL GetVersionMetric(WINVER_METRIC metric)
{
    // NB: OSVERSIONINFOEX is not supported on very early platforms but
    //     those don't require such a "metric"
    OSVERSIONINFOEXW ovi = { 0 };
    ovi.dwOSVersionInfoSize = sizeof(ovi);

    ULONGLONG dwlConditionMask = 0;

    switch (metric)
    {
        case WINVER_METRIC::ANY:
            return TRUE;

        case WINVER_METRIC::SERVER:
            ovi.wProductType = VER_NT_WORKSTATION;
            dwlConditionMask = VerSetConditionMask(dwlConditionMask, VER_PRODUCT_TYPE, VER_EQUAL);
            return VerifyVersionInfoW(&ovi, VER_PRODUCT_TYPE, dwlConditionMask) == FALSE;

        case WINVER_METRIC::WORKSTATION:
            ovi.wProductType = VER_NT_WORKSTATION;
            dwlConditionMask = VerSetConditionMask(dwlConditionMask, VER_PRODUCT_TYPE, VER_EQUAL);
            return VerifyVersionInfoW(&ovi, VER_PRODUCT_TYPE, dwlConditionMask) != FALSE;

        case WINVER_METRIC::HOMESERVER:
            ovi.wSuiteMask = VER_SUITE_WH_SERVER;
            dwlConditionMask = VerSetConditionMask(dwlConditionMask, VER_SUITENAME, VER_EQUAL);
            return VerifyVersionInfoW(&ovi, VER_SUITENAME, dwlConditionMask) != FALSE;

        default:
            ASSERT(false);
            break;
    }

    return FALSE;
}
Example #15
0
// WinXP SP1 or higher
bool IsWinXPSP1()
{
	static int ibIsWinXPSP1 = 0;
	if (!ibIsWinXPSP1)
	{
		OSVERSIONINFOEXW osvi = {sizeof(osvi), HIBYTE(_WIN32_WINNT_WINXP), LOBYTE(_WIN32_WINNT_WINXP)};
		osvi.wServicePackMajor = 1;
		DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(VerSetConditionMask(0,
			VER_MAJORVERSION, VER_GREATER_EQUAL),
			VER_MINORVERSION, VER_GREATER_EQUAL),
			VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
		ibIsWinXPSP1 = VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) ? 1 : -1;;
	}
	return (ibIsWinXPSP1 == 1);
}
Example #16
0
/******************************************************************************
 *        VerifyVersionInfoA   (KERNEL32.@)
 */
BOOL WINAPI VerifyVersionInfoA( LPOSVERSIONINFOEXA lpVersionInfo, DWORD dwTypeMask,
                                DWORDLONG dwlConditionMask)
{
    OSVERSIONINFOEXW verW;

    verW.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
    verW.dwMajorVersion = lpVersionInfo->dwMajorVersion;
    verW.dwMinorVersion = lpVersionInfo->dwMinorVersion;
    verW.dwBuildNumber = lpVersionInfo->dwBuildNumber;
    verW.dwPlatformId = lpVersionInfo->dwPlatformId;
    verW.wServicePackMajor = lpVersionInfo->wServicePackMajor;
    verW.wServicePackMinor = lpVersionInfo->wServicePackMinor;
    verW.wSuiteMask = lpVersionInfo->wSuiteMask;
    verW.wProductType = lpVersionInfo->wProductType;
    verW.wReserved = lpVersionInfo->wReserved;

    return VerifyVersionInfoW(&verW, dwTypeMask, dwlConditionMask);
}
Example #17
0
bool BrowserFactory::IsWindowsVersionOrGreater(unsigned short major_version,
                                               unsigned short minor_version,
                                               unsigned short service_pack) {
  OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0,{ 0 }, 0, 0 };
  DWORDLONG        const dwlConditionMask = VerSetConditionMask(
    VerSetConditionMask(
      VerSetConditionMask(
        0, VER_MAJORVERSION, VER_GREATER_EQUAL),
      VER_MINORVERSION, VER_GREATER_EQUAL),
    VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);

  osvi.dwMajorVersion = major_version;
  osvi.dwMinorVersion = minor_version;
  osvi.wServicePackMajor = service_pack;

  return VerifyVersionInfoW(&osvi,
                            VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR,
                            dwlConditionMask) != FALSE;
}
Example #18
0
static BOOL
IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
{
    OSVERSIONINFOEXW osvi;
    DWORDLONG const dwlConditionMask = VerSetConditionMask(
        VerSetConditionMask(
        VerSetConditionMask(
        0, VER_MAJORVERSION, VER_GREATER_EQUAL ),
        VER_MINORVERSION, VER_GREATER_EQUAL ),
        VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL );

    SDL_zero(osvi);
    osvi.dwOSVersionInfoSize = sizeof(osvi);
    osvi.dwMajorVersion = wMajorVersion;
    osvi.dwMinorVersion = wMinorVersion;
    osvi.wServicePackMajor = wServicePackMajor;

    return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
}
Example #19
0
UINT16 GetWinBuildNumber()
{
	UINT16 buildNumbers[] = { 10130, 10240, 14393 };
	OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0,{ 0 }, 0, 0 };
	ULONGLONG mask = ::VerSetConditionMask(0, VER_BUILDNUMBER, VER_EQUAL);



	for (size_t i = 0; i < sizeof(buildNumbers) / sizeof(buildNumbers[0]); i++)
	{
		osvi.dwBuildNumber = buildNumbers[i];
		if (VerifyVersionInfoW(&osvi, VER_BUILDNUMBER, mask) != FALSE)
		{
			return buildNumbers[i];
		}
	}
	
	return 0;
}
Example #20
0
/*
 * @implemented
 */
BOOL
WINAPI
VerifyVersionInfoA(IN LPOSVERSIONINFOEXA lpVersionInformation,
                   IN DWORD dwTypeMask,
                   IN DWORDLONG dwlConditionMask)
{
    OSVERSIONINFOEXW viex;

    /* NOTE: szCSDVersion is ignored, we don't need to convert it to Unicode */
    viex.dwOSVersionInfoSize = sizeof(viex);
    viex.dwMajorVersion = lpVersionInformation->dwMajorVersion;
    viex.dwMinorVersion = lpVersionInformation->dwMinorVersion;
    viex.dwBuildNumber = lpVersionInformation->dwBuildNumber;
    viex.dwPlatformId = lpVersionInformation->dwPlatformId;
    viex.wServicePackMajor = lpVersionInformation->wServicePackMajor;
    viex.wServicePackMinor = lpVersionInformation->wServicePackMinor;
    viex.wSuiteMask = lpVersionInformation->wSuiteMask;
    viex.wProductType = lpVersionInformation->wProductType;
    viex.wReserved = lpVersionInformation->wReserved;
    return VerifyVersionInfoW(&viex, dwTypeMask, dwlConditionMask);
}
Example #21
0
bool CheckLdrNotificationAvailable()
{
	static bool bLdrWasChecked = false;
	if (bLdrWasChecked)
		return gbLdrDllNotificationUsed;

	#ifndef _WIN32_WINNT_WIN8
	#define _WIN32_WINNT_WIN8 0x602
	#endif
	_ASSERTE(_WIN32_WINNT_WIN8==0x602);
	OSVERSIONINFOEXW osvi = {sizeof(osvi), HIBYTE(_WIN32_WINNT_WIN8), LOBYTE(_WIN32_WINNT_WIN8)};
	DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL);
	BOOL isAllowed = VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask);

	// LdrDllNotification работает так как нам надо начиная с Windows 8
	// В предыдущих версиях Windows нотификатор вызывается из LdrpFindOrMapDll
	// ДО того, как были обработаны импорты функцией LdrpProcessStaticImports (а точнее LdrpSnapThunk)

	if (isAllowed)
	{
		HMODULE hNtDll = GetModuleHandle(L"ntdll.dll");
		if (hNtDll)
		{
			LdrRegisterDllNotification = (LdrRegisterDllNotification_t)GetProcAddress(hNtDll, "LdrRegisterDllNotification");
			LdrUnregisterDllNotification = (LdrUnregisterDllNotification_t)GetProcAddress(hNtDll, "LdrUnregisterDllNotification");

			if (LdrRegisterDllNotification && LdrUnregisterDllNotification)
			{
				gnLdrDllNotificationState = LdrRegisterDllNotification(0, LdrDllNotification, NULL, &gpLdrDllNotificationCookie);
				gbLdrDllNotificationUsed = (gnLdrDllNotificationState == 0/*STATUS_SUCCESS*/);
			}
		}
	}

	bLdrWasChecked = true;

	return gbLdrDllNotificationUsed;
}
Example #22
0
void SetUserFriendlyFont(HWND hConWnd, int newFontY = 0, int newFontX = 0)
{
	// Соответствующие функции появились только в API Vista
	// Win2k & WinXP - доступны только хаки, что не подходит
	_ASSERTE(_WIN32_WINNT_VISTA==0x600);
	OSVERSIONINFOEXW osvi = {sizeof(osvi), HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA)};
	DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL);
	if (!VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask))
		return;

	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD crVisibleSize = {};
	CONSOLE_SCREEN_BUFFER_INFO csbi = {};
	if (GetConsoleScreenBufferInfo(hOutput, &csbi))
	{
		crVisibleSize.X = csbi.srWindow.Right - csbi.srWindow.Left + 1;
		crVisibleSize.Y = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
	}

	if ((crVisibleSize.X <= 0) || (crVisibleSize.Y <= 0))
	{
		_ASSERTE((crVisibleSize.X > 0) && (crVisibleSize.Y > 0));
		return;
	}

	int curSizeY = 0, curSizeX = 0;
	wchar_t sFontName[LF_FACESIZE] = L"";

	if (apiGetConsoleFontSize(hOutput, curSizeY, curSizeX, sFontName) && curSizeY && curSizeX)
	{
		if (newFontY <= 0 || newFontX <= 0)
		{
			DEBUGTEST(COORD crLargest = MyGetLargestConsoleWindowSize(hOutput));

			HMONITOR hMon = MonitorFromWindow(hConWnd, MONITOR_DEFAULTTOPRIMARY);
			MONITORINFO mi = {sizeof(mi)};
			int nMaxX = 0, nMaxY = 0;
			if (GetMonitorInfo(hMon, &mi))
			{
				nMaxX = mi.rcWork.right - mi.rcWork.left - 2*GetSystemMetrics(SM_CXSIZEFRAME) - GetSystemMetrics(SM_CYCAPTION);
				nMaxY = mi.rcWork.bottom - mi.rcWork.top - 2*GetSystemMetrics(SM_CYSIZEFRAME);
			}

			if ((nMaxX > 0) && (nMaxY > 0))
			{
				int nFontX = nMaxX  / crVisibleSize.X;
				int nFontY = nMaxY / crVisibleSize.Y;
				// Too large height?
				if (nFontY > 28)
				{
					nFontX = 28 * nFontX / nFontY;
					nFontY = 28;
				}

				// Evaluate default width for the font
				int nEvalX = EvaluateDefaultFontWidth(nFontY, sFontName);
				if (nEvalX > 0)
				{
					if ((nEvalX > nFontX) && (nFontX > 0))
						nFontY = nFontX * nFontY / nEvalX;
					else
						nFontX = nEvalX;
				}

				// Look in the registry?
				HKEY hk;
				DWORD nRegSize = 0, nLen;
				if (!RegOpenKeyEx(HKEY_CURRENT_USER, L"Console", 0, KEY_READ, &hk))
				{
					if (RegQueryValueEx(hk, L"FontSize", NULL, NULL, (LPBYTE)&nRegSize, &(nLen=sizeof(nRegSize))) || nLen!=sizeof(nRegSize))
						nRegSize = 0;
					RegCloseKey(hk);
				}
				if (!nRegSize && !RegOpenKeyEx(HKEY_CURRENT_USER, L"Console\\%SystemRoot%_system32_cmd.exe", 0, KEY_READ, &hk))
				{
					if (RegQueryValueEx(hk, L"FontSize", NULL, NULL, (LPBYTE)&nRegSize, &(nLen=sizeof(nRegSize))) || nLen!=sizeof(nRegSize))
						nRegSize = 0;
					RegCloseKey(hk);
				}
				if ((HIWORD(nRegSize) > curSizeY) && (HIWORD(nRegSize) < nFontY)
					&& (LOWORD(nRegSize) > curSizeX) && (LOWORD(nRegSize) < nFontX))
				{
					nFontY = HIWORD(nRegSize);
					nFontX = LOWORD(nRegSize);
				}

				if ((nFontX > curSizeX) || (nFontY > curSizeY))
				{
					newFontY = nFontY; newFontX = nFontX;
				}
			}
		}
	}

	if ((newFontY > 0) && (newFontX > 0))
	{
		if (!*sFontName)
			lstrcpyn(sFontName, L"Lucida Console", countof(sFontName));
		apiSetConsoleFontSize(hOutput, newFontY, newFontX, sFontName);
	}
}
Example #23
0
bool FindImageSubsystem(const wchar_t *Module, /*wchar_t* pstrDest,*/ DWORD& ImageSubsystem, DWORD& ImageBits, DWORD& FileAttrs)
{
    if (!Module || !*Module)
        return false;

    bool Result = false;
    //ImageSubsystem = IMAGE_SUBSYSTEM_UNKNOWN;

    // Исключения нас не интересуют - команда уже сформирована и отдана в CreateProcess!
    //// нулевой проход - смотрим исключения
    //// Берем "исключения" из реестра, которые должны исполняться директом,
    //// например, некоторые внутренние команды ком. процессора.
    //string strExcludeCmds;
    //GetRegKey(strSystemExecutor,L"ExcludeCmds",strExcludeCmds,L"");
    //UserDefinedList ExcludeCmdsList;
    //ExcludeCmdsList.Set(strExcludeCmds);
    //while (!ExcludeCmdsList.IsEmpty())
    //{
    //	if (!StrCmpI(Module,ExcludeCmdsList.GetNext()))
    //	{
    //		ImageSubsystem=IMAGE_SUBSYSTEM_WINDOWS_CUI;
    //		Result=true;
    //		break;
    //	}
    //}

    //string strFullName=Module;
    LPCWSTR ModuleExt = PointToExt(Module);
    wchar_t *strPathExt/*[32767]*/ = NULL; //(L".COM;.EXE;.BAT;.CMD;.VBS;.JS;.WSH");
    wchar_t *strPathEnv/*[32767]*/ = NULL;
    wchar_t *strExpand/*[32767]*/ = NULL;
    wchar_t *strTmpName/*[32767]*/ = NULL;
    wchar_t *pszFilePart = NULL;
    DWORD nPathExtLen = 0;
    LPCWSTR pszPathExtEnd = NULL;
    LPWSTR Ext = NULL;

    typedef LONG (WINAPI *RegOpenKeyExW_t)(HKEY hKey, LPCTSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult);
    RegOpenKeyExW_t _RegOpenKeyEx = NULL;
    typedef LONG (WINAPI *RegQueryValueExW_t)(HKEY hKey, LPCTSTR lpValueName, LPDWORD lpReserved, LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData);
    RegQueryValueExW_t _RegQueryValueEx = NULL;
    typedef LONG (WINAPI *RegCloseKey_t)(HKEY hKey);
    RegCloseKey_t _RegCloseKey = NULL;
    HMODULE hAdvApi = NULL;



    int cchstrPathExt = 32767;
    strPathExt = (wchar_t*)malloc(cchstrPathExt*sizeof(wchar_t));
    *strPathExt = 0;
    int cchstrPathEnv = 32767;
    strPathEnv = (wchar_t*)malloc(cchstrPathEnv*sizeof(wchar_t));
    *strPathEnv = 0;
    int cchstrExpand = 32767;
    strExpand = (wchar_t*)malloc(cchstrExpand*sizeof(wchar_t));
    *strExpand = 0;
    int cchstrTmpName = 32767;
    strTmpName = (wchar_t*)malloc(cchstrTmpName*sizeof(wchar_t));
    *strTmpName = 0;

    nPathExtLen = GetEnvironmentVariable(L"PATHEXT", strPathExt, cchstrPathExt-2);
    if (!nPathExtLen)
    {
        _wcscpy_c(strPathExt, cchstrPathExt, L".COM;.EXE;.BAT;.CMD;.VBS;.JS;.WSH");
        nPathExtLen = lstrlen(strPathExt);
    }
    pszPathExtEnd = strPathExt+nPathExtLen;
    // Разбить на токены
    strPathExt[nPathExtLen] = strPathExt[nPathExtLen+1] = 0;
    Ext = wcschr(strPathExt, L';');
    while (Ext)
    {
        *Ext = 0;
        Ext = wcschr(Ext+1, L';');
    }

    TODO("Проверить на превышение длин строк");

    // первый проход - в текущем каталоге
    LPWSTR pszExtCur = strPathExt;
    while (pszExtCur < pszPathExtEnd)
    {
        Ext = pszExtCur;
        pszExtCur = pszExtCur + lstrlen(pszExtCur)+1;

        _wcscpyn_c(strTmpName, cchstrTmpName, Module, cchstrTmpName); //-V501

        if (!ModuleExt)
        {
            if (!*Ext)
                continue;
            _wcscatn_c(strTmpName, cchstrTmpName, Ext, cchstrTmpName);
        }

        if (GetImageSubsystem(strTmpName, ImageSubsystem, ImageBits/*16/32/64*/, FileAttrs))
        {
            Result = true;
            goto wrap;
        }

        if (ModuleExt)
        {
            break;
        }
    }

    // второй проход - по правилам SearchPath

    // поиск по переменной PATH
    if (GetEnvironmentVariable(L"PATH", strPathEnv, cchstrPathEnv))
    {
        LPWSTR pszPathEnvEnd = strPathEnv + lstrlen(strPathEnv);

        LPWSTR pszPathCur = strPathEnv;
        while (pszPathCur && (pszPathCur < pszPathEnvEnd))
        {
            LPWSTR Path = pszPathCur;
            LPWSTR pszPathNext = wcschr(pszPathCur, L';');
            if (pszPathNext)
            {
                *pszPathNext = 0;
                pszPathCur = pszPathNext+1;
            }
            else
            {
                pszPathCur = pszPathEnvEnd;
            }
            if (!*Path)
                continue;

            pszExtCur = strPathExt;
            while (pszExtCur < pszPathExtEnd)
            {
                Ext = pszExtCur;
                pszExtCur = pszExtCur + lstrlen(pszExtCur)+1;
                if (!*Ext)
                    continue;

                if (SearchPath(Path, Module, Ext, cchstrTmpName, strTmpName, &pszFilePart))
                {
                    if (GetImageSubsystem(strTmpName, ImageSubsystem, ImageBits, FileAttrs))
                    {
                        Result = true;
                        goto wrap;
                    }
                }
            }
        }
    }

    pszExtCur = strPathExt;
    while (pszExtCur < pszPathExtEnd)
    {
        Ext = pszExtCur;
        pszExtCur = pszExtCur + lstrlen(pszExtCur)+1;
        if (!*Ext)
            continue;

        if (SearchPath(NULL, Module, Ext, cchstrTmpName, strTmpName, &pszFilePart))
        {
            if (GetImageSubsystem(strTmpName, ImageSubsystem, ImageBits, FileAttrs))
            {
                Result = true;
                goto wrap;
            }
        }
    }

    // третий проход - лезем в реестр в "App Paths"
    if (!wcschr(Module, L'\\'))
    {
        hAdvApi = LoadLibrary(L"AdvApi32.dll");
        if (!hAdvApi)
            goto wrap;
        _RegOpenKeyEx = (RegOpenKeyExW_t)GetProcAddress(hAdvApi, "RegOpenKeyExW");
        _RegQueryValueEx = (RegQueryValueExW_t)GetProcAddress(hAdvApi, "RegQueryValueExW");
        _RegCloseKey = (RegCloseKey_t)GetProcAddress(hAdvApi, "RegCloseKey");
        if (!_RegOpenKeyEx || !_RegQueryValueEx || !_RegCloseKey)
            goto wrap;

        LPCWSTR RegPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\";
        // В строке Module заменить исполняемый модуль на полный путь, который
        // берется из SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
        // Сначала смотрим в HKCU, затем - в HKLM
        HKEY RootFindKey[] = {HKEY_CURRENT_USER,HKEY_LOCAL_MACHINE,HKEY_LOCAL_MACHINE};

        BOOL lbAddExt = FALSE;
        pszExtCur = strPathExt;
        while (pszExtCur < pszPathExtEnd)
        {
            if (!lbAddExt)
            {
                Ext = NULL;
                lbAddExt = TRUE;
            }
            else
            {
                Ext = pszExtCur;
                pszExtCur = pszExtCur + lstrlen(pszExtCur)+1;
                if (!*Ext)
                    continue;
            }

            _wcscpy_c(strTmpName, cchstrTmpName, RegPath);
            _wcscatn_c(strTmpName, cchstrTmpName, Module, cchstrTmpName);
            if (Ext)
                _wcscatn_c(strTmpName, cchstrTmpName, Ext, cchstrTmpName);

            DWORD samDesired = KEY_QUERY_VALUE;
            DWORD RedirectionFlag = 0;
            // App Paths key is shared in Windows 7 and above
            static int sIsWindows7 = 0;
            if (sIsWindows7 == 0)
            {
                _ASSERTE(_WIN32_WINNT_WIN7==0x601);
                OSVERSIONINFOEXW osvi = {sizeof(osvi), HIBYTE(_WIN32_WINNT_WIN7), LOBYTE(_WIN32_WINNT_WIN7)};
                DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL);
                sIsWindows7 = VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask) ? 1 : -1;
            }
            if (sIsWindows7 != 1)
            {
#ifdef _WIN64
                RedirectionFlag = KEY_WOW64_32KEY;
#else
                RedirectionFlag = IsWindows64() ? KEY_WOW64_64KEY : 0;
#endif
            }
            for (size_t i = 0; i < countof(RootFindKey); i++)
            {
                if (i == (countof(RootFindKey)-1))
                {
                    if (RedirectionFlag)
                        samDesired |= RedirectionFlag;
                    else
                        break;
                }
                HKEY hKey;
                if (_RegOpenKeyEx(RootFindKey[i], strTmpName, 0, samDesired, &hKey) == ERROR_SUCCESS)
                {
                    DWORD nType = 0, nSize = sizeof(strTmpName)-2;
                    int RegResult = _RegQueryValueEx(hKey, L"", NULL, &nType, (LPBYTE)strTmpName, &nSize);
                    _RegCloseKey(hKey);

                    if ((RegResult == ERROR_SUCCESS) && (nType == REG_SZ || nType == REG_EXPAND_SZ || nType == REG_MULTI_SZ))
                    {
                        strTmpName[(nSize >> 1)+1] = 0;
                        if (!ExpandEnvironmentStrings(strTmpName, strExpand, cchstrExpand))
                            _wcscpy_c(strExpand, cchstrExpand, strTmpName);
                        if (GetImageSubsystem(Unquote(strExpand), ImageSubsystem, ImageBits, FileAttrs))
                        {
                            Result = true;
                            goto wrap;
                        }
                    }
                }
            }
Example #24
0
void SetConsoleFontSizeTo(HWND inConWnd, int inSizeY, int inSizeX, const wchar_t *asFontName, WORD anTextColors /*= 0*/, WORD anPopupColors /*= 0*/)
{
	_ASSERTE(_WIN32_WINNT_VISTA==0x600);
	OSVERSIONINFOEXW osvi = {sizeof(osvi), HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA)};
	DWORDLONG const dwlConditionMask = VerSetConditionMask(VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL), VER_MINORVERSION, VER_GREATER_EQUAL);
	if (VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION, dwlConditionMask))
	{
		// We have Vista
		apiSetConsoleFontSize(GetStdHandle(STD_OUTPUT_HANDLE), inSizeY, inSizeX, asFontName);
	}
	else 
	{
		// We have older NT (Win2k, WinXP)
		// So... using undocumented hack

		const COLORREF DefaultColors[16] =
		{
			0x00000000, 0x00800000, 0x00008000, 0x00808000,
			0x00000080, 0x00800080, 0x00008080, 0x00c0c0c0,
			0x00808080,	0x00ff0000, 0x0000ff00, 0x00ffff00,
			0x000000ff, 0x00ff00ff,	0x0000ffff, 0x00ffffff
		};

		if (!gpConsoleInfoStr)
		{
			gpConsoleInfoStr = (CONSOLE_INFO*)LocalAlloc(LPTR, sizeof(CONSOLE_INFO));

			if (!gpConsoleInfoStr)
			{
				_ASSERTE(gpConsoleInfoStr!=NULL);
				return; // memory allocation failed
			}

			gpConsoleInfoStr->Length = sizeof(CONSOLE_INFO);
		}

		if (!anTextColors)
		{
			CONSOLE_SCREEN_BUFFER_INFO csbi = {};
			GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
			anTextColors = csbi.wAttributes ? csbi.wAttributes : MAKEWORD(0x7, 0x0);
		}
		if (!anPopupColors)
		{
			anPopupColors = MAKEWORD(0x5, 0xf);
		}

		// get current size/position settings rather than using defaults..
		GetConsoleSizeInfo(gpConsoleInfoStr);
		// set these to zero to keep current settings
		gpConsoleInfoStr->FontSize.X				= inSizeX;
		gpConsoleInfoStr->FontSize.Y				= inSizeY;
		gpConsoleInfoStr->FontFamily				= 0;//0x30;//FF_MODERN|FIXED_PITCH;//0x30;
		gpConsoleInfoStr->FontWeight				= 0;//0x400;
		lstrcpynW(gpConsoleInfoStr->FaceName, asFontName ? asFontName : L"Lucida Console", countof(gpConsoleInfoStr->FaceName)); //-V303
		gpConsoleInfoStr->CursorSize				= 25;
		gpConsoleInfoStr->FullScreen				= FALSE;
		gpConsoleInfoStr->QuickEdit					= FALSE;
		//gpConsoleInfoStr->AutoPosition			= 0x10000;
		gpConsoleInfoStr->AutoPosition				= FALSE;
		RECT rcCon; GetWindowRect(inConWnd, &rcCon);
		gpConsoleInfoStr->WindowPosX = rcCon.left;
		gpConsoleInfoStr->WindowPosY = rcCon.top;
		gpConsoleInfoStr->InsertMode				= TRUE;
		gpConsoleInfoStr->ScreenColors				= anTextColors; //MAKEWORD(0x7, 0x0);
		gpConsoleInfoStr->PopupColors				= anPopupColors; //MAKEWORD(0x5, 0xf);
		gpConsoleInfoStr->HistoryNoDup				= FALSE;
		gpConsoleInfoStr->HistoryBufferSize			= 50;
		gpConsoleInfoStr->NumberOfHistoryBuffers	= 32; //-V112

		// Issue 700: Default history buffers count too small.
		HKEY hkConsole = NULL;
		LONG lRegRc;
		if (0 == (lRegRc = RegCreateKeyEx(HKEY_CURRENT_USER, L"Console\\ConEmu", 0, NULL, 0, KEY_READ, NULL, &hkConsole, NULL)))
		{
			DWORD nSize = sizeof(DWORD), nValue, nType;
			struct {
				LPCWSTR pszName;
				DWORD nMin, nMax;
				ULONG *pnVal;
			} BufferValues[] = {
				{L"HistoryBufferSize", 16, 999, &gpConsoleInfoStr->HistoryBufferSize},
				{L"NumberOfHistoryBuffers", 16, 999, &gpConsoleInfoStr->NumberOfHistoryBuffers}
			};
			for (size_t i = 0; i < countof(BufferValues); ++i)
			{
				lRegRc = RegQueryValueEx(hkConsole, BufferValues[i].pszName, NULL, &nType, (LPBYTE)&nValue, &nSize);
				if ((lRegRc == 0) && (nType == REG_DWORD) && (nSize == sizeof(DWORD)))
				{
					if (nValue < BufferValues[i].nMin)
						nValue = BufferValues[i].nMin;
					else if (nValue > BufferValues[i].nMax)
						nValue = BufferValues[i].nMax;

					if (nValue != *BufferValues[i].pnVal)
						*BufferValues[i].pnVal = nValue;
				}
			}
		}

		// color table
		for(size_t i = 0; i < 16; i++)
			gpConsoleInfoStr->ColorTable[i] = DefaultColors[i];

		gpConsoleInfoStr->CodePage					= GetConsoleOutputCP();//0;//0x352;
		gpConsoleInfoStr->Hwnd						= inConWnd;
		gpConsoleInfoStr->ConsoleTitle[0] = 0;

		// Send data to console window
		SetConsoleInfo(inConWnd, gpConsoleInfoStr);
	}
}