123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079 |
- #include "StdAfx.h"
- #include <tlhelp32.h>
- #include "Global.h"
- #include "my32.h"
- #pragma comment(lib,"my32.lib")
- #include <windows.h> // 关机函数;
- // Jeff.获取文件版本号函数头文件;
- #include <WinVer.h>
- #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 <strsafe.h>
- #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<int, int> g_nYearArray;
- CArray<CDatabase*, CDatabase*> 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;
- }
|