📄 dotnettester.cpp
字号:
// dotNetTester: Test for the existance of a specific .Net Framework version and launch an application
// Written by Guy Vider, 25/4/2007, gvidermac@gmail.com
// Feel free to use the code whnever and wherever it helps you - just keep this comment in :)
#include "stdafx.h"
#include <afxcoll.h>
#define KEY _T("SOFTWARE\\Microsoft\\.NETFramework\\policy")
#define KEY_LEN 256
#define VERSION 3
// Check the subkeys of the registry key HKLM\SOFTWARE\Microsoft\.NETFramework\policy
// FW specific keys will appear as subkeys formatted like this: with a V<major>.<minor>
bool CheckRegistryKeyExistance(TCHAR *fwVersion)
{
HKEY hKey;
DWORD dwIndex = 0;
DWORD cbName = KEY_LEN;
TCHAR sz[KEY_LEN];
bool result = false;
try
{
if(ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, KEY, 0, KEY_READ, &hKey))
{
// Iterate through all subkeys
while ((ERROR_NO_MORE_ITEMS != ::RegEnumKeyEx(hKey, dwIndex, sz, &cbName, NULL, NULL, NULL, NULL)))
{
if(!_tcsnicmp(sz, _T("V"), 1))
_tcsncpy(fwVersion, sz+1, VERSION + 1);
dwIndex++;
cbName = KEY_LEN;
}
::RegCloseKey(hKey);
printf("Fraemwork version found: %s\n", fwVersion);
result = true;
}
else
result = false;
return result;
}
catch(...)
{
return false;
}
}
//Compare the existing and required FW version numbers
bool CompareFWVersions(TCHAR *existing, TCHAR *required)
{
int existingMajor = atoi(existing);
int existingMinor = atoi(existing+2);
int requiredMajor = atoi(required);
int requiredMinor = atoi(required+2);
bool result = false;
// very simple comparison algorithm, really
if(requiredMajor < existingMajor)
result = true;
else if(requiredMajor == existingMajor && requiredMinor <= existingMinor)
result = true;
else
result = false;
return result;
}
//Run an application the simplest, shortest way...
void RunApplication(char *path)
{
printf("Now starting %s...\n", path);
try
{
WinExec(path, SW_SHOW);
}
catch(...)
{
printf("Couldn't run %s\n", path);
exit(-4);
}
}
// Main function
int main(int argc, char* argv[])
{
TCHAR fwVersion[VERSION];
// Ensure correct number of parameters
if(argc < 2 || argc > 3)
{
printf("Usage: dotNetTester <minimum FW version (x.y)> [<path of app to run>]\n");
exit(-1);
}
// Check if Framework key exists and get the FW version if yes
if(CheckRegistryKeyExistance(fwVersion))
{
// Compare versions
if(CompareFWVersions(fwVersion, argv[1]))
{
// Launch an application, if the user requested it
if(3 == argc)
RunApplication(argv[2]);
}
else
{
// Minimum FW requirement not met
printf("Minimum required .Net Framework version is %s, you have version %s installed. Please install the proper .Net Framework\n", argv[1], fwVersion);
exit(-2);
}
}
else
{
// No FW exists
printf("Please install the latest .Net Framework - it can be downloaded from Microsoft\n", argv[1], fwVersion);
exit(-3);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -