#include "StdAfx.h" #include #include "Global.h" #include "my32.h" #pragma comment(lib,"my32.lib") #include // 关机函数; // Jeff.获取文件版本号函数头文件; #include #pragma comment(lib,"version.lib") using namespace std; // 注册OCX bool RegOcx(CString ocxfile) #include "afxcoll.h" #include "afxtempl.h" #include "atlconv.h" // 在函数GetOSDisplayString的前面需要定义如下宏: // 同时还要包含strsafe.h #include #pragma comment(lib, "User32.lib") #define BUFSIZE 256 #define PRODUCT_ULTIMATE 0x00000001 #define PRODUCT_HOME_BASIC 0x00000002 #define PRODUCT_HOME_PREMIUM 0x00000003 #define PRODUCT_ENTERPRISE 0x00000004 #define PRODUCT_HOME_BASIC_N 0x00000005 #define PRODUCT_BUSINESS 0x00000006 #define PRODUCT_STANDARD_SERVER 0x00000007 #define PRODUCT_DATACENTER_SERVER 0x00000008 #define PRODUCT_SMALLBUSINESS_SERVER 0x00000009 #define PRODUCT_ENTERPRISE_SERVER 0x0000000A #define PRODUCT_STARTER 0x0000000B #define PRODUCT_DATACENTER_SERVER_CORE 0x0000000C #define PRODUCT_STANDARD_SERVER_CORE 0x0000000D #define PRODUCT_ENTERPRISE_SERVER_CORE 0x0000000E #define PRODUCT_ENTERPRISE_SERVER_IA64 0x0000000F #define PRODUCT_BUSINESS_N 0x00000010 #define PRODUCT_WEB_SERVER 0x00000011 #define PRODUCT_CLUSTER_SERVER 0x00000012 #define PRODUCT_HOME_SERVER 0x00000013 #define PRODUCT_STORAGE_EXPRESS_SERVER 0x00000014 #define PRODUCT_STORAGE_STANDARD_SERVER 0x00000015 #define PRODUCT_STORAGE_WORKGROUP_SERVER 0x00000016 #define PRODUCT_STORAGE_ENTERPRISE_SERVER 0x00000017 #define PRODUCT_SERVER_FOR_SMALLBUSINESS 0x00000018 #define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM 0x00000019 #define PRODUCT_HOME_PREMIUM_N 0x0000001A #define PRODUCT_ENTERPRISE_N 0x0000001B #define PRODUCT_ULTIMATE_N 0x0000001C #define PRODUCT_WEB_SERVER_CORE 0x0000001D #define PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT 0x0000001E #define PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY 0x0000001F #define PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING 0x00000020 #define PRODUCT_SMALLBUSINESS_SERVER_PRIME 0x00000021 #define PRODUCT_HOME_PREMIUM_SERVER 0x00000022 #define PRODUCT_SERVER_FOR_SMALLBUSINESS_V 0x00000023 #define PRODUCT_STANDARD_SERVER_V 0x00000024 #define PRODUCT_DATACENTER_SERVER_V 0x00000025 #define PRODUCT_ENTERPRISE_SERVER_V 0x00000026 #define PRODUCT_DATACENTER_SERVER_CORE_V 0x00000027 #define PRODUCT_STANDARD_SERVER_CORE_V 0x00000028 #define PRODUCT_ENTERPRISE_SERVER_CORE_V 0x00000029 #define PRODUCT_HYPERV 0x0000002A #define SM_TABLETPC 86 #define SM_MEDIACENTER 87 #define SM_STARTER 88 #define SM_SERVERR2 89 #define VER_SERVER_NT 0x80000000 #define VER_WORKSTATION_NT 0x40000000 #define VER_SUITE_SMALLBUSINESS 0x00000001 #define VER_SUITE_ENTERPRISE 0x00000002 #define VER_SUITE_BACKOFFICE 0x00000004 #define VER_SUITE_COMMUNICATIONS 0x00000008 #define VER_SUITE_TERMINAL 0x00000010 #define VER_SUITE_SMALLBUSINESS_RESTRICTED 0x00000020 #define VER_SUITE_EMBEDDEDNT 0x00000040 #define VER_SUITE_DATACENTER 0x00000080 #define VER_SUITE_SINGLEUSERTS 0x00000100 #define VER_SUITE_PERSONAL 0x00000200 #define VER_SUITE_BLADE 0x00000400 #define VER_SUITE_EMBEDDED_RESTRICTED 0x00000800 #define VER_SUITE_SECURITY_APPLIANCE 0x00001000 #define VER_SUITE_STORAGE_SERVER 0x00002000 #define VER_SUITE_COMPUTE_SERVER 0x00004000 #define VER_SUITE_WH_SERVER 0x00008000 typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO); typedef BOOL(WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD); //-------------------------------------------------------------- CDatabase g_db; CDatabase g_masterdb; CDatabase *g_pCurdb = NULL; CArray g_nYearArray; CArray g_dbArray; TCHAR g_ModulePath[MAX_PATH] = _T(""); // 软件目录; TCHAR g_ModuleFileName[MAX_PATH] = _T(""); // 软件名称; TCHAR g_szConnectString[MAX_PATH] = _T(""); // DB连接串; // 服务器信息; TCHAR g_szDBSource[MAX_PATH] = _T(""); // 数据库源(服务所在IP或计算机名); TCHAR g_szDBSourcePort[MAX_PATH] = _T(""); // 数据库源端口; DWORD g_dwDBServerPort = 0; // 数据库源端口; TCHAR g_szDBAccount[MAX_PATH] = _T(""); // 数据库登录用户; TCHAR g_szDBPassWord[MAX_PATH] = _T(""); // 数据库登录密码; TCHAR g_szDBName[MAX_PATH] = _T(""); // 数据库名称; // 升级包版本号; DWORD g_arrFileVersion[4] = { 0 }; CHAR g_szKeyPath[MAX_PATH] = {0}; int g_nVersion = -1; // start------------6版本加密狗信息----------------; BYTE g_byVersion[7][26] = { { 24, 0, 144, 87, 75, 28, 197, 248, 180, 6, 164, 231, 242, 14, 91, 130, 174, 139, 20, 8, 208, 11, 251, 239, 29, 161 }, // 儿童座席版; { 24, 0, 236, 49, 197, 138, 4, 38, 99, 92, 108, 73, 224, 45, 225, 72, 241, 52, 240, 43, 140, 171, 13, 239, 173, 79 }, // 婚纱座席版; { 24, 0, 15, 94, 36, 102, 72, 216, 4, 18, 103, 117, 240, 15, 7, 158, 250, 133, 112, 95, 163, 173, 44, 84, 41, 86 }, // 儿童企业版; { 24, 0, 222, 41, 26, 123, 125, 46, 65, 0, 144, 51, 210, 220, 57, 10, 86, 26, 12, 8, 180, 39, 138, 95, 89, 124 }, // 婚纱企业版; { 24, 0, 147, 58, 13, 35, 27, 196, 134, 222, 187, 98, 80, 70, 192, 21, 9, 39, 194, 87, 67, 94, 37, 81, 200, 217 }, // 儿童全能版; { 24, 0, 88, 15, 76, 236, 83, 224, 208, 31, 26, 110, 243, 71, 154, 140, 238, 242, 34, 143, 41, 212, 239, 50, 233, 81 }, // 婚纱全能版; }; BYTE g_byShotVersion[6][10] = { { 6, 0, 148, 11, 205, 50, 4, 171, 32, 39 }, // 儿童座席版; { 6, 0, 4, 29, 24, 247, 189, 95, 83, 76 }, // 婚纱座席版; { 6, 0, 4, 187, 103, 10, 59, 169, 240, 220 }, // 儿童企业版; { 6, 0, 137, 160, 230, 173, 202, 2, 117, 142 }, // 婚纱企业版; { 6, 0, 194, 199, 210, 67, 185, 1, 138, 195 }, // 儿童全能版; { 6, 0, 188, 145, 100, 47, 154, 177, 34, 205 }, // 婚纱全能版; }; // endof------------6版本加密狗信息----------------; // start---------------加密表达式加密后的数组----------------------; BYTE g_byCalExVersion[7][114] = { // 儿童座席版; { 109, 0, 90, 15, 108, 138, 37, 190, 82, 8, 20, 252, 32, 162, 7, 8, 254, 255, 56, 245, 194, 4, 46, 120, 49, 120, 156, 27, 17, 164, 217, 79, 167, 146, 197, 69, 55, 117, 197, 149, 42, 217, 151, 195, 254, 145, 187, 197, 232, 188, 114, 223, 40, 229, 151, 245, 116, 177, 141, 94, 134, 14, 13, 169, 165, 94, 5, 216, 235, 60, 176, 38, 174, 121, 187, 174, 16, 30, 17, 20, 246, 34, 20, 252, 32, 162, 7, 8, 254, 255, 56, 245, 194, 4, 46, 120, 49, 120, 156, 27, 17, 164, 217, 79, 167, 146, 143, 132, 219, 173, 211, 2, 99, 180 }, // 婚纱座席版; { 109, 0, 128, 13, 164, 143, 252, 87, 30, 48, 20, 21, 145, 114, 160, 96, 49, 127, 181, 32, 144, 213, 109, 99, 142, 219, 71, 26, 22, 220, 177, 179, 191, 82, 138, 45, 173, 160, 9, 83, 162, 106, 182, 175, 47, 92, 214, 78, 93, 21, 202, 165, 167, 200, 198, 172, 218, 74, 236, 194, 194, 228, 40, 27, 95, 60, 160, 173, 66, 185, 178, 7, 159, 220, 58, 71, 140, 19, 20, 37, 66, 84, 20, 21, 145, 114, 160, 96, 49, 127, 181, 32, 144, 213, 109, 99, 142, 219, 71, 26, 22, 220, 177, 179, 191, 82, 232, 182, 45, 27, 13, 195, 225, 204 }, // 儿童企业版; { 109, 0, 229, 36, 223, 222, 213, 46, 70, 55, 240, 207, 237, 75, 226, 68, 133, 100, 42, 119, 89, 131, 79, 104, 158, 17, 172, 239, 4, 158, 201, 51, 62, 51, 94, 232, 14, 124, 81, 64, 100, 141, 187, 85, 156, 59, 205, 116, 200, 24, 108, 218, 145, 22, 7, 36, 45, 7, 156, 255, 170, 130, 165, 140, 99, 216, 120, 72, 171, 196, 27, 49, 138, 199, 137, 246, 214, 247, 121, 114, 215, 199, 240, 207, 237, 75, 226, 68, 133, 100, 42, 119, 89, 131, 79, 104, 158, 17, 172, 239, 4, 158, 201, 51, 62, 51, 248, 214, 27, 254, 221, 208, 181, 209 }, // 婚纱企业版; { 109, 0, 94, 220, 40, 182, 30, 120, 56, 193, 78, 101, 1, 144, 68, 206, 192, 52, 67, 9, 243, 20, 9, 195, 153, 15, 22, 217, 243, 79, 177, 22, 24, 171, 185, 157, 172, 67, 117, 124, 153, 165, 3, 41, 218, 47, 149, 150, 48, 152, 75, 250, 204, 253, 92, 53, 121, 147, 242, 140, 80, 66, 34, 194, 16, 109, 237, 17, 222, 161, 96, 8, 11, 194, 58, 45, 86, 15, 207, 121, 247, 104, 78, 101, 1, 144, 68, 206, 192, 52, 67, 9, 243, 20, 9, 195, 153, 15, 22, 217, 243, 79, 177, 22, 24, 171, 122, 243, 190, 146, 77, 222, 208, 129 }, // 儿童全能版; { 109, 0, 26, 222, 0, 239, 199, 199, 188, 225, 180, 168, 178, 208, 21, 79, 62, 90, 51, 31, 88, 128, 180, 105, 53, 137, 230, 254, 244, 50, 195, 49, 167, 143, 168, 125, 130, 125, 8, 73, 224, 120, 80, 95, 178, 49, 44, 158, 38, 38, 247, 169, 62, 187, 119, 192, 77, 194, 118, 246, 133, 241, 157, 62, 144, 24, 94, 173, 49, 192, 46, 250, 249, 239, 82, 78, 135, 3, 86, 154, 8, 135, 180, 168, 178, 208, 21, 79, 62, 90, 51, 31, 88, 128, 180, 105, 53, 137, 230, 254, 244, 50, 195, 49, 167, 143, 241, 214, 38, 180, 198, 3, 158, 165 }, // 婚纱全能版; { 109, 0, 139, 145, 163, 176, 66, 18, 207, 79, 172, 200, 12, 124, 89, 151, 248, 238, 26, 85, 137, 146, 119, 58, 76, 194, 151, 239, 204, 104, 18, 166, 208, 54, 37, 85, 3, 174, 28, 74, 154, 156, 120, 194, 8, 112, 79, 42, 27, 89, 81, 95, 238, 74, 59, 199, 160, 101, 117, 179, 51, 138, 70, 176, 239, 182, 160, 212, 11, 33, 224, 74, 155, 143, 216, 10, 220, 71, 8, 168, 18, 175, 172, 200, 12, 124, 89, 151, 248, 238, 26, 85, 137, 146, 119, 58, 76, 194, 151, 239, 204, 104, 18, 166, 208, 54, 114, 118, 181, 24, 252, 41, 246, 88 }, }; // endof---------------加密表达式加密后的数组----------------------; //************************************// // 数据库地址; // 数据库端口; // 数据库管理员账号; // 数据库管理员密码; // 数据库名称; // // [函数]:GetIniInfo // [描述]:获取指定ini信息; // [参数]: // szPath:ini所在目录; // szIniName:ini名; // [返回]:void // //************************************// int GetIniInfo(const char *szPath, const char *szIniName) { TCHAR szDrive[_MAX_DRIVE] = { 0 }; TCHAR szDir[_MAX_DIR] = { 0 }; TCHAR szFna[_MAX_DIR] = { 0 }; TCHAR szExt[_MAX_DIR] = { 0 }; ::GetModuleFileName(NULL, g_ModulePath, sizeof(g_ModulePath) / sizeof(TCHAR)); _stprintf_s(g_ModuleFileName, _T("%s"), g_ModulePath); _tsplitpath_s(g_ModulePath, szDrive, szDir, szFna, szExt); _tcscpy_s(g_ModulePath, szDrive); _tcscat_s(g_ModulePath, szDir); // -----------------------------------------------------// TCHAR szFile[MAX_PATH + 1] = _T(""); if (szPath != NULL && szIniName != NULL) _stprintf_s(szFile, _T("%s%s"), szPath, szIniName); else _stprintf_s(szFile, _T("%sServiceInfo.ini"), g_ModulePath); HANDLE hFile = CreateFile(szFile, 0/*GENERIC_READ*/, 0, NULL, OPEN_EXISTING, 0, NULL); if (ERROR_FILE_NOT_FOUND == GetLastError()) { return -1; } TRACE("szFile =%s\n", szFile); // 获取服务器端信息; GetPrivateProfileString(_T("DatabaseInfo"), _T("dbSource"), _T(""), g_szDBSource, MAX_PATH, szFile); g_dwDBServerPort = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbServerPort"), 0, szFile); GetPrivateProfileString(_T("DatabaseInfo"), _T("dbAccount"), _T(""), g_szDBAccount, MAX_PATH, szFile); GetPrivateProfileString(_T("DatabaseInfo"), _T("dbPassWord"), _T(""), g_szDBPassWord, MAX_PATH, szFile); GetPrivateProfileString(_T("DatabaseInfo"), _T("dbName"), _T(""), g_szDBName, MAX_PATH, szFile); if (g_dwDBServerPort != 0) _stprintf_s(g_szConnectString, _T("driver={SQL Server};Server=%s,%d;database=%s;uid=%s;pwd=%s"), g_szDBSource, g_dwDBServerPort, g_szDBName, g_szDBAccount, g_szDBPassWord); else _stprintf_s(g_szConnectString, _T("driver={SQL Server};Server=%s;database=%s;uid=%s;pwd=%s"), g_szDBSource, g_szDBName, g_szDBAccount, g_szDBPassWord); return 0; } BOOL FindAppProcessID(CString path, DWORD &dwProcessID) { int nIndex = path.ReverseFind('\\'); path = path.Right(path.GetLength() - nIndex - 1); dwProcessID = FindProcess(path); return (dwProcessID != 0 ? TRUE : FALSE); BOOL bFound = FALSE; HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0); PROCESSENTRY32 Info; Info.dwSize = sizeof(PROCESSENTRY32); path.MakeLower(); if (::Process32First(handle, &Info)) { do{ CString ss = Info.szExeFile; ss.MakeLower(); if (ss == path) { ::CloseHandle(handle); dwProcessID = Info.th32ProcessID; bFound = TRUE; break; } } while (::Process32Next(handle, &Info)); if (handle) ::CloseHandle(handle); } return (bFound ? TRUE : FALSE); } DWORD FindProcess(__in CString strProName) { DWORD dwProcessID = 0; PROCESSENTRY32 pe32 = { 0 }; HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hProcessSnap == NULL) { return 0; } pe32.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hProcessSnap, &pe32)) { do { if (_tcscmp(strProName.GetBuffer(), pe32.szExeFile) == 0) { dwProcessID = pe32.th32ProcessID; break; } } while (Process32Next(hProcessSnap, &pe32)); } CloseHandle(hProcessSnap); return dwProcessID; } int FindAndCloseProcess(__in CString strProName) { int nIndex = strProName.ReverseFind('\\'); if (nIndex != -1) // 如果传的是全路径; strProName = strProName.Right(strProName.GetLength() - nIndex - 1); DWORD dwProcessID = 0; PROCESSENTRY32 pe32 = { 0 }; HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hProcessSnap == NULL) { return 0; } pe32.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hProcessSnap, &pe32)) { do { if (_tcscmp(strProName.GetBuffer(), pe32.szExeFile) == 0) { dwProcessID = pe32.th32ProcessID; break; } } while (Process32Next(hProcessSnap, &pe32)); } CloseHandle(hProcessSnap); if (dwProcessID == 0) return 0; HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwProcessID); if (hProcess == NULL) return 0; DWORD dwError; if (!TerminateProcess(hProcess, 0)) { dwError = GetLastError(); CloseHandle(hProcess); return -1; } // 等待进程结束响应; if (WAIT_OBJECT_0 != WaitForSingleObject(hProcess, INFINITE)) { CloseHandle(hProcess); AfxMessageBox(_T("结束进程失败")); return -1; } CloseHandle(hProcess); return 1; } CDatabase *GetSQLMaster() { TCHAR szDBSource[MAX_PATH] = _T(""); if (g_dwDBServerPort != 0) _stprintf_s(szDBSource, _T("%s,%d"), g_szDBSource, g_dwDBServerPort); else _stprintf_s(szDBSource, _T("%s"), g_szDBSource); CDatabase *pMasterdb = new CDatabase; TCHAR szConnectString[MAX_PATH] = _T(""); try { _stprintf_s(szConnectString, _T("driver={SQL Server};Server=%s;database=master;uid=%s;pwd=%s"), szDBSource, g_szDBAccount, g_szDBPassWord); pMasterdb->OpenEx(szConnectString, CDatabase::noOdbcDialog); pMasterdb->SetQueryTimeout(120); } catch (CDBException * e) { AfxMessageBox(e->m_strError); AfxMessageBox(_T("连接SQL数据库失败!")); _tprintf(_T("%s!\n"), e->m_strError); _tprintf(_T("连接SQL数据库失败!\n")); if (pMasterdb) { delete pMasterdb; pMasterdb = NULL; } e->Delete(); return NULL; } return pMasterdb; } int MyFindPort_2(int start, BYTE *InByte, int InLen, char *OutPath) { int n; int ret; int D[8]; double F[8]; char s0[50] = "", s1[50] = "", s2[50] = "", s3[50] = "", s4[50] = "", s5[50] = "", s6[50] = "", s7[50] = ""; CHAR KeyPath[MAX_PATH] = ""; //查找系统上所有的锁; for (n = 0; n < 256; n++) { ZeroMemory(KeyPath, sizeof(KeyPath)); ret = FindPort(n, KeyPath); if (ret != 0 && n == 0)return -1053;//表示系统上没有任何智能锁; if (ret != 0)return ret; #if 1 memcpy(g_szKeyPath,KeyPath,MAX_PATH); #endif //使用该设备路径锁进行运算; ret = CalEx(InByte, InLen, &D[0], &D[1], &D[2], &D[3], &D[4], &D[5], &D[6], &D[7], &F[0], &F[1], &F[2], &F[3], &F[4], &F[5], &F[6], &F[7], s0, s1, s2, s3, s4, s5, s6, s7, KeyPath, 20000); //如果正确,则返回该设备路径供以后使用; if (ret == -63)return ret; if ((ret == 0) && (D[0] == 123))return 0; } return -53; } int GetSoftWareVersion() { int nRet = 0; CHAR szUSBKeyPath[MAX_PATH] = ""; for (int i(0); i < 6; i++) { nRet = MyFindPort_2(0, g_byShotVersion[i], 10, szUSBKeyPath); if (nRet == 0) { g_nVersion = i; break; } else { TCHAR szErrInfo[MAX_PATH] = _T(""); GetUSBKey32ErrInfo(nRet,szErrInfo,MAX_PATH); } } return g_nVersion; } //------------------------------------------------------------------------------ // Remark by Jeff 2014.11.05 // 函数:GetUSBKey32ErrInfo // 描述:返回加密狗错误; // 参数: // lError:域天加密狗函数返回的错误码; // ErrorInfo:错误码描述缓冲区; // nErrorLen:错误码描述缓冲区大小; // 返回:无 // //------------------------------------------------------------------------------ void GetUSBKey32ErrInfo(IN const long lError, OUT TCHAR *ErrInfo, IN const INT_PTR &nErrorLen) { switch (lError) { case -1: StringCchCat(ErrInfo,nErrorLen, _T("未找到返回结果变量")); break; case -2: StringCchCat(ErrInfo,nErrorLen, _T("未找到 = 符号")); break; case -3: StringCchCat(ErrInfo,nErrorLen, _T("代表没有找到相应常数")); break; case -5: StringCchCat(ErrInfo,nErrorLen, _T("代表找不到字符串的第一个双引号")); break; case -6: StringCchCat(ErrInfo,nErrorLen, _T("代表找不到字符串的第二个双引号")); break; case -7: StringCchCat(ErrInfo,nErrorLen, _T("IF语句没有找到goto字符")); break; case -8: StringCchCat(ErrInfo,nErrorLen, _T("IF语句没有找到第一个比较字符")); break; case -9: StringCchCat(ErrInfo,nErrorLen, _T("IF语句没有找到比较符号")); break; case -10: StringCchCat(ErrInfo,nErrorLen, _T("两边变量类型不相符")); break; case -11: StringCchCat(ErrInfo,nErrorLen, _T("没有找到NOT符号")); break; case -12: StringCchCat(ErrInfo,nErrorLen, _T("不是整形变量")); break; case -13: StringCchCat(ErrInfo,nErrorLen, _T("代表没有找到相应整形常数")); break; case -14: StringCchCat(ErrInfo,nErrorLen, _T("代表没有找到相应字符串常数")); break; case -15: StringCchCat(ErrInfo,nErrorLen, _T("代表没有找到相应浮点常数")); break; case -16: StringCchCat(ErrInfo,nErrorLen, _T("代表不支持这个运算")); break; case -17: StringCchCat(ErrInfo,nErrorLen, _T("代表没有左边括号")); break; case -18: StringCchCat(ErrInfo,nErrorLen, _T("代表没有变量")); break; case -19: StringCchCat(ErrInfo,nErrorLen, _T("代表没“,”")); break; case -20: StringCchCat(ErrInfo,nErrorLen, _T("代表没有右边括号")); break; case -21: StringCchCat(ErrInfo,nErrorLen, _T("代表常数超过指这定的范围")); break; case -22: StringCchCat(ErrInfo,nErrorLen, _T("代表储存器的地址超过指定的范围,整数不能超过EEPROM_LEN-4,浮点不能超过30720-8")); break; case -23: StringCchCat(ErrInfo,nErrorLen, _T("代表储存器的地址超过指定的范围,字符串不能超过EEPROM_LEN-LEN,其中LEN为字符串的长度")); break; case -24: StringCchCat(ErrInfo,nErrorLen, _T("除法中,被除数不能为0")); break; case -25: StringCchCat(ErrInfo,nErrorLen, _T("未知错误")); break; case -26: StringCchCat(ErrInfo,nErrorLen, _T("第二个变量不在指定的位置")); break; case -27: StringCchCat(ErrInfo,nErrorLen, _T("字符串常量超过指定的长度")); break; case -28: StringCchCat(ErrInfo,nErrorLen, _T("不是字符串变量")); break; case -29: StringCchCat(ErrInfo,nErrorLen, _T("没有第三个变量")); break; case -30: StringCchCat(ErrInfo,nErrorLen, _T("GOTO的标识语句不能全为数字")); break; case -31: StringCchCat(ErrInfo,nErrorLen, _T("不能打开ENC文件")); break; case -32: StringCchCat(ErrInfo,nErrorLen, _T("不能读ENC文件")); break; case -33: StringCchCat(ErrInfo,nErrorLen, _T("GOTO CALL不能找到指定的跳转位置")); break; case -34: StringCchCat(ErrInfo,nErrorLen, _T("智能卡运算中,未知数据类型")); break; case -35: StringCchCat(ErrInfo,nErrorLen, _T("智能卡运算中,未知代码类型")); break; case -36: StringCchCat(ErrInfo,nErrorLen, _T("字符串长度超出50")); break; case -37: StringCchCat(ErrInfo,nErrorLen, _T("操作时超长,负长")); break; case -38: StringCchCat(ErrInfo,nErrorLen, _T("标识重复")); break; case -39: StringCchCat(ErrInfo,nErrorLen, _T("程序堆栈溢出")); break; case -40: StringCchCat(ErrInfo,nErrorLen, _T("堆栈溢出")); break; case -41: StringCchCat(ErrInfo,nErrorLen, _T("不能建立编译文件,请查看文件是否有只读属性,或被其它文件打开")); break; case -42: StringCchCat(ErrInfo,nErrorLen, _T("不能写文件,请查看文件是否有只读属性,或被其它文件打开")); break; case -43: StringCchCat(ErrInfo,nErrorLen, _T("程序被中途使用END语句结束")); break; case -44: StringCchCat(ErrInfo,nErrorLen, _T("程序跳转到外部的空间")); break; case -45: StringCchCat(ErrInfo,nErrorLen, _T("传送数据失败")); break; case -46: StringCchCat(ErrInfo,nErrorLen, _T("程序超出运算次数,可能是死循环")); break; case -47: StringCchCat(ErrInfo,nErrorLen, _T("写密码不正确")); break; case -48: StringCchCat(ErrInfo,nErrorLen, _T("读密码不正确")); break; case -49: StringCchCat(ErrInfo,nErrorLen, _T("读写EEPROM时,地址溢出")); break; case -50: StringCchCat(ErrInfo,nErrorLen, _T("USB操作失败,可能是没有找到相关的指令")); break; case -51: StringCchCat(ErrInfo,nErrorLen, _T("打开USB文件句柄失败")); break; case -52: StringCchCat(ErrInfo,nErrorLen, _T("使用加密锁加密自定义表达式,生成加密代码时生产错误")); break; case -53: StringCchCat(ErrInfo,nErrorLen, _T("无法打开usb设备,可能驱动程序没有安装或没有插入加密锁.")); break; case -63: StringCchCat(ErrInfo,nErrorLen, _T("不能打开指定的文件")); break; case -64: StringCchCat(ErrInfo,nErrorLen, _T("不能建立指定的文件")); break; case -65: StringCchCat(ErrInfo,nErrorLen, _T(" 验证码错误,可能是输入解密密钥错误,或注册授权码错误")); break; case -66: StringCchCat(ErrInfo,nErrorLen, _T("执行TIMEOUT函数时,输入的ID与锁ID不相符")); break; case -67: StringCchCat(ErrInfo,nErrorLen, _T("执行TIMEOUT函数时,智能卡运行函数已到期")); break; case -68: StringCchCat(ErrInfo,nErrorLen, _T("操作浮点运算时,输入的参数将会导致返回值是一个无穷值")); break; case -69: StringCchCat(ErrInfo,nErrorLen, _T("代表没足够的变量参数")); break; case -70: StringCchCat(ErrInfo,nErrorLen, _T("返回变量与函数不相符")); break; case -71: StringCchCat(ErrInfo,nErrorLen, _T("浮点数转换字符串时,要转换的数据太大")); break; case -72: StringCchCat(ErrInfo,nErrorLen, _T("初始化服务器错误")); break; case -73: StringCchCat(ErrInfo,nErrorLen, _T("对缓冲区进行MD5运算时错误")); break; case -74: StringCchCat(ErrInfo,nErrorLen, _T("MD5验证IPVAR错误")); break; case -75: StringCchCat(ErrInfo,nErrorLen, _T("MD5验证IPCount错误")); break; case -76: StringCchCat(ErrInfo,nErrorLen, _T("没有找到对应的SOCKET连接")); break; case -77: StringCchCat(ErrInfo,nErrorLen, _T("没有找到要删除的对应的SOCKET连接")); break; case -78: StringCchCat(ErrInfo,nErrorLen, _T("没有找到要删除的对应的功能模块号连接")); break; case -79: StringCchCat(ErrInfo,nErrorLen, _T("没有找到要增加的对应的功能模块号连接")); break; case -80: StringCchCat(ErrInfo,nErrorLen, _T("用户数已超过限制的授权数量")); break; case -81: StringCchCat(ErrInfo,nErrorLen, _T("找不到对应的INI文件条目")); break; case -82: StringCchCat(ErrInfo,nErrorLen, _T("没有进行初始化服务工作")); break; case -252: StringCchCat(ErrInfo,nErrorLen, _T("密码不正确")); break; case -1088: StringCchCat(ErrInfo,nErrorLen, _T("发送数据错误")); break; case -1089: StringCchCat(ErrInfo,nErrorLen, _T("获取数据错误")); break; case -1092: StringCchCat(ErrInfo,nErrorLen, _T("找不到对应的服务端操作码")); break; case -1093: StringCchCat(ErrInfo,nErrorLen, _T("表示连接服务时错误")); break; case -1095: StringCchCat(ErrInfo,nErrorLen, _T("获取主机名称失败")); break; case -1097: StringCchCat(ErrInfo,nErrorLen, _T("建立套字接错误")); break; case -1098: StringCchCat(ErrInfo,nErrorLen, _T("绑定套字节端口错误")); break; case -1099: StringCchCat(ErrInfo,nErrorLen, _T("表示无效连接,不能进行相关的操作")); break; case -2002: StringCchCat(ErrInfo,nErrorLen, _T("表示监听时产生错误")); break; case -2003: StringCchCat(ErrInfo,nErrorLen, _T("表示发送的数据长度与接收的数据长度不相符")); break; case -2005: StringCchCat(ErrInfo,nErrorLen, _T("表示当前服务不存在任何连接")); break; case -2006: StringCchCat(ErrInfo,nErrorLen, _T("表示当前查询节点超出集合范范围")); break; case -2009: StringCchCat(ErrInfo,nErrorLen, _T("表示关闭连接错误")); break; case -1052: StringCchCat(ErrInfo,nErrorLen, _T("可能是输入的授权号不正确")); break; case -1053: StringCchCat(ErrInfo,nErrorLen, _T("系统上没有任何智能锁")); break; default: StringCchCat(ErrInfo,nErrorLen, _T("未知错误代码")); } } // Jeff.Hacker. WINDOWS NT 以上的内核需要提权,才能对系统进行高级管理; BOOL GetDebugPriv() { // 返回的访问令牌指针; HANDLE hToken; // 接收所返回的制定特权名称的信息; LUID sedebugnameValue; // 新特权信息的指针(结构体); TOKEN_PRIVILEGES tkp; DWORD dwCurProcId = GetCurrentProcessId(); // 要修改访问权限的进程句柄; HANDLE hCurProc; hCurProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwCurProcId); if (!::OpenProcessToken(hCurProc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) { ShowSystemErrorInfo(_T("升级包OpenProcessToken失败."),GetLastError()); return FALSE; } if (!::LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue)) { ShowSystemErrorInfo(_T("升级包LookupPrivilegeValue失败."),GetLastError()); CloseHandle(hToken); return FALSE; } tkp.PrivilegeCount = 1; tkp.Privileges[0].Luid = sedebugnameValue; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!::AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof tkp, NULL, NULL)) { ShowSystemErrorInfo(_T("升级包AdjustTokenPrivileges失败."),GetLastError()); CloseHandle(hToken); return FALSE; } CloseHandle(hCurProc); CloseHandle(hToken); return TRUE; } //该版本号是在资源文件中的../Version/VS_VERSION_INFO/FILEVERSION的值 CString GetFileVerionString() { TCHAR szPath[200] = { 0 }; TCHAR szVersionBuffer[200] = { 0 }; DWORD dwHandle; DWORD InfoSize; CString strVersion; ::GetModuleFileName(NULL, szPath, sizeof(szPath)); //首先获得版本信息资源的长度 InfoSize = GetFileVersionInfoSize(szPath, &dwHandle); //将版本信息资源读入缓冲区 if (InfoSize == 0) return _T("None VerSion Supprot"); char *InfoBuf = new char[InfoSize]; GetFileVersionInfo(szPath, 0, InfoSize, InfoBuf); //获得生成文件使用的代码页及文件版本 unsigned int cbTranslate = 0; struct LANGANDCODEPAGE { WORD wLanguage; WORD wCodePage; } *lpTranslate; VerQueryValue(InfoBuf, TEXT("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslate, &cbTranslate); // Read the file description for each language and code page. for (unsigned int i = 0; i < (cbTranslate / sizeof(LANGANDCODEPAGE)); i++) { TCHAR SubBlock[200]; _stprintf_s(SubBlock, _T("\\StringFileInfo\\%04x%04x\\FileVersion"), lpTranslate[i].wLanguage, lpTranslate[i].wCodePage); void *lpBuffer = NULL; unsigned int dwBytes = 0; VerQueryValue(InfoBuf, SubBlock, &lpBuffer, &dwBytes); CString strTemp = (TCHAR*)lpBuffer; strVersion += strTemp; } if (InfoBuf) { delete[]InfoBuf; } return strVersion; } bool GetFileVersion(HMODULE hModule, WORD *pBuffer) { TCHAR fname[MAX_PATH]; VS_FIXEDFILEINFO *pVi; DWORD dwHandle; string str; if (::GetModuleFileName(hModule, fname, MAX_PATH)) { int size = GetFileVersionInfoSize(fname, &dwHandle); if (size > 0) { BYTE *buffer = new BYTE[size]; memset(buffer, 0, size); if (GetFileVersionInfo(fname, dwHandle, size, buffer)) { if (VerQueryValue(buffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&size)) { pBuffer[0] = HIWORD(pVi->dwFileVersionMS); pBuffer[1] = LOWORD(pVi->dwFileVersionMS); pBuffer[2] = HIWORD(pVi->dwFileVersionLS); pBuffer[3] = LOWORD(pVi->dwFileVersionLS); delete[]buffer; return true; } } delete[]buffer; } } return false; } string GetFileVersion(HMODULE hModule) { string str; WORD buffer[4]; if (GetFileVersion(hModule, buffer)) { char str2[32]; for (int i = 0; i < sizeof(buffer) / sizeof(WORD); i++) { _itoa_s(buffer[i], str2, 10); str += str2; if (i != sizeof(buffer) / sizeof(WORD) - 1) { str += "."; } } } return str; } /************************************************************************/ /* 函数:GetProductVersion 描述:获取可执行文件的产品版号; 参数: hModule[IN] 可执行文件模块句柄; dwArray[OUT] 返回的产品版本号; 返回: 成功返回TRUE,失败返回FALSE; 注意: 当hModule为空时,表示要获取的可执行文件为本程序的产品版本号; */ /************************************************************************/ BOOL GetProductVersion(IN HMODULE hModule, IN DWORD (&dwArray)[4] ) { TCHAR fname[MAX_PATH]; VS_FIXEDFILEINFO *pVi; DWORD dwHandle; if (::GetModuleFileName(hModule, fname, MAX_PATH)) { INT nSize = GetFileVersionInfoSize(fname, &dwHandle); if (nSize > 0) { BYTE *pBuffer = new BYTE[nSize]; memset(pBuffer, 0, nSize); if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer)) { if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize)) { dwArray[0] = HIWORD(pVi->dwProductVersionMS); dwArray[1] = LOWORD(pVi->dwProductVersionMS); dwArray[2] = HIWORD(pVi->dwProductVersionLS); dwArray[3] = LOWORD(pVi->dwProductVersionLS); if(pBuffer) delete[]pBuffer; return TRUE; } } if(pBuffer) delete[]pBuffer; } } return FALSE; } /************************************************************************/ /* 函数:GetProductVersion 描述:获取可执行文件的产品版号; 参数: lpFileName[IN] 可执行文件名全路径; dwArray[OUT] 返回的产品版本号; 返回: 成功返回TRUE,失败返回FALSE; 注意: */ /************************************************************************/ BOOL GetProductVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] ) { if ( lpFileName == NULL || !PathFileExists(lpFileName) ) { OutputDebugString(_T("文件名错误或文件不存在\n")); return FALSE; } DWORD dwHandle = 0; VS_FIXEDFILEINFO *pVi = NULL; INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle); if ( nSize > 0 ) { BYTE *pBuffer = new BYTE[nSize]; memset(pBuffer, 0, nSize); if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) ) { if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize)) { dwArray[0] = HIWORD(pVi->dwProductVersionMS); dwArray[1] = LOWORD(pVi->dwProductVersionMS); dwArray[2] = HIWORD(pVi->dwProductVersionLS); dwArray[3] = LOWORD(pVi->dwProductVersionLS); if (pBuffer) delete[]pBuffer; return TRUE; } } if (pBuffer) delete[]pBuffer; } return FALSE; } BOOL RegisterOcx(LPCTSTR OcxFileName) { LPCTSTR pszDllName = OcxFileName; // ActiveX控件的路径及文件名 HINSTANCE hLib = LoadLibrary(pszDllName); // 装载ActiveX控件 //HINSTANCE hLib = LoadLibraryEx(pszDllName,NULL,LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE ); //LOAD_LIBRARY_AS_DATAFILE|LOAD_WITH_ALTERED_SEARCH_PATH|DONT_RESOLVE_DLL_REFERENCES 装载ActiveX控件 TRACE("----------------"); if (hLib < (HINSTANCE)HINSTANCE_ERROR) { return FALSE; } // FARPROC lpDllGetClass; // lpDllGetClass = GetProcAddress(hLib,_T("DllGetClassObject")); //获取注册函数DllRegisterServer地址 // // if(lpDllGetClass != NULL) //调用注册函数DllRegisterServer // { // if(FAILED((*lpDllGetClass)())) // { // FreeLibrary(hLib); // return FALSE ; // } // return TRUE ; // } // else // return FALSE ; FARPROC lpDllEntryPoint; lpDllEntryPoint = GetProcAddress(hLib, "DllRegisterServer"); //获取注册函数DllRegisterServer地址 //typedef int (CALLBACK* myfunction)(void); if (lpDllEntryPoint != NULL) //调用注册函数DllRegisterServer { if (FAILED((*lpDllEntryPoint)())) { FreeLibrary(hLib); return FALSE; } return TRUE; } else return FALSE; } BOOL UnRegisterOcx(LPCTSTR OcxFileName) { LPCTSTR pszDllName = OcxFileName; //ActiveX控件的路径及文件名 HINSTANCE hLib = LoadLibrary(pszDllName); //装载ActiveX控件 if (hLib < (HINSTANCE)HINSTANCE_ERROR) { return FALSE; } FARPROC lpDllEntryPoint; lpDllEntryPoint = GetProcAddress(hLib, "DllUnregisterServer"); //获取注册函数DllUnregisterServer地址 if (lpDllEntryPoint != NULL) //调用注册函数DllUnregisterServer { if (FAILED((*lpDllEntryPoint)())) { FreeLibrary(hLib); return FALSE; } return TRUE; } else return FALSE; } BOOL IsReg() // 没多大意义,只针对已知控件的CLSID才行; 不能通用; { HKEY hKey; BOOL bPresent; TCHAR szPath[_MAX_PATH]; DWORD dwRegType; DWORD cbData = sizeof szPath * sizeof TCHAR; hKey = NULL; bPresent = FALSE; ::RegOpenKey(HKEY_CLASSES_ROOT, _T("CLSID\\{4CAD92F0-D7C4-11D0-BCF7-00C04FC2FB86}\\InprocServer32"), &hKey); if (hKey) { HANDLE hfile; szPath[0] = 0; ::RegQueryValueEx(hKey, NULL, NULL, &dwRegType, (LPBYTE)szPath, &cbData); ::RegCloseKey(hKey); hfile = ::CreateFile(szPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (INVALID_HANDLE_VALUE != hfile) { bPresent = TRUE; ::CloseHandle(hfile); } } return bPresent; } BOOL IsRegistOcx() // 没多大意义,只针对已知控件的CLSID才行; { HKEY hKey; //BOOL bPresent; TCHAR szPath[_MAX_PATH]; //DWORD dwRegType; DWORD cbData = sizeof szPath * sizeof TCHAR; if (RegOpenKeyEx(HKEY_CLASSES_ROOT, _T("ActiveX.ActiveXControl//Clsid"), 0, KEY_READ, &hKey) != ERROR_SUCCESS) return false; else return true; //ActiveX.ActiveXControl为控件的名称和内部名称 如"NTGRAPH.NTGraphCtrl.1//CLSID" } /************************************************************************/ /* 目标:HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES\WOW6432NODE\CLSID\{AC5D0DDE-BD4C-11D1-B137-0000F8753F5D}\INPROCSERVER32\[] 目标:HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES\WOW6432NODE\CLSID\{3A2B370C-BA0A-11D1-B137-0000F8753F5D}\INPROCSERVER32\[] */ /************************************************************************/ bool RegOcx(CString ocxfile) { HKEY hKey; bool bRet; USES_CONVERSION; ITypeLib* pTypeLib; CString CLSIDStr; CLSID clsid; LPOLESTR wRes = NULL; #ifndef UNICODE if (LoadTypeLib(T2OLE(ocxfile), &pTypeLib) == S_OK) #else if (LoadTypeLib(ocxfile, &pTypeLib) == S_OK) #endif { TLIBATTR tlibAttr, *ptlibAttr; ptlibAttr = &tlibAttr; pTypeLib->GetLibAttr(&ptlibAttr); clsid = ptlibAttr->guid; pTypeLib->Release(); } ::StringFromCLSID(clsid, &wRes); CLSIDStr = OLE2CT(wRes); #if 0 // Jeff. 未完…… // 注册表的位置与操作系统版本有关; // 用SYSTEM_INFO结构判断64位AMD处理器; SYSTEM_INFO info; // 调用GetSystemInfo函数填充结构; GetSystemInfo(&info); OSVERSIONINFOEX os; // 在调用函数前必须用sizeof(OSVERSIONINFOEXA)填充dwOSVersionInfoSize结构成员; os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if (GetVersionEx((OSVERSIONINFO *)&os)) { if (os.dwMajorVersion <= 5) { } else if (os.dwMajorVersion >= 6) { } } #else //CLSIDStr.Format("{%s}",OLE2CT(wRes)); //if(RegOpenKeyEx(HKEY_CLASSES_ROOT,"CLSID\\"+CLSIDStr,0,KEY_ALL_ACCESS,&hKey)==ERROR_SUCCESS) //if( ::RegOpenKeyEx(HKEY_CLASSES_ROOT,"CLSID\\"+CLSIDStr,0,KEY_READ,&hKey) == ERROR_SUCCESS) // 只适合xp的; if (::RegOpenKeyEx(HKEY_CLASSES_ROOT, CString(_T("Wow6432Node\\TypeLib\\")) + CLSIDStr, 0, KEY_READ, &hKey) == ERROR_SUCCESS) { return true; } #endif else { HINSTANCE hLib = LoadLibrary(ocxfile); if (hLib < (HINSTANCE)HINSTANCE_ERROR) { MessageBox(0, _T("不能载入OCX文件!无法注册控件!"), _T("结果"), MB_OK); bRet = false; } else { FARPROC lpDllEntryPoint; lpDllEntryPoint = GetProcAddress(hLib, "DllRegisterServer"); if (lpDllEntryPoint != NULL) { if (FAILED((*lpDllEntryPoint)())) { MessageBox(0, _T("调用注册函数(DllRegisterServer)失败!无法注册控件!"), _T("结果"), MB_OK); bRet = false; } else bRet = true; } else { MessageBox(0, _T("调用注册函数(DllRegisterServer)失败!无法注册控件!"), _T("结果"), MB_OK); bRet = false; } } FreeLibrary(hLib); return bRet; } } //------------------------------------------------------------------------------------ /* // 函数原型: // BOOL GetVersionEx(POSVERSIONINFO pVersionInformation); // 我们先来看看OSVERSIONINFOEX这个结构: typedef struct { DWORD dwOSVersionInfoSize; //在使用GetVersionEx之前要将此初始化为结构的大小 DWORD dwMajorVersion; //系统主版本号 DWORD dwMinorVersion; //系统次版本号 DWORD dwBuildNumber; //系统构建号 DWORD dwPlatformId; //系统支持的平台(详见附1) TCHAR szCSDVersion[128]; //系统补丁包的名称 WORD wServicePackMajor; //系统补丁包的主版本 WORD wServicePackMinor; //系统补丁包的次版本 WORD wSuiteMask; //标识系统上的程序组(详见附2) BYTE wProductType; //标识系统类型(详见附3) BYTE wReserved; //保留,未使用 } OSVERSIONINFOEX, *POSVERSIONINFOEX; 这个结构在Windows 2000后出现,老版本的OSVERSIONINFO结构没有wServicePackMajor、wServicePackMinor、wSuiteMask、wProductType和wReserved这几个成员。 接着在调用函数之前先初始化结构的大小: OSVERSIONINFOEX os; os.dwOSVersionInfoSize=sizeof(os); 函数返回值为TRUE表示成功: if(!GetVersionEx((OSVERSIONINFO *)&os)) { return FALSE; } 函数调用成功以后就可以通过OSVERSIONINFOEX来查看系统的版本信息了。 下面是一个通过系统版本来判断操作系统名称的例子: */ CString getSystemName() { SYSTEM_INFO info; // 用SYSTEM_INFO结构判断64位AMD处理器 GetSystemInfo(&info); // 调用GetSystemInfo函数填充结构 OSVERSIONINFOEX os; os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); /*在调用函数前必须用sizeof(OSVERSIONINFOEXA)填充dwOSVersionInfoSize结构成员*/ if (GetVersionEx((OSVERSIONINFO *)&os)) /*调用GetVersionEx函数OSVERSIONINFOEX结构必须将指针类型强制转换*/ { CString vname; //下面根据版本信息判断操作系统名称; switch (os.dwMajorVersion) //判断主版本号 ; { case 4: switch (os.dwMinorVersion) //判断次版本号 ; { case 0: if (os.dwPlatformId == VER_PLATFORM_WIN32_NT) vname = _T("Microsoft Windows NT 4.0"); //1996年7月发布 else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) vname = _T("Microsoft Windows 95"); break; case 10: vname = _T("Microsoft Windows 98"); break; case 90: vname = _T("Microsoft Windows Me"); break; } break; case 5: switch (os.dwMinorVersion) //再比较dwMinorVersion的值 { case 0: vname = _T("Microsoft Windows 2000"); //1999年12月发布 break; case 1: vname = _T("Microsoft Windows XP"); //2001年8月发布 break; case 2: if (os.wProductType == VER_NT_WORKSTATION && info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) vname = _T("Microsoft Windows XP Professional x64 Edition"); else if (GetSystemMetrics(SM_SERVERR2) == 0) vname = _T("Microsoft Windows Server 2003"); //2003年3月发布 else if (GetSystemMetrics(SM_SERVERR2) != 0) vname = _T("Microsoft Windows Server 2003 R2"); break; } break; case 6: switch (os.dwMinorVersion) { case 0: if (os.wProductType == VER_NT_WORKSTATION) /*VER_NT_WORKSTATION是桌面系统 */ vname = _T("Microsoft Windows Vista"); else vname = _T("Microsoft Windows Server 2008"); //服务器版本 break; case 1: if (os.wProductType == VER_NT_WORKSTATION) vname = _T("Microsoft Windows 7"); else vname = _T("Microsoft Windows Server 2008 R2"); break; } break; default: vname = _T("未知操作系统"); } return vname; } else return _T(""); } /* 以上只能判断操作系统的名称,那如何判断它是什么版本的呢(如Home版或Professional版)。 下面这个函数就返回具体的版本名称: */ CString getVersionMark() { OSVERSIONINFOEX os; os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if (GetVersionEx((OSVERSIONINFO *)&os)) { CString vmark; switch (os.dwMajorVersion) //先判断操作系统版本 { case 5: switch (os.dwMinorVersion) { case 0: //Windows 2000 if (os.wSuiteMask == VER_SUITE_ENTERPRISE) vmark = _T("Advanced Server"); break; case 1: //Windows XP if (os.wSuiteMask == VER_SUITE_EMBEDDEDNT) vmark = _T("Embedded"); else if (os.wSuiteMask == VER_SUITE_PERSONAL) vmark = _T("Home Edition"); else vmark = _T("Professional"); break; case 2: if (GetSystemMetrics(SM_SERVERR2) == 0 && os.wSuiteMask == VER_SUITE_BLADE) //Windows Server 2003 vmark = _T("Web Edition"); else if (GetSystemMetrics(SM_SERVERR2) == 0 && os.wSuiteMask == VER_SUITE_COMPUTE_SERVER) vmark = _T("Compute Cluster Edition"); else if (GetSystemMetrics(SM_SERVERR2) == 0 && os.wSuiteMask == VER_SUITE_STORAGE_SERVER) vmark = _T("Storage Server"); else if (GetSystemMetrics(SM_SERVERR2) == 0 && os.wSuiteMask == VER_SUITE_DATACENTER) vmark = _T("Datacenter Edition"); else if (GetSystemMetrics(SM_SERVERR2) == 0 && os.wSuiteMask == VER_SUITE_ENTERPRISE) vmark = _T("Enterprise Edition"); else if (GetSystemMetrics(SM_SERVERR2) != 0 && os.wSuiteMask == VER_SUITE_STORAGE_SERVER) /*Windows Server 2003 R2*/ vmark = _T("Storage Server"); break; } break; case 6: switch (os.dwMinorVersion) { case 0: if (os.wProductType != VER_NT_WORKSTATION && os.wSuiteMask == VER_SUITE_DATACENTER) /*Windows Server 2008*/ vmark = _T("Datacenter Server"); else if (os.wProductType != VER_NT_WORKSTATION && os.wSuiteMask == VER_SUITE_ENTERPRISE) vmark = _T("Enterprise"); else if (os.wProductType == VER_NT_WORKSTATION && os.wSuiteMask == VER_SUITE_PERSONAL) //Windows Vista vmark = _T("Home"); break; } break; default: vmark = _T(""); } return vmark; } else return _T(""); } /* 另外,操作系统的补丁包名称也可以从szCSDVersion直接获取。 程序编译出来后大家可以用不同版本的兼容模式运行这个程序, 来检测结果是不是正确,兼容模式会欺骗GetVersionEx这些API函数,拿Windows 98 兼容模式来说,程序会以为自己是在Windows 98 上运行。 有什么问题请给我留言,如果在本页面下发没有看到有留言请刷新一下页面。 附1:dwPlatformId的值: VER_PLATFORM_WIN32s (Win32s), VER_PLATFORM_WIN32_WINDOWS (Windows 95/Windows 98), VER_PLATFORM_WIN32_NT (Windows NT/Windows 2000), VER_PLATFORM_WIN32_CEHH (Windows CE). 附2:wSuiteMask的值: VER_SUITE_SMALLBUSINESS, VER_SUITE_ENTERPRISE, VER_SUITE_BACKOFFICE, VER_SUITE_COMMUNICATIONS, VER_SUITE_TERMINAL, VER_SUITE_SMALLBUSINESS_RESTRICTED, VER_SUITE_EMBEDDEDNT, VER_SUITE_DATACENTER. 附3:wProductType的值: VER_NT_WORKSTATION, VER_NT_SERVER, VER_NT_DOMAIN_CONTROLLER. */ //--------------------------------------------------------------------- BOOL GetOSDisplayString(LPTSTR pszOS) { OSVERSIONINFOEX osvi; SYSTEM_INFO si; PGNSI pGNSI; PGPI pGPI; BOOL bOsVersionInfoEx; DWORD dwType; ZeroMemory(&si, sizeof(SYSTEM_INFO)); ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if (!(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *)&osvi))) return 1; // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise. pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo"); if (NULL != pGNSI) pGNSI(&si); else GetSystemInfo(&si); if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4) { StringCchCopy(pszOS, BUFSIZE, TEXT("Microsoft ")); // Test for the specific product. if (osvi.dwMajorVersion == 6) { if (osvi.dwMinorVersion == 0) { if (osvi.wProductType == VER_NT_WORKSTATION) StringCchCat(pszOS, BUFSIZE, TEXT("Windows Vista ")); else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 ")); } if (osvi.dwMinorVersion == 1) { if (osvi.wProductType == VER_NT_WORKSTATION) StringCchCat(pszOS, BUFSIZE, TEXT("Windows 7 ")); else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 R2 ")); } pGPI = (PGPI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo"); pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType); switch (dwType) { case PRODUCT_ULTIMATE: StringCchCat(pszOS, BUFSIZE, TEXT("Ultimate Edition")); break; case PRODUCT_HOME_PREMIUM: StringCchCat(pszOS, BUFSIZE, TEXT("Home Premium Edition")); break; case PRODUCT_HOME_BASIC: StringCchCat(pszOS, BUFSIZE, TEXT("Home Basic Edition")); break; case PRODUCT_ENTERPRISE: StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition")); break; case PRODUCT_BUSINESS: StringCchCat(pszOS, BUFSIZE, TEXT("Business Edition")); break; case PRODUCT_STARTER: StringCchCat(pszOS, BUFSIZE, TEXT("Starter Edition")); break; case PRODUCT_CLUSTER_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Cluster Server Edition")); break; case PRODUCT_DATACENTER_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition")); break; case PRODUCT_DATACENTER_SERVER_CORE: StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition (core installation)")); break; case PRODUCT_ENTERPRISE_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition")); break; case PRODUCT_ENTERPRISE_SERVER_CORE: StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition (core installation)")); break; case PRODUCT_ENTERPRISE_SERVER_IA64: StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems")); break; case PRODUCT_SMALLBUSINESS_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server")); break; case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM: StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server Premium Edition")); break; case PRODUCT_STANDARD_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition")); break; case PRODUCT_STANDARD_SERVER_CORE: StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition (core installation)")); break; case PRODUCT_WEB_SERVER: StringCchCat(pszOS, BUFSIZE, TEXT("Web Server Edition")); break; } } if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2) { if (GetSystemMetrics(SM_SERVERR2)) StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003 R2, ")); else if (osvi.wSuiteMask == VER_SUITE_STORAGE_SERVER) StringCchCat(pszOS, BUFSIZE, TEXT("Windows Storage Server 2003")); else if (osvi.wSuiteMask == VER_SUITE_WH_SERVER) StringCchCat(pszOS, BUFSIZE, TEXT("Windows Home Server")); else if (osvi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) { StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP Professional x64 Edition")); } else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003, ")); // Test for the server type. if (osvi.wProductType != VER_NT_WORKSTATION) { if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64) { if (osvi.wSuiteMask & VER_SUITE_DATACENTER) StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition for Itanium-based Systems")); else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems")); } else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) { if (osvi.wSuiteMask & VER_SUITE_DATACENTER) StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter x64 Edition")); else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise x64 Edition")); else StringCchCat(pszOS, BUFSIZE, TEXT("Standard x64 Edition")); } else { if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER) StringCchCat(pszOS, BUFSIZE, TEXT("Compute Cluster Edition")); else if (osvi.wSuiteMask & VER_SUITE_DATACENTER) StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition")); else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition")); else if (osvi.wSuiteMask & VER_SUITE_BLADE) StringCchCat(pszOS, BUFSIZE, TEXT("Web Edition")); else StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition")); } } } if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) { StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP ")); if (osvi.wSuiteMask & VER_SUITE_PERSONAL) StringCchCat(pszOS, BUFSIZE, TEXT("Home Edition")); else StringCchCat(pszOS, BUFSIZE, TEXT("Professional")); } if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0) { StringCchCat(pszOS, BUFSIZE, TEXT("Windows 2000 ")); if (osvi.wProductType == VER_NT_WORKSTATION) { StringCchCat(pszOS, BUFSIZE, TEXT("Professional")); } else { if (osvi.wSuiteMask & VER_SUITE_DATACENTER) StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Server")); else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE) StringCchCat(pszOS, BUFSIZE, TEXT("Advanced Server")); else StringCchCat(pszOS, BUFSIZE, TEXT("Server")); } } // Include service pack (if any) and build number. if (_tcslen(osvi.szCSDVersion) > 0) { StringCchCat(pszOS, BUFSIZE, TEXT(" ")); StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion); } TCHAR buf[80]; StringCchPrintf(buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber); StringCchCat(pszOS, BUFSIZE, buf); if (osvi.dwMajorVersion >= 6) { if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) StringCchCat(pszOS, BUFSIZE, TEXT(", 64-bit")); else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL) StringCchCat(pszOS, BUFSIZE, TEXT(", 32-bit")); } return TRUE; } else { _tprintf(_T("This sample does not support this version of Windows.\n")); return FALSE; } } void WriteLog(const CString &str) // 写日志; { //SecurityKeylock lock(_T("lyfzUpgrade")); try { CStdioFile fp; CString path = CString(g_ModulePath) + _T("升级包日志.txt"); if (PathFileExists(path)) { fp.Open(path, CFile::modeWrite); ULONGLONG length = fp.GetLength(); if (length > 5 * 1024 * 1024) { fp.Close(); ::DeleteFile(path); return; } fp.SeekToEnd(); } else fp.Open(path, CFile::modeCreate | CFile::modeWrite); #ifdef UNICODE char* old_locale = _strdup(setlocale(LC_CTYPE, NULL)); setlocale(LC_CTYPE, "chs");//设定 // WriteString在UNICODE下无法写中文,需要setlocale语言区域; fp.WriteString((CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S")) + _T(" ") + str + _T("\n"))); setlocale(LC_CTYPE, old_locale); free(old_locale);//还原区域设定 #else fp.WriteString((CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S")) + _T(" ") + str + _T("\n"))); #endif fp.Close(); } catch (...) { } } //--------------------------------------------------------------------- // add by Jeff 2014.10.24 // 函数:全局函数lyfzCopyFileEx // 描述: // 参数: // lpExistingFileName:你要拷贝的源文件名; // lpNewFileName: 你要拷贝的目标文件名; // bFailIfExists: True:如果目标已经存在,不覆盖目标(True)并重命名; FALSE:如果目标已经存在,覆盖目标 // 返回: //--------------------------------------------------------------------- BOOL lyfzCopyFileEx(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, const BOOL &bFailIfExists) { // 1.如果是覆盖选项; if (!bFailIfExists) return CopyFile(lpExistingFileName, lpNewFileName, bFailIfExists); // 2.如果不是覆盖选项; TCHAR szNewFileName[_MAX_PATH] = { 0 }; TCHAR szDrive[_MAX_DRIVE] = { 0 }; TCHAR szDir[_MAX_DIR] = { 0 }; TCHAR szFna[_MAX_FNAME] = { 0 }; TCHAR szExt[_MAX_EXT] = { 0 }; _stprintf_s(szNewFileName, _T("%s"), lpNewFileName); _tsplitpath_s(szNewFileName, szDrive, szDir, szFna, szExt); int nIndex = 1; while (PathFileExists(szNewFileName)) { _stprintf_s(szNewFileName, _T("%s%s%s (%d)%s"), szDrive, szDir, szFna, nIndex++, szExt); } return CopyFile(lpExistingFileName, szNewFileName, bFailIfExists); } #if 0 //--------------------------------------------------------------------- // add by Jeff 2014.10.25 // 函数:全局函数lyfzCreateDirectory // 描述:创建目录及子目录文件夹; // 参数: // lpDestDirectory:目录,必须是"E:\lyfzdb\数据\"格式,目录最后必须有"\" // 建议使用系统_tsplitpath来分隔目录,这样会保存最后一个"\"斜杠; // 返回:成功创建返回TRUE; //--------------------------------------------------------------------- BOOL lyfzCreateDirectory(LPCTSTR lpDestDirectory) { BOOL bExists = FALSE; TCHAR szNewFileName[_MAX_PATH] = { 0 }; TCHAR szDrive[_MAX_DRIVE] = { 0 }; TCHAR szDir[_MAX_DIR] = { 0 }; TCHAR szFna[_MAX_FNAME] = { 0 }; TCHAR szExt[_MAX_EXT] = { 0 }; int nIndex = 0; //sprintf(szNewFileName, "%s", lpDestDirectory); do { bExists = PathFileExists(lpDestDirectory); if (!bExists) { memset(szDrive, 0, _MAX_DRIVE*sizeof(TCHAR)); memset(szDir, 0, _MAX_DIR*sizeof(TCHAR)); memset(szFna, 0, _MAX_FNAME*sizeof(TCHAR)); memset(szExt, 0, _MAX_EXT*sizeof(TCHAR)); tSplitpath(lpDestDirectory, szDrive, szDir, ++nIndex); swprintf_s(szNewFileName, L"%s%s", szDrive, szDir); if (FALSE == CreateDirectory(szNewFileName, NULL)) { return FALSE; } } } while (!bExists); return TRUE; } #endif void tSplitpath(const char *path, char *drive, char *dir, const int &nTimes) { int nlen = strlen(path); int i = 0; int npoint = 0; // '.' int nsprit = 0; // '\\' int ncolon = 0; // ':' int ntimes = -1; while (nlen > i++) { if (path[i] == ':') ncolon = i; else if (path[i] == '\\') { if (nTimes == ntimes++) break; nsprit = i; } } memcpy(drive, path, ncolon + 1); memcpy(dir, &path[ncolon + 1], nsprit - ncolon); } void tSplitpath(const char *path, char *drive, char *dir, char *fname, char *ext) { #if 0 // Jeff:从尾部开始; int nlen = strlen(path); int i = nlen; int npoint = 0; // '.' int nsprit = 0; // '\\' int ncolon = 0; // ':' while (i--) { if (path[nlen - 1] == '\\' && i == nlen - 1) { continue; } if (path[i] == '.' && !npoint) { npoint = i; memcpy(ext, &path[i], nlen - i); } else if (path[i] == '\\' && !nsprit) { nsprit = i; memcpy(fname, &path[i + 1], abs(npoint - i)); } else if (path[i] == ':' && !ncolon) { ncolon = i; memcpy(dir, &path[i + 1], nsprit - i); break; } } memcpy(drive, path, i + 1); #else // 从头部开始 int nlen = strlen(path); int i = 0; int npoint = 0; // '.' int nsprit = 0; // '\\' int ncolon = 0; // ':' while (nlen > i++) { if (path[i] == ':') ncolon = i; else if (path[i] == '\\') nsprit = i; else if (path[i] == '.') npoint = i; } memcpy(drive, path, ncolon + 1); memcpy(dir, &path[ncolon + 1], nsprit - ncolon - 1); memcpy(fname, &path[nsprit + 1], abs(npoint - nsprit - 1)); if (npoint) memcpy(ext, &path[npoint], nlen - npoint); #endif } //--------------------------------------------------------------------- // add by Jeff 2014.10.27 // 函数:全局函数IsDirectoryLegitimate,多字节版本,非UNICODE // 描述:判断一个目录路径字符串,是否属于合法的、可创建的目录路径。 // 参数:strDirectory 被验证的路径字符串; // // 返回:合法路径返回TRUE; //--------------------------------------------------------------------- BOOL IsDirectoryLegitimate(const CString &strDirectory) { if (strDirectory.Find('/') != -1 || strDirectory.Find('\\') != -1 || strDirectory.Find(':') != -1 || strDirectory.Find('*') != -1 || strDirectory.Find('?') != -1 || strDirectory.Find('\"') != -1 || strDirectory.Find('>') != -1 || strDirectory.Find('<') != -1 || strDirectory.Find('|') != -1 ) return FALSE; return TRUE; } //-------------------------------------------------------------------------------- // Jeff add 2014.06.23; // 函数:ErrorExit // 描述: // 参数: // lpszFunction:函数名; // dwError:错误码; // //-------------------------------------------------------------------------------- void ShowSystemErrorInfo(CString strDescription, const DWORD &dwError) { #if 1 LPVOID lpMsgBuf; BOOL fOk = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); if (!fOk) { // Is it a network-related error? HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES); if (hDll != NULL) { FormatMessage( FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, hDll, dwError, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPTSTR)&lpMsgBuf, 0, NULL); FreeLibrary(hDll); } } if (lpMsgBuf != NULL) { CString strDisplay; strDisplay.Format(_T("%s.错误码=%d,Windows描述:%s"), strDescription, dwError, (PCTSTR)LocalLock(lpMsgBuf)); WriteLog(strDisplay); LocalFree(lpMsgBuf); } else { WriteLog(strDescription); } #else HLOCAL hlocal = NULL; // Buffer that gets the error message string // Get the error code's textual description BOOL fOk = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwError, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (PTSTR)&hlocal, 0, NULL); if (!fOk) { // Is it a network-related error? HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES); if (hDll != NULL) { FormatMessage( FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, hDll, dwError, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (PTSTR)&hlocal, 0, NULL); FreeLibrary(hDll); } } if (hlocal != NULL) { CString strDisplay; strDisplay.Format("%s 失败错误码=%d,Windows系统描述:%s", strDescription, dwError, (PCTSTR)LocalLock(hlocal)); WriteLog(strDisplay); LocalFree(hlocal); } else { WriteLog("Error number not found."); } #endif } // The system displays a dialog box with a custom message and a message to the user to close applications within the specified time-out interval (30 seconds). // After the time-out interval elapses, the system is restarted. //The application must enable the SE_SHUTDOWN_NAME privilege before calling InitiateSystemShutdown BOOL MySystemShutdown(LPTSTR lpMsg) { HANDLE hToken; // handle to process token TOKEN_PRIVILEGES tkp; // pointer to token structure BOOL fResult; // system shutdown flag // Get the current process token handle so we can get shutdown // privilege. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return FALSE; // Get the LUID for shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) return FALSE; // Display the shutdown dialog box and start the countdown. fResult = InitiateSystemShutdown( NULL, // shut down local computer lpMsg, // message for user 30, // time-out period, in seconds FALSE, // ask user to close apps TRUE); // reboot after shutdown if (!fResult) return FALSE; // Disable shutdown privilege. tkp.Privileges[0].Attributes = 0; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); return TRUE; } // If the AbortSystemShutdown function is executed in the time-out period specified by InitiateSystemShutdown, // the system does not shut down. For example, if PreventSystemShutdown is called after MySystemShutdown, // the system closes the dialog box and does not restart the system. BOOL PreventSystemShutdown() { HANDLE hToken; // handle to process token TOKEN_PRIVILEGES tkp; // pointer to token structure // Get the current process token handle so we can get shutdown // privilege. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return FALSE; // Get the LUID for shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); if (GetLastError() != ERROR_SUCCESS) return FALSE; // Prevent the system from shutting down. if (!AbortSystemShutdown(NULL)) return FALSE; // Disable shutdown privilege. tkp.Privileges[0].Attributes = 0; AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); return TRUE; } // Shutting down flushes file buffers to disk and brings the system to a condition in which it is safe to turn off the computer // The application must first enable the SE_SHUTDOWN_NAME privilege. // The final parameter in the call to ExitWindowsEx indicates that the system was shut down for a planning update of the operating system. BOOL MySystemShutdown() { HANDLE hToken; TOKEN_PRIVILEGES tkp; // Get a token for this process. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return(FALSE); // Get the LUID for the shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); if (GetLastError() != ERROR_SUCCESS) return FALSE; // Shut down the system and force all applications to close. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, SHTDN_REASON_MAJOR_OPERATINGSYSTEM | SHTDN_REASON_MINOR_UPGRADE | SHTDN_REASON_FLAG_PLANNED)) return FALSE; return TRUE; }