Global.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079
  1. #include "StdAfx.h"
  2. #include <tlhelp32.h>
  3. #include "Global.h"
  4. #include "my32.h"
  5. #pragma comment(lib,"my32.lib")
  6. #include <windows.h> // 关机函数;
  7. // Jeff.获取文件版本号函数头文件;
  8. #include <WinVer.h>
  9. #pragma comment(lib,"version.lib")
  10. using namespace std;
  11. // 注册OCX bool RegOcx(CString ocxfile)
  12. #include "afxcoll.h"
  13. #include "afxtempl.h"
  14. #include "atlconv.h"
  15. // 在函数GetOSDisplayString的前面需要定义如下宏:
  16. // 同时还要包含strsafe.h
  17. #include <strsafe.h>
  18. #pragma comment(lib, "User32.lib")
  19. #define BUFSIZE 256
  20. #define PRODUCT_ULTIMATE 0x00000001
  21. #define PRODUCT_HOME_BASIC 0x00000002
  22. #define PRODUCT_HOME_PREMIUM 0x00000003
  23. #define PRODUCT_ENTERPRISE 0x00000004
  24. #define PRODUCT_HOME_BASIC_N 0x00000005
  25. #define PRODUCT_BUSINESS 0x00000006
  26. #define PRODUCT_STANDARD_SERVER 0x00000007
  27. #define PRODUCT_DATACENTER_SERVER 0x00000008
  28. #define PRODUCT_SMALLBUSINESS_SERVER 0x00000009
  29. #define PRODUCT_ENTERPRISE_SERVER 0x0000000A
  30. #define PRODUCT_STARTER 0x0000000B
  31. #define PRODUCT_DATACENTER_SERVER_CORE 0x0000000C
  32. #define PRODUCT_STANDARD_SERVER_CORE 0x0000000D
  33. #define PRODUCT_ENTERPRISE_SERVER_CORE 0x0000000E
  34. #define PRODUCT_ENTERPRISE_SERVER_IA64 0x0000000F
  35. #define PRODUCT_BUSINESS_N 0x00000010
  36. #define PRODUCT_WEB_SERVER 0x00000011
  37. #define PRODUCT_CLUSTER_SERVER 0x00000012
  38. #define PRODUCT_HOME_SERVER 0x00000013
  39. #define PRODUCT_STORAGE_EXPRESS_SERVER 0x00000014
  40. #define PRODUCT_STORAGE_STANDARD_SERVER 0x00000015
  41. #define PRODUCT_STORAGE_WORKGROUP_SERVER 0x00000016
  42. #define PRODUCT_STORAGE_ENTERPRISE_SERVER 0x00000017
  43. #define PRODUCT_SERVER_FOR_SMALLBUSINESS 0x00000018
  44. #define PRODUCT_SMALLBUSINESS_SERVER_PREMIUM 0x00000019
  45. #define PRODUCT_HOME_PREMIUM_N 0x0000001A
  46. #define PRODUCT_ENTERPRISE_N 0x0000001B
  47. #define PRODUCT_ULTIMATE_N 0x0000001C
  48. #define PRODUCT_WEB_SERVER_CORE 0x0000001D
  49. #define PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT 0x0000001E
  50. #define PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY 0x0000001F
  51. #define PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING 0x00000020
  52. #define PRODUCT_SMALLBUSINESS_SERVER_PRIME 0x00000021
  53. #define PRODUCT_HOME_PREMIUM_SERVER 0x00000022
  54. #define PRODUCT_SERVER_FOR_SMALLBUSINESS_V 0x00000023
  55. #define PRODUCT_STANDARD_SERVER_V 0x00000024
  56. #define PRODUCT_DATACENTER_SERVER_V 0x00000025
  57. #define PRODUCT_ENTERPRISE_SERVER_V 0x00000026
  58. #define PRODUCT_DATACENTER_SERVER_CORE_V 0x00000027
  59. #define PRODUCT_STANDARD_SERVER_CORE_V 0x00000028
  60. #define PRODUCT_ENTERPRISE_SERVER_CORE_V 0x00000029
  61. #define PRODUCT_HYPERV 0x0000002A
  62. #define SM_TABLETPC 86
  63. #define SM_MEDIACENTER 87
  64. #define SM_STARTER 88
  65. #define SM_SERVERR2 89
  66. #define VER_SERVER_NT 0x80000000
  67. #define VER_WORKSTATION_NT 0x40000000
  68. #define VER_SUITE_SMALLBUSINESS 0x00000001
  69. #define VER_SUITE_ENTERPRISE 0x00000002
  70. #define VER_SUITE_BACKOFFICE 0x00000004
  71. #define VER_SUITE_COMMUNICATIONS 0x00000008
  72. #define VER_SUITE_TERMINAL 0x00000010
  73. #define VER_SUITE_SMALLBUSINESS_RESTRICTED 0x00000020
  74. #define VER_SUITE_EMBEDDEDNT 0x00000040
  75. #define VER_SUITE_DATACENTER 0x00000080
  76. #define VER_SUITE_SINGLEUSERTS 0x00000100
  77. #define VER_SUITE_PERSONAL 0x00000200
  78. #define VER_SUITE_BLADE 0x00000400
  79. #define VER_SUITE_EMBEDDED_RESTRICTED 0x00000800
  80. #define VER_SUITE_SECURITY_APPLIANCE 0x00001000
  81. #define VER_SUITE_STORAGE_SERVER 0x00002000
  82. #define VER_SUITE_COMPUTE_SERVER 0x00004000
  83. #define VER_SUITE_WH_SERVER 0x00008000
  84. typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
  85. typedef BOOL(WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
  86. //--------------------------------------------------------------
  87. CDatabase g_db;
  88. CDatabase g_masterdb;
  89. CDatabase *g_pCurdb = NULL;
  90. CArray<int, int> g_nYearArray;
  91. CArray<CDatabase*, CDatabase*> g_dbArray;
  92. TCHAR g_ModulePath[MAX_PATH] = _T(""); // 软件目录;
  93. TCHAR g_ModuleFileName[MAX_PATH] = _T(""); // 软件名称;
  94. TCHAR g_szConnectString[MAX_PATH] = _T(""); // DB连接串;
  95. // 服务器信息;
  96. TCHAR g_szDBSource[MAX_PATH] = _T(""); // 数据库源(服务所在IP或计算机名);
  97. TCHAR g_szDBSourcePort[MAX_PATH] = _T(""); // 数据库源端口;
  98. DWORD g_dwDBServerPort = 0; // 数据库源端口;
  99. TCHAR g_szDBAccount[MAX_PATH] = _T(""); // 数据库登录用户;
  100. TCHAR g_szDBPassWord[MAX_PATH] = _T(""); // 数据库登录密码;
  101. TCHAR g_szDBName[MAX_PATH] = _T(""); // 数据库名称;
  102. // 升级包版本号;
  103. DWORD g_arrFileVersion[4] = { 0 };
  104. CHAR g_szKeyPath[MAX_PATH] = {0};
  105. int g_nVersion = -1;
  106. // start------------6版本加密狗信息----------------;
  107. BYTE g_byVersion[7][26] =
  108. {
  109. { 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 }, // 儿童座席版;
  110. { 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 }, // 婚纱座席版;
  111. { 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 }, // 儿童企业版;
  112. { 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 }, // 婚纱企业版;
  113. { 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 }, // 儿童全能版;
  114. { 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 }, // 婚纱全能版;
  115. };
  116. BYTE g_byShotVersion[6][10] =
  117. {
  118. { 6, 0, 148, 11, 205, 50, 4, 171, 32, 39 }, // 儿童座席版;
  119. { 6, 0, 4, 29, 24, 247, 189, 95, 83, 76 }, // 婚纱座席版;
  120. { 6, 0, 4, 187, 103, 10, 59, 169, 240, 220 }, // 儿童企业版;
  121. { 6, 0, 137, 160, 230, 173, 202, 2, 117, 142 }, // 婚纱企业版;
  122. { 6, 0, 194, 199, 210, 67, 185, 1, 138, 195 }, // 儿童全能版;
  123. { 6, 0, 188, 145, 100, 47, 154, 177, 34, 205 }, // 婚纱全能版;
  124. };
  125. // endof------------6版本加密狗信息----------------;
  126. // start---------------加密表达式加密后的数组----------------------;
  127. BYTE g_byCalExVersion[7][114] =
  128. {
  129. // 儿童座席版;
  130. { 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,
  131. 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,
  132. 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,
  133. 4, 46, 120, 49, 120, 156, 27, 17, 164, 217, 79, 167, 146, 143, 132, 219, 173, 211, 2, 99, 180 },
  134. // 婚纱座席版;
  135. { 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,
  136. 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,
  137. 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,
  138. 213, 109, 99, 142, 219, 71, 26, 22, 220, 177, 179, 191, 82, 232, 182, 45, 27, 13, 195, 225, 204 },
  139. // 儿童企业版;
  140. { 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,
  141. 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,
  142. 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,
  143. 131, 79, 104, 158, 17, 172, 239, 4, 158, 201, 51, 62, 51, 248, 214, 27, 254, 221, 208, 181, 209 },
  144. // 婚纱企业版;
  145. { 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,
  146. 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,
  147. 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,
  148. 20, 9, 195, 153, 15, 22, 217, 243, 79, 177, 22, 24, 171, 122, 243, 190, 146, 77, 222, 208, 129 },
  149. // 儿童全能版;
  150. { 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,
  151. 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,
  152. 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,
  153. 128, 180, 105, 53, 137, 230, 254, 244, 50, 195, 49, 167, 143, 241, 214, 38, 180, 198, 3, 158, 165 },
  154. // 婚纱全能版;
  155. { 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,
  156. 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,
  157. 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,
  158. 146, 119, 58, 76, 194, 151, 239, 204, 104, 18, 166, 208, 54, 114, 118, 181, 24, 252, 41, 246, 88 },
  159. };
  160. // endof---------------加密表达式加密后的数组----------------------;
  161. //************************************//
  162. // 数据库地址;
  163. // 数据库端口;
  164. // 数据库管理员账号;
  165. // 数据库管理员密码;
  166. // 数据库名称;
  167. //
  168. // [函数]:GetIniInfo
  169. // [描述]:获取指定ini信息;
  170. // [参数]:
  171. // szPath:ini所在目录;
  172. // szIniName:ini名;
  173. // [返回]:void
  174. //
  175. //************************************//
  176. int GetIniInfo(const char *szPath, const char *szIniName)
  177. {
  178. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  179. TCHAR szDir[_MAX_DIR] = { 0 };
  180. TCHAR szFna[_MAX_DIR] = { 0 };
  181. TCHAR szExt[_MAX_DIR] = { 0 };
  182. ::GetModuleFileName(NULL, g_ModulePath, sizeof(g_ModulePath) / sizeof(TCHAR));
  183. _stprintf_s(g_ModuleFileName, _T("%s"), g_ModulePath);
  184. _tsplitpath_s(g_ModulePath, szDrive, szDir, szFna, szExt);
  185. _tcscpy_s(g_ModulePath, szDrive);
  186. _tcscat_s(g_ModulePath, szDir);
  187. // -----------------------------------------------------//
  188. TCHAR szFile[MAX_PATH + 1] = _T("");
  189. if (szPath != NULL && szIniName != NULL)
  190. _stprintf_s(szFile, _T("%s%s"), szPath, szIniName);
  191. else
  192. _stprintf_s(szFile, _T("%sServiceInfo.ini"), g_ModulePath);
  193. HANDLE hFile = CreateFile(szFile, 0/*GENERIC_READ*/, 0, NULL, OPEN_EXISTING, 0, NULL);
  194. if (ERROR_FILE_NOT_FOUND == GetLastError())
  195. {
  196. return -1;
  197. }
  198. TRACE("szFile =%s\n", szFile);
  199. // 获取服务器端信息;
  200. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbSource"), _T(""), g_szDBSource, MAX_PATH, szFile);
  201. g_dwDBServerPort = GetPrivateProfileInt(_T("DatabaseInfo"), _T("dbServerPort"), 0, szFile);
  202. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbAccount"), _T(""), g_szDBAccount, MAX_PATH, szFile);
  203. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbPassWord"), _T(""), g_szDBPassWord, MAX_PATH, szFile);
  204. GetPrivateProfileString(_T("DatabaseInfo"), _T("dbName"), _T(""), g_szDBName, MAX_PATH, szFile);
  205. if (g_dwDBServerPort != 0)
  206. _stprintf_s(g_szConnectString, _T("driver={SQL Server};Server=%s,%d;database=%s;uid=%s;pwd=%s"),
  207. g_szDBSource, g_dwDBServerPort, g_szDBName, g_szDBAccount, g_szDBPassWord);
  208. else
  209. _stprintf_s(g_szConnectString, _T("driver={SQL Server};Server=%s;database=%s;uid=%s;pwd=%s"),
  210. g_szDBSource, g_szDBName, g_szDBAccount, g_szDBPassWord);
  211. return 0;
  212. }
  213. BOOL FindAppProcessID(CString path, DWORD &dwProcessID)
  214. {
  215. int nIndex = path.ReverseFind('\\');
  216. path = path.Right(path.GetLength() - nIndex - 1);
  217. dwProcessID = FindProcess(path);
  218. return (dwProcessID != 0 ? TRUE : FALSE);
  219. BOOL bFound = FALSE;
  220. HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  221. PROCESSENTRY32 Info;
  222. Info.dwSize = sizeof(PROCESSENTRY32);
  223. path.MakeLower();
  224. if (::Process32First(handle, &Info))
  225. {
  226. do{
  227. CString ss = Info.szExeFile;
  228. ss.MakeLower();
  229. if (ss == path)
  230. {
  231. ::CloseHandle(handle);
  232. dwProcessID = Info.th32ProcessID;
  233. bFound = TRUE;
  234. break;
  235. }
  236. } while (::Process32Next(handle, &Info));
  237. if (handle) ::CloseHandle(handle);
  238. }
  239. return (bFound ? TRUE : FALSE);
  240. }
  241. DWORD FindProcess(__in CString strProName)
  242. {
  243. DWORD dwProcessID = 0;
  244. PROCESSENTRY32 pe32 = { 0 };
  245. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  246. if (hProcessSnap == NULL)
  247. {
  248. return 0;
  249. }
  250. pe32.dwSize = sizeof(PROCESSENTRY32);
  251. if (Process32First(hProcessSnap, &pe32))
  252. {
  253. do
  254. {
  255. if (_tcscmp(strProName.GetBuffer(), pe32.szExeFile) == 0)
  256. {
  257. dwProcessID = pe32.th32ProcessID;
  258. break;
  259. }
  260. } while (Process32Next(hProcessSnap, &pe32));
  261. }
  262. CloseHandle(hProcessSnap);
  263. return dwProcessID;
  264. }
  265. int FindAndCloseProcess(__in CString strProName)
  266. {
  267. int nIndex = strProName.ReverseFind('\\');
  268. if (nIndex != -1) // 如果传的是全路径;
  269. strProName = strProName.Right(strProName.GetLength() - nIndex - 1);
  270. DWORD dwProcessID = 0;
  271. PROCESSENTRY32 pe32 = { 0 };
  272. HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  273. if (hProcessSnap == NULL)
  274. {
  275. return 0;
  276. }
  277. pe32.dwSize = sizeof(PROCESSENTRY32);
  278. if (Process32First(hProcessSnap, &pe32))
  279. {
  280. do
  281. {
  282. if (_tcscmp(strProName.GetBuffer(), pe32.szExeFile) == 0)
  283. {
  284. dwProcessID = pe32.th32ProcessID;
  285. break;
  286. }
  287. } while (Process32Next(hProcessSnap, &pe32));
  288. }
  289. CloseHandle(hProcessSnap);
  290. if (dwProcessID == 0)
  291. return 0;
  292. HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwProcessID);
  293. if (hProcess == NULL)
  294. return 0;
  295. DWORD dwError;
  296. if (!TerminateProcess(hProcess, 0))
  297. {
  298. dwError = GetLastError();
  299. CloseHandle(hProcess);
  300. return -1;
  301. }
  302. // 等待进程结束响应;
  303. if (WAIT_OBJECT_0 != WaitForSingleObject(hProcess, INFINITE))
  304. {
  305. CloseHandle(hProcess);
  306. AfxMessageBox(_T("结束进程失败"));
  307. return -1;
  308. }
  309. CloseHandle(hProcess);
  310. return 1;
  311. }
  312. CDatabase *GetSQLMaster()
  313. {
  314. TCHAR szDBSource[MAX_PATH] = _T("");
  315. if (g_dwDBServerPort != 0)
  316. _stprintf_s(szDBSource, _T("%s,%d"), g_szDBSource, g_dwDBServerPort);
  317. else
  318. _stprintf_s(szDBSource, _T("%s"), g_szDBSource);
  319. CDatabase *pMasterdb = new CDatabase;
  320. TCHAR szConnectString[MAX_PATH] = _T("");
  321. try
  322. {
  323. _stprintf_s(szConnectString, _T("driver={SQL Server};Server=%s;database=master;uid=%s;pwd=%s"), szDBSource, g_szDBAccount, g_szDBPassWord);
  324. pMasterdb->OpenEx(szConnectString, CDatabase::noOdbcDialog);
  325. pMasterdb->SetQueryTimeout(120);
  326. }
  327. catch (CDBException * e)
  328. {
  329. AfxMessageBox(e->m_strError);
  330. AfxMessageBox(_T("连接SQL数据库失败!"));
  331. _tprintf(_T("%s!\n"), e->m_strError);
  332. _tprintf(_T("连接SQL数据库失败!\n"));
  333. if (pMasterdb)
  334. {
  335. delete pMasterdb;
  336. pMasterdb = NULL;
  337. }
  338. e->Delete();
  339. return NULL;
  340. }
  341. return pMasterdb;
  342. }
  343. int MyFindPort_2(int start, BYTE *InByte, int InLen, char *OutPath)
  344. {
  345. int n;
  346. int ret;
  347. int D[8];
  348. double F[8];
  349. char s0[50] = "", s1[50] = "", s2[50] = "", s3[50] = "", s4[50] = "", s5[50] = "", s6[50] = "", s7[50] = "";
  350. CHAR KeyPath[MAX_PATH] = "";
  351. //查找系统上所有的锁;
  352. for (n = 0; n < 256; n++)
  353. {
  354. ZeroMemory(KeyPath, sizeof(KeyPath));
  355. ret = FindPort(n, KeyPath);
  356. if (ret != 0 && n == 0)return -1053;//表示系统上没有任何智能锁;
  357. if (ret != 0)return ret;
  358. #if 1
  359. memcpy(g_szKeyPath,KeyPath,MAX_PATH);
  360. #endif
  361. //使用该设备路径锁进行运算;
  362. ret = CalEx(InByte, InLen, &D[0], &D[1], &D[2], &D[3], &D[4], &D[5], &D[6], &D[7],
  363. &F[0], &F[1], &F[2], &F[3], &F[4], &F[5], &F[6], &F[7],
  364. s0, s1, s2, s3, s4, s5, s6, s7, KeyPath, 20000);
  365. //如果正确,则返回该设备路径供以后使用;
  366. if (ret == -63)return ret;
  367. if ((ret == 0) && (D[0] == 123))return 0;
  368. }
  369. return -53;
  370. }
  371. int GetSoftWareVersion()
  372. {
  373. int nRet = 0;
  374. CHAR szUSBKeyPath[MAX_PATH] = "";
  375. for (int i(0); i < 6; i++)
  376. {
  377. nRet = MyFindPort_2(0, g_byShotVersion[i], 10, szUSBKeyPath);
  378. if (nRet == 0)
  379. {
  380. g_nVersion = i;
  381. break;
  382. }
  383. else
  384. {
  385. TCHAR szErrInfo[MAX_PATH] = _T("");
  386. GetUSBKey32ErrInfo(nRet,szErrInfo,MAX_PATH);
  387. }
  388. }
  389. return g_nVersion;
  390. }
  391. //------------------------------------------------------------------------------
  392. // Remark by Jeff 2014.11.05
  393. // 函数:GetUSBKey32ErrInfo
  394. // 描述:返回加密狗错误;
  395. // 参数:
  396. // lError:域天加密狗函数返回的错误码;
  397. // ErrorInfo:错误码描述缓冲区;
  398. // nErrorLen:错误码描述缓冲区大小;
  399. // 返回:无
  400. //
  401. //------------------------------------------------------------------------------
  402. void GetUSBKey32ErrInfo(IN const long lError, OUT TCHAR *ErrInfo, IN const INT_PTR &nErrorLen)
  403. {
  404. switch (lError)
  405. {
  406. case -1:
  407. StringCchCat(ErrInfo,nErrorLen, _T("未找到返回结果变量"));
  408. break;
  409. case -2:
  410. StringCchCat(ErrInfo,nErrorLen, _T("未找到 = 符号"));
  411. break;
  412. case -3:
  413. StringCchCat(ErrInfo,nErrorLen, _T("代表没有找到相应常数"));
  414. break;
  415. case -5:
  416. StringCchCat(ErrInfo,nErrorLen, _T("代表找不到字符串的第一个双引号"));
  417. break;
  418. case -6:
  419. StringCchCat(ErrInfo,nErrorLen, _T("代表找不到字符串的第二个双引号"));
  420. break;
  421. case -7:
  422. StringCchCat(ErrInfo,nErrorLen, _T("IF语句没有找到goto字符"));
  423. break;
  424. case -8:
  425. StringCchCat(ErrInfo,nErrorLen, _T("IF语句没有找到第一个比较字符"));
  426. break;
  427. case -9:
  428. StringCchCat(ErrInfo,nErrorLen, _T("IF语句没有找到比较符号"));
  429. break;
  430. case -10:
  431. StringCchCat(ErrInfo,nErrorLen, _T("两边变量类型不相符"));
  432. break;
  433. case -11:
  434. StringCchCat(ErrInfo,nErrorLen, _T("没有找到NOT符号"));
  435. break;
  436. case -12:
  437. StringCchCat(ErrInfo,nErrorLen, _T("不是整形变量"));
  438. break;
  439. case -13:
  440. StringCchCat(ErrInfo,nErrorLen, _T("代表没有找到相应整形常数"));
  441. break;
  442. case -14:
  443. StringCchCat(ErrInfo,nErrorLen, _T("代表没有找到相应字符串常数"));
  444. break;
  445. case -15:
  446. StringCchCat(ErrInfo,nErrorLen, _T("代表没有找到相应浮点常数"));
  447. break;
  448. case -16:
  449. StringCchCat(ErrInfo,nErrorLen, _T("代表不支持这个运算"));
  450. break;
  451. case -17:
  452. StringCchCat(ErrInfo,nErrorLen, _T("代表没有左边括号"));
  453. break;
  454. case -18:
  455. StringCchCat(ErrInfo,nErrorLen, _T("代表没有变量"));
  456. break;
  457. case -19:
  458. StringCchCat(ErrInfo,nErrorLen, _T("代表没“,”"));
  459. break;
  460. case -20:
  461. StringCchCat(ErrInfo,nErrorLen, _T("代表没有右边括号"));
  462. break;
  463. case -21:
  464. StringCchCat(ErrInfo,nErrorLen, _T("代表常数超过指这定的范围"));
  465. break;
  466. case -22:
  467. StringCchCat(ErrInfo,nErrorLen, _T("代表储存器的地址超过指定的范围,整数不能超过EEPROM_LEN-4,浮点不能超过30720-8"));
  468. break;
  469. case -23:
  470. StringCchCat(ErrInfo,nErrorLen, _T("代表储存器的地址超过指定的范围,字符串不能超过EEPROM_LEN-LEN,其中LEN为字符串的长度"));
  471. break;
  472. case -24:
  473. StringCchCat(ErrInfo,nErrorLen, _T("除法中,被除数不能为0"));
  474. break;
  475. case -25:
  476. StringCchCat(ErrInfo,nErrorLen, _T("未知错误"));
  477. break;
  478. case -26:
  479. StringCchCat(ErrInfo,nErrorLen, _T("第二个变量不在指定的位置"));
  480. break;
  481. case -27:
  482. StringCchCat(ErrInfo,nErrorLen, _T("字符串常量超过指定的长度"));
  483. break;
  484. case -28:
  485. StringCchCat(ErrInfo,nErrorLen, _T("不是字符串变量"));
  486. break;
  487. case -29:
  488. StringCchCat(ErrInfo,nErrorLen, _T("没有第三个变量"));
  489. break;
  490. case -30:
  491. StringCchCat(ErrInfo,nErrorLen, _T("GOTO的标识语句不能全为数字"));
  492. break;
  493. case -31:
  494. StringCchCat(ErrInfo,nErrorLen, _T("不能打开ENC文件"));
  495. break;
  496. case -32:
  497. StringCchCat(ErrInfo,nErrorLen, _T("不能读ENC文件"));
  498. break;
  499. case -33:
  500. StringCchCat(ErrInfo,nErrorLen, _T("GOTO CALL不能找到指定的跳转位置"));
  501. break;
  502. case -34:
  503. StringCchCat(ErrInfo,nErrorLen, _T("智能卡运算中,未知数据类型"));
  504. break;
  505. case -35:
  506. StringCchCat(ErrInfo,nErrorLen, _T("智能卡运算中,未知代码类型"));
  507. break;
  508. case -36:
  509. StringCchCat(ErrInfo,nErrorLen, _T("字符串长度超出50"));
  510. break;
  511. case -37:
  512. StringCchCat(ErrInfo,nErrorLen, _T("操作时超长,负长"));
  513. break;
  514. case -38:
  515. StringCchCat(ErrInfo,nErrorLen, _T("标识重复"));
  516. break;
  517. case -39:
  518. StringCchCat(ErrInfo,nErrorLen, _T("程序堆栈溢出"));
  519. break;
  520. case -40:
  521. StringCchCat(ErrInfo,nErrorLen, _T("堆栈溢出"));
  522. break;
  523. case -41:
  524. StringCchCat(ErrInfo,nErrorLen, _T("不能建立编译文件,请查看文件是否有只读属性,或被其它文件打开"));
  525. break;
  526. case -42:
  527. StringCchCat(ErrInfo,nErrorLen, _T("不能写文件,请查看文件是否有只读属性,或被其它文件打开"));
  528. break;
  529. case -43:
  530. StringCchCat(ErrInfo,nErrorLen, _T("程序被中途使用END语句结束"));
  531. break;
  532. case -44:
  533. StringCchCat(ErrInfo,nErrorLen, _T("程序跳转到外部的空间"));
  534. break;
  535. case -45:
  536. StringCchCat(ErrInfo,nErrorLen, _T("传送数据失败"));
  537. break;
  538. case -46:
  539. StringCchCat(ErrInfo,nErrorLen, _T("程序超出运算次数,可能是死循环"));
  540. break;
  541. case -47:
  542. StringCchCat(ErrInfo,nErrorLen, _T("写密码不正确"));
  543. break;
  544. case -48:
  545. StringCchCat(ErrInfo,nErrorLen, _T("读密码不正确"));
  546. break;
  547. case -49:
  548. StringCchCat(ErrInfo,nErrorLen, _T("读写EEPROM时,地址溢出"));
  549. break;
  550. case -50:
  551. StringCchCat(ErrInfo,nErrorLen, _T("USB操作失败,可能是没有找到相关的指令"));
  552. break;
  553. case -51:
  554. StringCchCat(ErrInfo,nErrorLen, _T("打开USB文件句柄失败"));
  555. break;
  556. case -52:
  557. StringCchCat(ErrInfo,nErrorLen, _T("使用加密锁加密自定义表达式,生成加密代码时生产错误"));
  558. break;
  559. case -53:
  560. StringCchCat(ErrInfo,nErrorLen, _T("无法打开usb设备,可能驱动程序没有安装或没有插入加密锁."));
  561. break;
  562. case -63:
  563. StringCchCat(ErrInfo,nErrorLen, _T("不能打开指定的文件"));
  564. break;
  565. case -64:
  566. StringCchCat(ErrInfo,nErrorLen, _T("不能建立指定的文件"));
  567. break;
  568. case -65:
  569. StringCchCat(ErrInfo,nErrorLen, _T(" 验证码错误,可能是输入解密密钥错误,或注册授权码错误"));
  570. break;
  571. case -66:
  572. StringCchCat(ErrInfo,nErrorLen, _T("执行TIMEOUT函数时,输入的ID与锁ID不相符"));
  573. break;
  574. case -67:
  575. StringCchCat(ErrInfo,nErrorLen, _T("执行TIMEOUT函数时,智能卡运行函数已到期"));
  576. break;
  577. case -68:
  578. StringCchCat(ErrInfo,nErrorLen, _T("操作浮点运算时,输入的参数将会导致返回值是一个无穷值"));
  579. break;
  580. case -69:
  581. StringCchCat(ErrInfo,nErrorLen, _T("代表没足够的变量参数"));
  582. break;
  583. case -70:
  584. StringCchCat(ErrInfo,nErrorLen, _T("返回变量与函数不相符"));
  585. break;
  586. case -71:
  587. StringCchCat(ErrInfo,nErrorLen, _T("浮点数转换字符串时,要转换的数据太大"));
  588. break;
  589. case -72:
  590. StringCchCat(ErrInfo,nErrorLen, _T("初始化服务器错误"));
  591. break;
  592. case -73:
  593. StringCchCat(ErrInfo,nErrorLen, _T("对缓冲区进行MD5运算时错误"));
  594. break;
  595. case -74:
  596. StringCchCat(ErrInfo,nErrorLen, _T("MD5验证IPVAR错误"));
  597. break;
  598. case -75:
  599. StringCchCat(ErrInfo,nErrorLen, _T("MD5验证IPCount错误"));
  600. break;
  601. case -76:
  602. StringCchCat(ErrInfo,nErrorLen, _T("没有找到对应的SOCKET连接"));
  603. break;
  604. case -77:
  605. StringCchCat(ErrInfo,nErrorLen, _T("没有找到要删除的对应的SOCKET连接"));
  606. break;
  607. case -78:
  608. StringCchCat(ErrInfo,nErrorLen, _T("没有找到要删除的对应的功能模块号连接"));
  609. break;
  610. case -79:
  611. StringCchCat(ErrInfo,nErrorLen, _T("没有找到要增加的对应的功能模块号连接"));
  612. break;
  613. case -80:
  614. StringCchCat(ErrInfo,nErrorLen, _T("用户数已超过限制的授权数量"));
  615. break;
  616. case -81:
  617. StringCchCat(ErrInfo,nErrorLen, _T("找不到对应的INI文件条目"));
  618. break;
  619. case -82:
  620. StringCchCat(ErrInfo,nErrorLen, _T("没有进行初始化服务工作"));
  621. break;
  622. case -252:
  623. StringCchCat(ErrInfo,nErrorLen, _T("密码不正确"));
  624. break;
  625. case -1088:
  626. StringCchCat(ErrInfo,nErrorLen, _T("发送数据错误"));
  627. break;
  628. case -1089:
  629. StringCchCat(ErrInfo,nErrorLen, _T("获取数据错误"));
  630. break;
  631. case -1092:
  632. StringCchCat(ErrInfo,nErrorLen, _T("找不到对应的服务端操作码"));
  633. break;
  634. case -1093:
  635. StringCchCat(ErrInfo,nErrorLen, _T("表示连接服务时错误"));
  636. break;
  637. case -1095:
  638. StringCchCat(ErrInfo,nErrorLen, _T("获取主机名称失败"));
  639. break;
  640. case -1097:
  641. StringCchCat(ErrInfo,nErrorLen, _T("建立套字接错误"));
  642. break;
  643. case -1098:
  644. StringCchCat(ErrInfo,nErrorLen, _T("绑定套字节端口错误"));
  645. break;
  646. case -1099:
  647. StringCchCat(ErrInfo,nErrorLen, _T("表示无效连接,不能进行相关的操作"));
  648. break;
  649. case -2002:
  650. StringCchCat(ErrInfo,nErrorLen, _T("表示监听时产生错误"));
  651. break;
  652. case -2003:
  653. StringCchCat(ErrInfo,nErrorLen, _T("表示发送的数据长度与接收的数据长度不相符"));
  654. break;
  655. case -2005:
  656. StringCchCat(ErrInfo,nErrorLen, _T("表示当前服务不存在任何连接"));
  657. break;
  658. case -2006:
  659. StringCchCat(ErrInfo,nErrorLen, _T("表示当前查询节点超出集合范范围"));
  660. break;
  661. case -2009:
  662. StringCchCat(ErrInfo,nErrorLen, _T("表示关闭连接错误"));
  663. break;
  664. case -1052:
  665. StringCchCat(ErrInfo,nErrorLen, _T("可能是输入的授权号不正确"));
  666. break;
  667. case -1053:
  668. StringCchCat(ErrInfo,nErrorLen, _T("系统上没有任何智能锁"));
  669. break;
  670. default:
  671. StringCchCat(ErrInfo,nErrorLen, _T("未知错误代码"));
  672. }
  673. }
  674. // Jeff.Hacker. WINDOWS NT 以上的内核需要提权,才能对系统进行高级管理;
  675. BOOL GetDebugPriv()
  676. {
  677. // 返回的访问令牌指针;
  678. HANDLE hToken;
  679. // 接收所返回的制定特权名称的信息;
  680. LUID sedebugnameValue;
  681. // 新特权信息的指针(结构体);
  682. TOKEN_PRIVILEGES tkp;
  683. DWORD dwCurProcId = GetCurrentProcessId();
  684. // 要修改访问权限的进程句柄;
  685. HANDLE hCurProc;
  686. hCurProc = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwCurProcId);
  687. if (!::OpenProcessToken(hCurProc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  688. {
  689. ShowSystemErrorInfo(_T("升级包OpenProcessToken失败."),GetLastError());
  690. return FALSE;
  691. }
  692. if (!::LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &sedebugnameValue))
  693. {
  694. ShowSystemErrorInfo(_T("升级包LookupPrivilegeValue失败."),GetLastError());
  695. CloseHandle(hToken);
  696. return FALSE;
  697. }
  698. tkp.PrivilegeCount = 1;
  699. tkp.Privileges[0].Luid = sedebugnameValue;
  700. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  701. if (!::AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof tkp, NULL, NULL))
  702. {
  703. ShowSystemErrorInfo(_T("升级包AdjustTokenPrivileges失败."),GetLastError());
  704. CloseHandle(hToken);
  705. return FALSE;
  706. }
  707. CloseHandle(hCurProc);
  708. CloseHandle(hToken);
  709. return TRUE;
  710. }
  711. //该版本号是在资源文件中的../Version/VS_VERSION_INFO/FILEVERSION的值
  712. CString GetFileVerionString()
  713. {
  714. TCHAR szPath[200] = { 0 };
  715. TCHAR szVersionBuffer[200] = { 0 };
  716. DWORD dwHandle;
  717. DWORD InfoSize;
  718. CString strVersion;
  719. ::GetModuleFileName(NULL, szPath, sizeof(szPath)); //首先获得版本信息资源的长度
  720. InfoSize = GetFileVersionInfoSize(szPath, &dwHandle); //将版本信息资源读入缓冲区
  721. if (InfoSize == 0) return _T("None VerSion Supprot");
  722. char *InfoBuf = new char[InfoSize];
  723. GetFileVersionInfo(szPath, 0, InfoSize, InfoBuf); //获得生成文件使用的代码页及文件版本
  724. unsigned int cbTranslate = 0;
  725. struct LANGANDCODEPAGE
  726. {
  727. WORD wLanguage;
  728. WORD wCodePage;
  729. } *lpTranslate;
  730. VerQueryValue(InfoBuf, TEXT("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslate, &cbTranslate);
  731. // Read the file description for each language and code page.
  732. for (unsigned int i = 0; i < (cbTranslate / sizeof(LANGANDCODEPAGE)); i++)
  733. {
  734. TCHAR SubBlock[200];
  735. _stprintf_s(SubBlock, _T("\\StringFileInfo\\%04x%04x\\FileVersion"), lpTranslate[i].wLanguage, lpTranslate[i].wCodePage);
  736. void *lpBuffer = NULL;
  737. unsigned int dwBytes = 0;
  738. VerQueryValue(InfoBuf, SubBlock, &lpBuffer, &dwBytes);
  739. CString strTemp = (TCHAR*)lpBuffer;
  740. strVersion += strTemp;
  741. }
  742. if (InfoBuf)
  743. {
  744. delete[]InfoBuf;
  745. }
  746. return strVersion;
  747. }
  748. bool GetFileVersion(HMODULE hModule, WORD *pBuffer)
  749. {
  750. TCHAR fname[MAX_PATH];
  751. VS_FIXEDFILEINFO *pVi;
  752. DWORD dwHandle;
  753. string str;
  754. if (::GetModuleFileName(hModule, fname, MAX_PATH))
  755. {
  756. int size = GetFileVersionInfoSize(fname, &dwHandle);
  757. if (size > 0)
  758. {
  759. BYTE *buffer = new BYTE[size];
  760. memset(buffer, 0, size);
  761. if (GetFileVersionInfo(fname, dwHandle, size, buffer))
  762. {
  763. if (VerQueryValue(buffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&size))
  764. {
  765. pBuffer[0] = HIWORD(pVi->dwFileVersionMS);
  766. pBuffer[1] = LOWORD(pVi->dwFileVersionMS);
  767. pBuffer[2] = HIWORD(pVi->dwFileVersionLS);
  768. pBuffer[3] = LOWORD(pVi->dwFileVersionLS);
  769. delete[]buffer;
  770. return true;
  771. }
  772. }
  773. delete[]buffer;
  774. }
  775. }
  776. return false;
  777. }
  778. string GetFileVersion(HMODULE hModule)
  779. {
  780. string str;
  781. WORD buffer[4];
  782. if (GetFileVersion(hModule, buffer))
  783. {
  784. char str2[32];
  785. for (int i = 0; i < sizeof(buffer) / sizeof(WORD); i++)
  786. {
  787. _itoa_s(buffer[i], str2, 10);
  788. str += str2;
  789. if (i != sizeof(buffer) / sizeof(WORD) - 1)
  790. {
  791. str += ".";
  792. }
  793. }
  794. }
  795. return str;
  796. }
  797. /************************************************************************/
  798. /*
  799. 函数:GetProductVersion
  800. 描述:获取可执行文件的产品版号;
  801. 参数:
  802. hModule[IN] 可执行文件模块句柄;
  803. dwArray[OUT] 返回的产品版本号;
  804. 返回:
  805. 成功返回TRUE,失败返回FALSE;
  806. 注意:
  807. 当hModule为空时,表示要获取的可执行文件为本程序的产品版本号;
  808. */
  809. /************************************************************************/
  810. BOOL GetProductVersion(IN HMODULE hModule, IN DWORD (&dwArray)[4] )
  811. {
  812. TCHAR fname[MAX_PATH];
  813. VS_FIXEDFILEINFO *pVi;
  814. DWORD dwHandle;
  815. if (::GetModuleFileName(hModule, fname, MAX_PATH))
  816. {
  817. INT nSize = GetFileVersionInfoSize(fname, &dwHandle);
  818. if (nSize > 0)
  819. {
  820. BYTE *pBuffer = new BYTE[nSize];
  821. memset(pBuffer, 0, nSize);
  822. if (GetFileVersionInfo(fname, dwHandle, nSize, pBuffer))
  823. {
  824. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  825. {
  826. dwArray[0] = HIWORD(pVi->dwProductVersionMS);
  827. dwArray[1] = LOWORD(pVi->dwProductVersionMS);
  828. dwArray[2] = HIWORD(pVi->dwProductVersionLS);
  829. dwArray[3] = LOWORD(pVi->dwProductVersionLS);
  830. if(pBuffer)
  831. delete[]pBuffer;
  832. return TRUE;
  833. }
  834. }
  835. if(pBuffer)
  836. delete[]pBuffer;
  837. }
  838. }
  839. return FALSE;
  840. }
  841. /************************************************************************/
  842. /*
  843. 函数:GetProductVersion
  844. 描述:获取可执行文件的产品版号;
  845. 参数:
  846. lpFileName[IN] 可执行文件名全路径;
  847. dwArray[OUT] 返回的产品版本号;
  848. 返回:
  849. 成功返回TRUE,失败返回FALSE;
  850. 注意:
  851. */
  852. /************************************************************************/
  853. BOOL GetProductVersionEx( IN LPCTSTR lpFileName, IN DWORD (&dwArray)[4] )
  854. {
  855. if ( lpFileName == NULL || !PathFileExists(lpFileName) )
  856. {
  857. OutputDebugString(_T("文件名错误或文件不存在\n"));
  858. return FALSE;
  859. }
  860. DWORD dwHandle = 0;
  861. VS_FIXEDFILEINFO *pVi = NULL;
  862. INT nSize = GetFileVersionInfoSize(lpFileName, &dwHandle);
  863. if ( nSize > 0 )
  864. {
  865. BYTE *pBuffer = new BYTE[nSize];
  866. memset(pBuffer, 0, nSize);
  867. if ( GetFileVersionInfo(lpFileName, dwHandle, nSize, pBuffer) )
  868. {
  869. if (VerQueryValue(pBuffer, _T("\\"), (LPVOID *)&pVi, (PUINT)&nSize))
  870. {
  871. dwArray[0] = HIWORD(pVi->dwProductVersionMS);
  872. dwArray[1] = LOWORD(pVi->dwProductVersionMS);
  873. dwArray[2] = HIWORD(pVi->dwProductVersionLS);
  874. dwArray[3] = LOWORD(pVi->dwProductVersionLS);
  875. if (pBuffer)
  876. delete[]pBuffer;
  877. return TRUE;
  878. }
  879. }
  880. if (pBuffer)
  881. delete[]pBuffer;
  882. }
  883. return FALSE;
  884. }
  885. BOOL RegisterOcx(LPCTSTR OcxFileName)
  886. {
  887. LPCTSTR pszDllName = OcxFileName; // ActiveX控件的路径及文件名
  888. HINSTANCE hLib = LoadLibrary(pszDllName); // 装载ActiveX控件
  889. //HINSTANCE hLib = LoadLibraryEx(pszDllName,NULL,LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE );
  890. //LOAD_LIBRARY_AS_DATAFILE|LOAD_WITH_ALTERED_SEARCH_PATH|DONT_RESOLVE_DLL_REFERENCES 装载ActiveX控件
  891. TRACE("----------------");
  892. if (hLib < (HINSTANCE)HINSTANCE_ERROR)
  893. {
  894. return FALSE;
  895. }
  896. // FARPROC lpDllGetClass;
  897. // lpDllGetClass = GetProcAddress(hLib,_T("DllGetClassObject")); //获取注册函数DllRegisterServer地址
  898. //
  899. // if(lpDllGetClass != NULL) //调用注册函数DllRegisterServer
  900. // {
  901. // if(FAILED((*lpDllGetClass)()))
  902. // {
  903. // FreeLibrary(hLib);
  904. // return FALSE ;
  905. // }
  906. // return TRUE ;
  907. // }
  908. // else
  909. // return FALSE ;
  910. FARPROC lpDllEntryPoint;
  911. lpDllEntryPoint = GetProcAddress(hLib, "DllRegisterServer"); //获取注册函数DllRegisterServer地址
  912. //typedef int (CALLBACK* myfunction)(void);
  913. if (lpDllEntryPoint != NULL) //调用注册函数DllRegisterServer
  914. {
  915. if (FAILED((*lpDllEntryPoint)()))
  916. {
  917. FreeLibrary(hLib);
  918. return FALSE;
  919. }
  920. return TRUE;
  921. }
  922. else
  923. return FALSE;
  924. }
  925. BOOL UnRegisterOcx(LPCTSTR OcxFileName)
  926. {
  927. LPCTSTR pszDllName = OcxFileName; //ActiveX控件的路径及文件名
  928. HINSTANCE hLib = LoadLibrary(pszDllName); //装载ActiveX控件
  929. if (hLib < (HINSTANCE)HINSTANCE_ERROR)
  930. {
  931. return FALSE;
  932. }
  933. FARPROC lpDllEntryPoint;
  934. lpDllEntryPoint = GetProcAddress(hLib, "DllUnregisterServer"); //获取注册函数DllUnregisterServer地址
  935. if (lpDllEntryPoint != NULL) //调用注册函数DllUnregisterServer
  936. {
  937. if (FAILED((*lpDllEntryPoint)()))
  938. {
  939. FreeLibrary(hLib);
  940. return FALSE;
  941. }
  942. return TRUE;
  943. }
  944. else
  945. return FALSE;
  946. }
  947. BOOL IsReg() // 没多大意义,只针对已知控件的CLSID才行; 不能通用;
  948. {
  949. HKEY hKey;
  950. BOOL bPresent;
  951. TCHAR szPath[_MAX_PATH];
  952. DWORD dwRegType;
  953. DWORD cbData = sizeof szPath * sizeof TCHAR;
  954. hKey = NULL;
  955. bPresent = FALSE;
  956. ::RegOpenKey(HKEY_CLASSES_ROOT, _T("CLSID\\{4CAD92F0-D7C4-11D0-BCF7-00C04FC2FB86}\\InprocServer32"), &hKey);
  957. if (hKey)
  958. {
  959. HANDLE hfile;
  960. szPath[0] = 0;
  961. ::RegQueryValueEx(hKey, NULL, NULL, &dwRegType, (LPBYTE)szPath, &cbData);
  962. ::RegCloseKey(hKey);
  963. hfile = ::CreateFile(szPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  964. if (INVALID_HANDLE_VALUE != hfile)
  965. {
  966. bPresent = TRUE;
  967. ::CloseHandle(hfile);
  968. }
  969. }
  970. return bPresent;
  971. }
  972. BOOL IsRegistOcx() // 没多大意义,只针对已知控件的CLSID才行;
  973. {
  974. HKEY hKey;
  975. //BOOL bPresent;
  976. TCHAR szPath[_MAX_PATH];
  977. //DWORD dwRegType;
  978. DWORD cbData = sizeof szPath * sizeof TCHAR;
  979. if (RegOpenKeyEx(HKEY_CLASSES_ROOT, _T("ActiveX.ActiveXControl//Clsid"), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
  980. return false;
  981. else
  982. return true;
  983. //ActiveX.ActiveXControl为控件的名称和内部名称 如"NTGRAPH.NTGraphCtrl.1//CLSID"
  984. }
  985. /************************************************************************/
  986. /*
  987. 目标:HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES\WOW6432NODE\CLSID\{AC5D0DDE-BD4C-11D1-B137-0000F8753F5D}\INPROCSERVER32\[]
  988. 目标:HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES\WOW6432NODE\CLSID\{3A2B370C-BA0A-11D1-B137-0000F8753F5D}\INPROCSERVER32\[]
  989. */
  990. /************************************************************************/
  991. bool RegOcx(CString ocxfile)
  992. {
  993. HKEY hKey;
  994. bool bRet;
  995. USES_CONVERSION;
  996. ITypeLib* pTypeLib;
  997. CString CLSIDStr;
  998. CLSID clsid;
  999. LPOLESTR wRes = NULL;
  1000. #ifndef UNICODE
  1001. if (LoadTypeLib(T2OLE(ocxfile), &pTypeLib) == S_OK)
  1002. #else
  1003. if (LoadTypeLib(ocxfile, &pTypeLib) == S_OK)
  1004. #endif
  1005. {
  1006. TLIBATTR tlibAttr, *ptlibAttr;
  1007. ptlibAttr = &tlibAttr;
  1008. pTypeLib->GetLibAttr(&ptlibAttr);
  1009. clsid = ptlibAttr->guid;
  1010. pTypeLib->Release();
  1011. }
  1012. ::StringFromCLSID(clsid, &wRes);
  1013. CLSIDStr = OLE2CT(wRes);
  1014. #if 0 // Jeff. 未完……
  1015. // 注册表的位置与操作系统版本有关;
  1016. // 用SYSTEM_INFO结构判断64位AMD处理器;
  1017. SYSTEM_INFO info;
  1018. // 调用GetSystemInfo函数填充结构;
  1019. GetSystemInfo(&info);
  1020. OSVERSIONINFOEX os;
  1021. // 在调用函数前必须用sizeof(OSVERSIONINFOEXA)填充dwOSVersionInfoSize结构成员;
  1022. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  1023. if (GetVersionEx((OSVERSIONINFO *)&os))
  1024. {
  1025. if (os.dwMajorVersion <= 5)
  1026. {
  1027. }
  1028. else if (os.dwMajorVersion >= 6)
  1029. {
  1030. }
  1031. }
  1032. #else
  1033. //CLSIDStr.Format("{%s}",OLE2CT(wRes));
  1034. //if(RegOpenKeyEx(HKEY_CLASSES_ROOT,"CLSID\\"+CLSIDStr,0,KEY_ALL_ACCESS,&hKey)==ERROR_SUCCESS)
  1035. //if( ::RegOpenKeyEx(HKEY_CLASSES_ROOT,"CLSID\\"+CLSIDStr,0,KEY_READ,&hKey) == ERROR_SUCCESS) // 只适合xp的;
  1036. if (::RegOpenKeyEx(HKEY_CLASSES_ROOT, CString(_T("Wow6432Node\\TypeLib\\")) + CLSIDStr, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
  1037. {
  1038. return true;
  1039. }
  1040. #endif
  1041. else
  1042. {
  1043. HINSTANCE hLib = LoadLibrary(ocxfile);
  1044. if (hLib < (HINSTANCE)HINSTANCE_ERROR)
  1045. {
  1046. MessageBox(0, _T("不能载入OCX文件!无法注册控件!"), _T("结果"), MB_OK);
  1047. bRet = false;
  1048. }
  1049. else
  1050. {
  1051. FARPROC lpDllEntryPoint;
  1052. lpDllEntryPoint = GetProcAddress(hLib, "DllRegisterServer");
  1053. if (lpDllEntryPoint != NULL)
  1054. {
  1055. if (FAILED((*lpDllEntryPoint)()))
  1056. {
  1057. MessageBox(0, _T("调用注册函数(DllRegisterServer)失败!无法注册控件!"), _T("结果"), MB_OK);
  1058. bRet = false;
  1059. }
  1060. else
  1061. bRet = true;
  1062. }
  1063. else
  1064. {
  1065. MessageBox(0, _T("调用注册函数(DllRegisterServer)失败!无法注册控件!"), _T("结果"), MB_OK);
  1066. bRet = false;
  1067. }
  1068. }
  1069. FreeLibrary(hLib);
  1070. return bRet;
  1071. }
  1072. }
  1073. //------------------------------------------------------------------------------------
  1074. /*
  1075. // 函数原型:
  1076. // BOOL GetVersionEx(POSVERSIONINFO pVersionInformation);
  1077. // 我们先来看看OSVERSIONINFOEX这个结构:
  1078. typedef struct
  1079. {
  1080. DWORD dwOSVersionInfoSize; //在使用GetVersionEx之前要将此初始化为结构的大小
  1081. DWORD dwMajorVersion; //系统主版本号
  1082. DWORD dwMinorVersion; //系统次版本号
  1083. DWORD dwBuildNumber; //系统构建号
  1084. DWORD dwPlatformId; //系统支持的平台(详见附1)
  1085. TCHAR szCSDVersion[128]; //系统补丁包的名称
  1086. WORD wServicePackMajor; //系统补丁包的主版本
  1087. WORD wServicePackMinor; //系统补丁包的次版本
  1088. WORD wSuiteMask; //标识系统上的程序组(详见附2)
  1089. BYTE wProductType; //标识系统类型(详见附3)
  1090. BYTE wReserved; //保留,未使用
  1091. } OSVERSIONINFOEX, *POSVERSIONINFOEX;
  1092. 这个结构在Windows 2000后出现,老版本的OSVERSIONINFO结构没有wServicePackMajor、wServicePackMinor、wSuiteMask、wProductType和wReserved这几个成员。
  1093. 接着在调用函数之前先初始化结构的大小:
  1094. OSVERSIONINFOEX os;
  1095. os.dwOSVersionInfoSize=sizeof(os);
  1096. 函数返回值为TRUE表示成功:
  1097. if(!GetVersionEx((OSVERSIONINFO *)&os))
  1098. {
  1099. return FALSE;
  1100. }
  1101. 函数调用成功以后就可以通过OSVERSIONINFOEX来查看系统的版本信息了。
  1102. 下面是一个通过系统版本来判断操作系统名称的例子:
  1103. */
  1104. CString getSystemName()
  1105. {
  1106. SYSTEM_INFO info; // 用SYSTEM_INFO结构判断64位AMD处理器
  1107. GetSystemInfo(&info); // 调用GetSystemInfo函数填充结构
  1108. OSVERSIONINFOEX os;
  1109. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); /*在调用函数前必须用sizeof(OSVERSIONINFOEXA)填充dwOSVersionInfoSize结构成员*/
  1110. if (GetVersionEx((OSVERSIONINFO *)&os)) /*调用GetVersionEx函数OSVERSIONINFOEX结构必须将指针类型强制转换*/
  1111. {
  1112. CString vname;
  1113. //下面根据版本信息判断操作系统名称;
  1114. switch (os.dwMajorVersion) //判断主版本号 ;
  1115. {
  1116. case 4:
  1117. switch (os.dwMinorVersion) //判断次版本号 ;
  1118. {
  1119. case 0:
  1120. if (os.dwPlatformId == VER_PLATFORM_WIN32_NT)
  1121. vname = _T("Microsoft Windows NT 4.0"); //1996年7月发布
  1122. else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  1123. vname = _T("Microsoft Windows 95");
  1124. break;
  1125. case 10:
  1126. vname = _T("Microsoft Windows 98");
  1127. break;
  1128. case 90:
  1129. vname = _T("Microsoft Windows Me");
  1130. break;
  1131. }
  1132. break;
  1133. case 5:
  1134. switch (os.dwMinorVersion) //再比较dwMinorVersion的值
  1135. {
  1136. case 0:
  1137. vname = _T("Microsoft Windows 2000"); //1999年12月发布
  1138. break;
  1139. case 1:
  1140. vname = _T("Microsoft Windows XP"); //2001年8月发布
  1141. break;
  1142. case 2:
  1143. if (os.wProductType == VER_NT_WORKSTATION && info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1144. vname = _T("Microsoft Windows XP Professional x64 Edition");
  1145. else if (GetSystemMetrics(SM_SERVERR2) == 0)
  1146. vname = _T("Microsoft Windows Server 2003"); //2003年3月发布
  1147. else if (GetSystemMetrics(SM_SERVERR2) != 0)
  1148. vname = _T("Microsoft Windows Server 2003 R2");
  1149. break;
  1150. }
  1151. break;
  1152. case 6:
  1153. switch (os.dwMinorVersion)
  1154. {
  1155. case 0:
  1156. if (os.wProductType == VER_NT_WORKSTATION) /*VER_NT_WORKSTATION是桌面系统 */
  1157. vname = _T("Microsoft Windows Vista");
  1158. else
  1159. vname = _T("Microsoft Windows Server 2008"); //服务器版本
  1160. break;
  1161. case 1:
  1162. if (os.wProductType == VER_NT_WORKSTATION)
  1163. vname = _T("Microsoft Windows 7");
  1164. else
  1165. vname = _T("Microsoft Windows Server 2008 R2");
  1166. break;
  1167. }
  1168. break;
  1169. default:
  1170. vname = _T("未知操作系统");
  1171. }
  1172. return vname;
  1173. }
  1174. else
  1175. return _T("");
  1176. }
  1177. /*
  1178. 以上只能判断操作系统的名称,那如何判断它是什么版本的呢(如Home版或Professional版)。
  1179. 下面这个函数就返回具体的版本名称:
  1180. */
  1181. CString getVersionMark()
  1182. {
  1183. OSVERSIONINFOEX os;
  1184. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  1185. if (GetVersionEx((OSVERSIONINFO *)&os))
  1186. {
  1187. CString vmark;
  1188. switch (os.dwMajorVersion) //先判断操作系统版本
  1189. {
  1190. case 5:
  1191. switch (os.dwMinorVersion)
  1192. {
  1193. case 0: //Windows 2000
  1194. if (os.wSuiteMask == VER_SUITE_ENTERPRISE)
  1195. vmark = _T("Advanced Server");
  1196. break;
  1197. case 1: //Windows XP
  1198. if (os.wSuiteMask == VER_SUITE_EMBEDDEDNT)
  1199. vmark = _T("Embedded");
  1200. else if (os.wSuiteMask == VER_SUITE_PERSONAL)
  1201. vmark = _T("Home Edition");
  1202. else
  1203. vmark = _T("Professional");
  1204. break;
  1205. case 2:
  1206. if (GetSystemMetrics(SM_SERVERR2) == 0 && os.wSuiteMask == VER_SUITE_BLADE) //Windows Server 2003
  1207. vmark = _T("Web Edition");
  1208. else if (GetSystemMetrics(SM_SERVERR2) == 0 && os.wSuiteMask == VER_SUITE_COMPUTE_SERVER)
  1209. vmark = _T("Compute Cluster Edition");
  1210. else if (GetSystemMetrics(SM_SERVERR2) == 0 && os.wSuiteMask == VER_SUITE_STORAGE_SERVER)
  1211. vmark = _T("Storage Server");
  1212. else if (GetSystemMetrics(SM_SERVERR2) == 0 && os.wSuiteMask == VER_SUITE_DATACENTER)
  1213. vmark = _T("Datacenter Edition");
  1214. else if (GetSystemMetrics(SM_SERVERR2) == 0 && os.wSuiteMask == VER_SUITE_ENTERPRISE)
  1215. vmark = _T("Enterprise Edition");
  1216. else if (GetSystemMetrics(SM_SERVERR2) != 0 && os.wSuiteMask == VER_SUITE_STORAGE_SERVER) /*Windows Server 2003 R2*/
  1217. vmark = _T("Storage Server");
  1218. break;
  1219. }
  1220. break;
  1221. case 6:
  1222. switch (os.dwMinorVersion)
  1223. {
  1224. case 0:
  1225. if (os.wProductType != VER_NT_WORKSTATION && os.wSuiteMask == VER_SUITE_DATACENTER) /*Windows Server 2008*/
  1226. vmark = _T("Datacenter Server");
  1227. else if (os.wProductType != VER_NT_WORKSTATION && os.wSuiteMask == VER_SUITE_ENTERPRISE)
  1228. vmark = _T("Enterprise");
  1229. else if (os.wProductType == VER_NT_WORKSTATION && os.wSuiteMask == VER_SUITE_PERSONAL) //Windows Vista
  1230. vmark = _T("Home");
  1231. break;
  1232. }
  1233. break;
  1234. default:
  1235. vmark = _T("");
  1236. }
  1237. return vmark;
  1238. }
  1239. else
  1240. return _T("");
  1241. }
  1242. /*
  1243. 另外,操作系统的补丁包名称也可以从szCSDVersion直接获取。
  1244. 程序编译出来后大家可以用不同版本的兼容模式运行这个程序,
  1245. 来检测结果是不是正确,兼容模式会欺骗GetVersionEx这些API函数,拿Windows 98 兼容模式来说,程序会以为自己是在Windows 98 上运行。
  1246. 有什么问题请给我留言,如果在本页面下发没有看到有留言请刷新一下页面。
  1247. 附1:dwPlatformId的值:
  1248. VER_PLATFORM_WIN32s (Win32s),
  1249. VER_PLATFORM_WIN32_WINDOWS (Windows 95/Windows 98),
  1250. VER_PLATFORM_WIN32_NT (Windows NT/Windows 2000),
  1251. VER_PLATFORM_WIN32_CEHH (Windows CE).
  1252. 附2:wSuiteMask的值:
  1253. VER_SUITE_SMALLBUSINESS,
  1254. VER_SUITE_ENTERPRISE,
  1255. VER_SUITE_BACKOFFICE,
  1256. VER_SUITE_COMMUNICATIONS,
  1257. VER_SUITE_TERMINAL,
  1258. VER_SUITE_SMALLBUSINESS_RESTRICTED,
  1259. VER_SUITE_EMBEDDEDNT,
  1260. VER_SUITE_DATACENTER.
  1261. 附3:wProductType的值:
  1262. VER_NT_WORKSTATION,
  1263. VER_NT_SERVER,
  1264. VER_NT_DOMAIN_CONTROLLER.
  1265. */
  1266. //---------------------------------------------------------------------
  1267. BOOL GetOSDisplayString(LPTSTR pszOS)
  1268. {
  1269. OSVERSIONINFOEX osvi;
  1270. SYSTEM_INFO si;
  1271. PGNSI pGNSI;
  1272. PGPI pGPI;
  1273. BOOL bOsVersionInfoEx;
  1274. DWORD dwType;
  1275. ZeroMemory(&si, sizeof(SYSTEM_INFO));
  1276. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
  1277. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  1278. if (!(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *)&osvi)))
  1279. return 1;
  1280. // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
  1281. pGNSI = (PGNSI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
  1282. if (NULL != pGNSI)
  1283. pGNSI(&si);
  1284. else GetSystemInfo(&si);
  1285. if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4)
  1286. {
  1287. StringCchCopy(pszOS, BUFSIZE, TEXT("Microsoft "));
  1288. // Test for the specific product.
  1289. if (osvi.dwMajorVersion == 6)
  1290. {
  1291. if (osvi.dwMinorVersion == 0)
  1292. {
  1293. if (osvi.wProductType == VER_NT_WORKSTATION)
  1294. StringCchCat(pszOS, BUFSIZE, TEXT("Windows Vista "));
  1295. else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 "));
  1296. }
  1297. if (osvi.dwMinorVersion == 1)
  1298. {
  1299. if (osvi.wProductType == VER_NT_WORKSTATION)
  1300. StringCchCat(pszOS, BUFSIZE, TEXT("Windows 7 "));
  1301. else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 R2 "));
  1302. }
  1303. pGPI = (PGPI)GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
  1304. pGPI(osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);
  1305. switch (dwType)
  1306. {
  1307. case PRODUCT_ULTIMATE:
  1308. StringCchCat(pszOS, BUFSIZE, TEXT("Ultimate Edition"));
  1309. break;
  1310. case PRODUCT_HOME_PREMIUM:
  1311. StringCchCat(pszOS, BUFSIZE, TEXT("Home Premium Edition"));
  1312. break;
  1313. case PRODUCT_HOME_BASIC:
  1314. StringCchCat(pszOS, BUFSIZE, TEXT("Home Basic Edition"));
  1315. break;
  1316. case PRODUCT_ENTERPRISE:
  1317. StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition"));
  1318. break;
  1319. case PRODUCT_BUSINESS:
  1320. StringCchCat(pszOS, BUFSIZE, TEXT("Business Edition"));
  1321. break;
  1322. case PRODUCT_STARTER:
  1323. StringCchCat(pszOS, BUFSIZE, TEXT("Starter Edition"));
  1324. break;
  1325. case PRODUCT_CLUSTER_SERVER:
  1326. StringCchCat(pszOS, BUFSIZE, TEXT("Cluster Server Edition"));
  1327. break;
  1328. case PRODUCT_DATACENTER_SERVER:
  1329. StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition"));
  1330. break;
  1331. case PRODUCT_DATACENTER_SERVER_CORE:
  1332. StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition (core installation)"));
  1333. break;
  1334. case PRODUCT_ENTERPRISE_SERVER:
  1335. StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition"));
  1336. break;
  1337. case PRODUCT_ENTERPRISE_SERVER_CORE:
  1338. StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition (core installation)"));
  1339. break;
  1340. case PRODUCT_ENTERPRISE_SERVER_IA64:
  1341. StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
  1342. break;
  1343. case PRODUCT_SMALLBUSINESS_SERVER:
  1344. StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server"));
  1345. break;
  1346. case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
  1347. StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server Premium Edition"));
  1348. break;
  1349. case PRODUCT_STANDARD_SERVER:
  1350. StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition"));
  1351. break;
  1352. case PRODUCT_STANDARD_SERVER_CORE:
  1353. StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition (core installation)"));
  1354. break;
  1355. case PRODUCT_WEB_SERVER:
  1356. StringCchCat(pszOS, BUFSIZE, TEXT("Web Server Edition"));
  1357. break;
  1358. }
  1359. }
  1360. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
  1361. {
  1362. if (GetSystemMetrics(SM_SERVERR2))
  1363. StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003 R2, "));
  1364. else if (osvi.wSuiteMask == VER_SUITE_STORAGE_SERVER)
  1365. StringCchCat(pszOS, BUFSIZE, TEXT("Windows Storage Server 2003"));
  1366. else if (osvi.wSuiteMask == VER_SUITE_WH_SERVER)
  1367. StringCchCat(pszOS, BUFSIZE, TEXT("Windows Home Server"));
  1368. else if (osvi.wProductType == VER_NT_WORKSTATION &&
  1369. si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1370. {
  1371. StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP Professional x64 Edition"));
  1372. }
  1373. else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003, "));
  1374. // Test for the server type.
  1375. if (osvi.wProductType != VER_NT_WORKSTATION)
  1376. {
  1377. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
  1378. {
  1379. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1380. StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition for Itanium-based Systems"));
  1381. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1382. StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems"));
  1383. }
  1384. else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1385. {
  1386. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1387. StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter x64 Edition"));
  1388. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1389. StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise x64 Edition"));
  1390. else StringCchCat(pszOS, BUFSIZE, TEXT("Standard x64 Edition"));
  1391. }
  1392. else
  1393. {
  1394. if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
  1395. StringCchCat(pszOS, BUFSIZE, TEXT("Compute Cluster Edition"));
  1396. else if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1397. StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition"));
  1398. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1399. StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition"));
  1400. else if (osvi.wSuiteMask & VER_SUITE_BLADE)
  1401. StringCchCat(pszOS, BUFSIZE, TEXT("Web Edition"));
  1402. else StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition"));
  1403. }
  1404. }
  1405. }
  1406. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
  1407. {
  1408. StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP "));
  1409. if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
  1410. StringCchCat(pszOS, BUFSIZE, TEXT("Home Edition"));
  1411. else StringCchCat(pszOS, BUFSIZE, TEXT("Professional"));
  1412. }
  1413. if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
  1414. {
  1415. StringCchCat(pszOS, BUFSIZE, TEXT("Windows 2000 "));
  1416. if (osvi.wProductType == VER_NT_WORKSTATION)
  1417. {
  1418. StringCchCat(pszOS, BUFSIZE, TEXT("Professional"));
  1419. }
  1420. else
  1421. {
  1422. if (osvi.wSuiteMask & VER_SUITE_DATACENTER)
  1423. StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Server"));
  1424. else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
  1425. StringCchCat(pszOS, BUFSIZE, TEXT("Advanced Server"));
  1426. else StringCchCat(pszOS, BUFSIZE, TEXT("Server"));
  1427. }
  1428. }
  1429. // Include service pack (if any) and build number.
  1430. if (_tcslen(osvi.szCSDVersion) > 0)
  1431. {
  1432. StringCchCat(pszOS, BUFSIZE, TEXT(" "));
  1433. StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion);
  1434. }
  1435. TCHAR buf[80];
  1436. StringCchPrintf(buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
  1437. StringCchCat(pszOS, BUFSIZE, buf);
  1438. if (osvi.dwMajorVersion >= 6)
  1439. {
  1440. if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
  1441. StringCchCat(pszOS, BUFSIZE, TEXT(", 64-bit"));
  1442. else if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
  1443. StringCchCat(pszOS, BUFSIZE, TEXT(", 32-bit"));
  1444. }
  1445. return TRUE;
  1446. }
  1447. else
  1448. {
  1449. _tprintf(_T("This sample does not support this version of Windows.\n"));
  1450. return FALSE;
  1451. }
  1452. }
  1453. void WriteLog(const CString &str) // 写日志;
  1454. {
  1455. //SecurityKeylock lock(_T("lyfzUpgrade"));
  1456. try
  1457. {
  1458. CStdioFile fp;
  1459. CString path = CString(g_ModulePath) + _T("升级包日志.txt");
  1460. if (PathFileExists(path))
  1461. {
  1462. fp.Open(path, CFile::modeWrite);
  1463. ULONGLONG length = fp.GetLength();
  1464. if (length > 5 * 1024 * 1024)
  1465. {
  1466. fp.Close();
  1467. ::DeleteFile(path);
  1468. return;
  1469. }
  1470. fp.SeekToEnd();
  1471. }
  1472. else
  1473. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  1474. #ifdef UNICODE
  1475. char* old_locale = _strdup(setlocale(LC_CTYPE, NULL));
  1476. setlocale(LC_CTYPE, "chs");//设定
  1477. // WriteString在UNICODE下无法写中文,需要setlocale语言区域;
  1478. fp.WriteString((CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S")) + _T(" ") + str + _T("\n")));
  1479. setlocale(LC_CTYPE, old_locale);
  1480. free(old_locale);//还原区域设定
  1481. #else
  1482. fp.WriteString((CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S")) + _T(" ") + str + _T("\n")));
  1483. #endif
  1484. fp.Close();
  1485. }
  1486. catch (...)
  1487. {
  1488. }
  1489. }
  1490. //---------------------------------------------------------------------
  1491. // add by Jeff 2014.10.24
  1492. // 函数:全局函数lyfzCopyFileEx
  1493. // 描述:
  1494. // 参数:
  1495. // lpExistingFileName:你要拷贝的源文件名;
  1496. // lpNewFileName: 你要拷贝的目标文件名;
  1497. // bFailIfExists: True:如果目标已经存在,不覆盖目标(True)并重命名; FALSE:如果目标已经存在,覆盖目标
  1498. // 返回:
  1499. //---------------------------------------------------------------------
  1500. BOOL lyfzCopyFileEx(LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, const BOOL &bFailIfExists)
  1501. {
  1502. // 1.如果是覆盖选项;
  1503. if (!bFailIfExists)
  1504. return CopyFile(lpExistingFileName, lpNewFileName, bFailIfExists);
  1505. // 2.如果不是覆盖选项;
  1506. TCHAR szNewFileName[_MAX_PATH] = { 0 };
  1507. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  1508. TCHAR szDir[_MAX_DIR] = { 0 };
  1509. TCHAR szFna[_MAX_FNAME] = { 0 };
  1510. TCHAR szExt[_MAX_EXT] = { 0 };
  1511. _stprintf_s(szNewFileName, _T("%s"), lpNewFileName);
  1512. _tsplitpath_s(szNewFileName, szDrive, szDir, szFna, szExt);
  1513. int nIndex = 1;
  1514. while (PathFileExists(szNewFileName))
  1515. {
  1516. _stprintf_s(szNewFileName, _T("%s%s%s (%d)%s"), szDrive, szDir, szFna, nIndex++, szExt);
  1517. }
  1518. return CopyFile(lpExistingFileName, szNewFileName, bFailIfExists);
  1519. }
  1520. #if 0
  1521. //---------------------------------------------------------------------
  1522. // add by Jeff 2014.10.25
  1523. // 函数:全局函数lyfzCreateDirectory
  1524. // 描述:创建目录及子目录文件夹;
  1525. // 参数:
  1526. // lpDestDirectory:目录,必须是"E:\lyfzdb\数据\"格式,目录最后必须有"\"
  1527. // 建议使用系统_tsplitpath来分隔目录,这样会保存最后一个"\"斜杠;
  1528. // 返回:成功创建返回TRUE;
  1529. //---------------------------------------------------------------------
  1530. BOOL lyfzCreateDirectory(LPCTSTR lpDestDirectory)
  1531. {
  1532. BOOL bExists = FALSE;
  1533. TCHAR szNewFileName[_MAX_PATH] = { 0 };
  1534. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  1535. TCHAR szDir[_MAX_DIR] = { 0 };
  1536. TCHAR szFna[_MAX_FNAME] = { 0 };
  1537. TCHAR szExt[_MAX_EXT] = { 0 };
  1538. int nIndex = 0;
  1539. //sprintf(szNewFileName, "%s", lpDestDirectory);
  1540. do
  1541. {
  1542. bExists = PathFileExists(lpDestDirectory);
  1543. if (!bExists)
  1544. {
  1545. memset(szDrive, 0, _MAX_DRIVE*sizeof(TCHAR));
  1546. memset(szDir, 0, _MAX_DIR*sizeof(TCHAR));
  1547. memset(szFna, 0, _MAX_FNAME*sizeof(TCHAR));
  1548. memset(szExt, 0, _MAX_EXT*sizeof(TCHAR));
  1549. tSplitpath(lpDestDirectory, szDrive, szDir, ++nIndex);
  1550. swprintf_s(szNewFileName, L"%s%s", szDrive, szDir);
  1551. if (FALSE == CreateDirectory(szNewFileName, NULL))
  1552. {
  1553. return FALSE;
  1554. }
  1555. }
  1556. } while (!bExists);
  1557. return TRUE;
  1558. }
  1559. #endif
  1560. void tSplitpath(const char *path, char *drive, char *dir, const int &nTimes)
  1561. {
  1562. int nlen = strlen(path);
  1563. int i = 0;
  1564. int npoint = 0; // '.'
  1565. int nsprit = 0; // '\\'
  1566. int ncolon = 0; // ':'
  1567. int ntimes = -1;
  1568. while (nlen > i++)
  1569. {
  1570. if (path[i] == ':')
  1571. ncolon = i;
  1572. else if (path[i] == '\\')
  1573. {
  1574. if (nTimes == ntimes++)
  1575. break;
  1576. nsprit = i;
  1577. }
  1578. }
  1579. memcpy(drive, path, ncolon + 1);
  1580. memcpy(dir, &path[ncolon + 1], nsprit - ncolon);
  1581. }
  1582. void tSplitpath(const char *path, char *drive, char *dir, char *fname, char *ext)
  1583. {
  1584. #if 0 // Jeff:从尾部开始;
  1585. int nlen = strlen(path);
  1586. int i = nlen;
  1587. int npoint = 0; // '.'
  1588. int nsprit = 0; // '\\'
  1589. int ncolon = 0; // ':'
  1590. while (i--)
  1591. {
  1592. if (path[nlen - 1] == '\\' && i == nlen - 1)
  1593. {
  1594. continue;
  1595. }
  1596. if (path[i] == '.' && !npoint)
  1597. {
  1598. npoint = i;
  1599. memcpy(ext, &path[i], nlen - i);
  1600. }
  1601. else if (path[i] == '\\' && !nsprit)
  1602. {
  1603. nsprit = i;
  1604. memcpy(fname, &path[i + 1], abs(npoint - i));
  1605. }
  1606. else if (path[i] == ':' && !ncolon)
  1607. {
  1608. ncolon = i;
  1609. memcpy(dir, &path[i + 1], nsprit - i);
  1610. break;
  1611. }
  1612. }
  1613. memcpy(drive, path, i + 1);
  1614. #else // 从头部开始
  1615. int nlen = strlen(path);
  1616. int i = 0;
  1617. int npoint = 0; // '.'
  1618. int nsprit = 0; // '\\'
  1619. int ncolon = 0; // ':'
  1620. while (nlen > i++)
  1621. {
  1622. if (path[i] == ':')
  1623. ncolon = i;
  1624. else if (path[i] == '\\')
  1625. nsprit = i;
  1626. else if (path[i] == '.')
  1627. npoint = i;
  1628. }
  1629. memcpy(drive, path, ncolon + 1);
  1630. memcpy(dir, &path[ncolon + 1], nsprit - ncolon - 1);
  1631. memcpy(fname, &path[nsprit + 1], abs(npoint - nsprit - 1));
  1632. if (npoint) memcpy(ext, &path[npoint], nlen - npoint);
  1633. #endif
  1634. }
  1635. //---------------------------------------------------------------------
  1636. // add by Jeff 2014.10.27
  1637. // 函数:全局函数IsDirectoryLegitimate,多字节版本,非UNICODE
  1638. // 描述:判断一个目录路径字符串,是否属于合法的、可创建的目录路径。
  1639. // 参数:strDirectory 被验证的路径字符串;
  1640. //
  1641. // 返回:合法路径返回TRUE;
  1642. //---------------------------------------------------------------------
  1643. BOOL IsDirectoryLegitimate(const CString &strDirectory)
  1644. {
  1645. if (strDirectory.Find('/') != -1 ||
  1646. strDirectory.Find('\\') != -1 ||
  1647. strDirectory.Find(':') != -1 ||
  1648. strDirectory.Find('*') != -1 ||
  1649. strDirectory.Find('?') != -1 ||
  1650. strDirectory.Find('\"') != -1 ||
  1651. strDirectory.Find('>') != -1 ||
  1652. strDirectory.Find('<') != -1 ||
  1653. strDirectory.Find('|') != -1
  1654. )
  1655. return FALSE;
  1656. return TRUE;
  1657. }
  1658. //--------------------------------------------------------------------------------
  1659. // Jeff add 2014.06.23;
  1660. // 函数:ErrorExit
  1661. // 描述:
  1662. // 参数:
  1663. // lpszFunction:函数名;
  1664. // dwError:错误码;
  1665. //
  1666. //--------------------------------------------------------------------------------
  1667. void ShowSystemErrorInfo(CString strDescription, const DWORD &dwError)
  1668. {
  1669. #if 1
  1670. LPVOID lpMsgBuf;
  1671. BOOL fOk = FormatMessage(
  1672. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1673. FORMAT_MESSAGE_FROM_SYSTEM |
  1674. FORMAT_MESSAGE_IGNORE_INSERTS,
  1675. NULL,
  1676. dwError,
  1677. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  1678. (LPTSTR)&lpMsgBuf,
  1679. 0, NULL);
  1680. if (!fOk)
  1681. {
  1682. // Is it a network-related error?
  1683. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  1684. if (hDll != NULL)
  1685. {
  1686. FormatMessage(
  1687. FORMAT_MESSAGE_FROM_HMODULE |
  1688. FORMAT_MESSAGE_FROM_SYSTEM |
  1689. FORMAT_MESSAGE_IGNORE_INSERTS,
  1690. hDll,
  1691. dwError,
  1692. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1693. (LPTSTR)&lpMsgBuf,
  1694. 0,
  1695. NULL);
  1696. FreeLibrary(hDll);
  1697. }
  1698. }
  1699. if (lpMsgBuf != NULL)
  1700. {
  1701. CString strDisplay;
  1702. strDisplay.Format(_T("%s.错误码=%d,Windows描述:%s"), strDescription, dwError, (PCTSTR)LocalLock(lpMsgBuf));
  1703. WriteLog(strDisplay);
  1704. LocalFree(lpMsgBuf);
  1705. }
  1706. else
  1707. {
  1708. WriteLog(strDescription);
  1709. }
  1710. #else
  1711. HLOCAL hlocal = NULL; // Buffer that gets the error message string
  1712. // Get the error code's textual description
  1713. BOOL fOk = FormatMessage(
  1714. FORMAT_MESSAGE_FROM_SYSTEM |
  1715. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1716. FORMAT_MESSAGE_IGNORE_INSERTS,
  1717. NULL,
  1718. dwError,
  1719. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1720. (PTSTR)&hlocal,
  1721. 0,
  1722. NULL);
  1723. if (!fOk)
  1724. {
  1725. // Is it a network-related error?
  1726. HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
  1727. if (hDll != NULL)
  1728. {
  1729. FormatMessage(
  1730. FORMAT_MESSAGE_FROM_HMODULE |
  1731. FORMAT_MESSAGE_FROM_SYSTEM |
  1732. FORMAT_MESSAGE_IGNORE_INSERTS,
  1733. hDll,
  1734. dwError,
  1735. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  1736. (PTSTR)&hlocal,
  1737. 0,
  1738. NULL);
  1739. FreeLibrary(hDll);
  1740. }
  1741. }
  1742. if (hlocal != NULL)
  1743. {
  1744. CString strDisplay;
  1745. strDisplay.Format("%s 失败错误码=%d,Windows系统描述:%s", strDescription, dwError, (PCTSTR)LocalLock(hlocal));
  1746. WriteLog(strDisplay);
  1747. LocalFree(hlocal);
  1748. }
  1749. else
  1750. {
  1751. WriteLog("Error number not found.");
  1752. }
  1753. #endif
  1754. }
  1755. // 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).
  1756. // After the time-out interval elapses, the system is restarted.
  1757. //The application must enable the SE_SHUTDOWN_NAME privilege before calling InitiateSystemShutdown
  1758. BOOL MySystemShutdown(LPTSTR lpMsg)
  1759. {
  1760. HANDLE hToken; // handle to process token
  1761. TOKEN_PRIVILEGES tkp; // pointer to token structure
  1762. BOOL fResult; // system shutdown flag
  1763. // Get the current process token handle so we can get shutdown
  1764. // privilege.
  1765. if (!OpenProcessToken(GetCurrentProcess(),
  1766. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  1767. return FALSE;
  1768. // Get the LUID for shutdown privilege.
  1769. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  1770. &tkp.Privileges[0].Luid);
  1771. tkp.PrivilegeCount = 1; // one privilege to set
  1772. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1773. // Get shutdown privilege for this process.
  1774. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  1775. (PTOKEN_PRIVILEGES)NULL, 0);
  1776. // Cannot test the return value of AdjustTokenPrivileges.
  1777. if (GetLastError() != ERROR_SUCCESS)
  1778. return FALSE;
  1779. // Display the shutdown dialog box and start the countdown.
  1780. fResult = InitiateSystemShutdown(
  1781. NULL, // shut down local computer
  1782. lpMsg, // message for user
  1783. 30, // time-out period, in seconds
  1784. FALSE, // ask user to close apps
  1785. TRUE); // reboot after shutdown
  1786. if (!fResult)
  1787. return FALSE;
  1788. // Disable shutdown privilege.
  1789. tkp.Privileges[0].Attributes = 0;
  1790. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  1791. (PTOKEN_PRIVILEGES)NULL, 0);
  1792. return TRUE;
  1793. }
  1794. // If the AbortSystemShutdown function is executed in the time-out period specified by InitiateSystemShutdown,
  1795. // the system does not shut down. For example, if PreventSystemShutdown is called after MySystemShutdown,
  1796. // the system closes the dialog box and does not restart the system.
  1797. BOOL PreventSystemShutdown()
  1798. {
  1799. HANDLE hToken; // handle to process token
  1800. TOKEN_PRIVILEGES tkp; // pointer to token structure
  1801. // Get the current process token handle so we can get shutdown
  1802. // privilege.
  1803. if (!OpenProcessToken(GetCurrentProcess(),
  1804. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  1805. return FALSE;
  1806. // Get the LUID for shutdown privilege.
  1807. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  1808. &tkp.Privileges[0].Luid);
  1809. tkp.PrivilegeCount = 1; // one privilege to set
  1810. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1811. // Get shutdown privilege for this process.
  1812. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  1813. (PTOKEN_PRIVILEGES)NULL, 0);
  1814. if (GetLastError() != ERROR_SUCCESS)
  1815. return FALSE;
  1816. // Prevent the system from shutting down.
  1817. if (!AbortSystemShutdown(NULL))
  1818. return FALSE;
  1819. // Disable shutdown privilege.
  1820. tkp.Privileges[0].Attributes = 0;
  1821. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  1822. (PTOKEN_PRIVILEGES)NULL, 0);
  1823. return TRUE;
  1824. }
  1825. // Shutting down flushes file buffers to disk and brings the system to a condition in which it is safe to turn off the computer
  1826. // The application must first enable the SE_SHUTDOWN_NAME privilege.
  1827. // The final parameter in the call to ExitWindowsEx indicates that the system was shut down for a planning update of the operating system.
  1828. BOOL MySystemShutdown()
  1829. {
  1830. HANDLE hToken;
  1831. TOKEN_PRIVILEGES tkp;
  1832. // Get a token for this process.
  1833. if (!OpenProcessToken(GetCurrentProcess(),
  1834. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  1835. return(FALSE);
  1836. // Get the LUID for the shutdown privilege.
  1837. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  1838. &tkp.Privileges[0].Luid);
  1839. tkp.PrivilegeCount = 1; // one privilege to set
  1840. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1841. // Get the shutdown privilege for this process.
  1842. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  1843. (PTOKEN_PRIVILEGES)NULL, 0);
  1844. if (GetLastError() != ERROR_SUCCESS)
  1845. return FALSE;
  1846. // Shut down the system and force all applications to close.
  1847. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
  1848. SHTDN_REASON_MAJOR_OPERATINGSYSTEM |
  1849. SHTDN_REASON_MINOR_UPGRADE |
  1850. SHTDN_REASON_FLAG_PLANNED))
  1851. return FALSE;
  1852. return TRUE;
  1853. }