VTM_IL.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // test.cpp : 定义应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "VTM_IL.h"
  5. #include<string>
  6. #include <windows.h>
  7. #include <shellapi.h>
  8. #include <fstream>
  9. #include <ShlObj.h>
  10. using namespace std;
  11. #define MAX_LOADSTRING 100
  12. // 全局变量:
  13. HINSTANCE hInst; // 当前实例
  14. TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
  15. TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
  16. // 此代码模块中包含的函数的前向声明:
  17. ATOM MyRegisterClass(HINSTANCE hInstance);
  18. BOOL InitInstance(HINSTANCE, int);
  19. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  20. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  21. int APIENTRY _tWinMain(HINSTANCE hInstance,
  22. HINSTANCE hPrevInstance,
  23. LPTSTR lpCmdLine,
  24. int nCmdShow)
  25. {
  26. UNREFERENCED_PARAMETER(hPrevInstance);
  27. UNREFERENCED_PARAMETER(lpCmdLine);
  28. // TODO: 在此放置代码。
  29. MSG msg;
  30. HACCEL hAccelTable;
  31. // 初始化全局字符串
  32. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  33. LoadString(hInstance, IDC_TEST, szWindowClass, MAX_LOADSTRING);
  34. MyRegisterClass(hInstance);
  35. // 执行应用程序初始化:
  36. if (!InitInstance (hInstance, nCmdShow))
  37. {
  38. return FALSE;
  39. }
  40. hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEST));
  41. // 主消息循环:
  42. while (GetMessage(&msg, NULL, 0, 0))
  43. {
  44. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  45. {
  46. TranslateMessage(&msg);
  47. DispatchMessage(&msg);
  48. }
  49. }
  50. return (int) msg.wParam;
  51. }
  52. //
  53. // 函数: MyRegisterClass()
  54. //
  55. // 目的: 注册窗口类。
  56. //
  57. // 注释:
  58. //
  59. // 仅当希望
  60. // 此代码与添加到 Windows 95 中的“RegisterClassEx”
  61. // 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
  62. // 这样应用程序就可以获得关联的
  63. // “格式正确的”小图标。
  64. //
  65. ATOM MyRegisterClass(HINSTANCE hInstance)
  66. {
  67. WNDCLASSEX wcex;
  68. wcex.cbSize = sizeof(WNDCLASSEX);
  69. wcex.style = CS_HREDRAW | CS_VREDRAW;
  70. wcex.lpfnWndProc = WndProc;
  71. wcex.cbClsExtra = 0;
  72. wcex.cbWndExtra = 0;
  73. wcex.hInstance = hInstance;
  74. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST));
  75. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  76. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  77. wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TEST);
  78. wcex.lpszClassName = szWindowClass;
  79. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  80. return RegisterClassEx(&wcex);
  81. }
  82. int GetVersion(char** pVersion)
  83. {
  84. char szFileName[MAX_PATH] = {0};
  85. char szFilePath[MAX_PATH] = {0};
  86. GetModuleFileNameA(NULL, szFileName, sizeof(szFileName));
  87. string strFileName = szFileName;
  88. int nPos = strFileName.find_last_of("\\");
  89. string strDir = strFileName.substr(0, nPos);
  90. sprintf_s(szFilePath, MAX_PATH,"%s\\active.txt", strDir.c_str());
  91. char cVer[MAX_PATH] = {0};
  92. ifstream infile(szFilePath);
  93. if(infile) // 有该文件
  94. {
  95. if(infile.getline(cVer, MAX_PATH))
  96. {
  97. memcpy_s(*pVersion, MAX_PATH, cVer, MAX_PATH);
  98. infile.close();
  99. return 1;
  100. }
  101. else
  102. {
  103. MessageBoxA(NULL,"获取版本信息失败:文件内容为空",NULL,0);
  104. return 0;
  105. }
  106. }
  107. else
  108. {
  109. MessageBoxA(NULL,"获取版本信息失败:文件打开失败",NULL,0);
  110. return 0;
  111. }
  112. infile.close();
  113. return 0;
  114. }
  115. BOOL GetSpShellPath(char** pPath)
  116. {
  117. char* pVer = new char[MAX_PATH];
  118. memset(pVer, 0, MAX_PATH);
  119. char szFileName[MAX_PATH] = {0};
  120. char szFilePath[MAX_PATH] = {0};
  121. GetModuleFileNameA(NULL, szFileName, sizeof(szFileName));
  122. string strFileName = szFileName;
  123. int nPos = strFileName.find_last_of("\\");
  124. string strDir = strFileName.substr(0, nPos);
  125. if(GetVersion(&pVer))
  126. {
  127. sprintf_s(szFilePath, MAX_PATH, "%s\\%s\\bin\\SpShell.exe", strDir.c_str(), pVer);
  128. }
  129. else
  130. {
  131. return FALSE;
  132. }
  133. memcpy_s(*pPath, MAX_PATH, szFilePath, MAX_PATH);
  134. if (NULL != pVer)
  135. {
  136. free(pVer);
  137. pVer = NULL;
  138. }
  139. ifstream shellFile(*pPath);
  140. if(shellFile)
  141. {
  142. return TRUE;
  143. }
  144. else
  145. {
  146. MessageBoxA(NULL,"获取执行文件失败:文件无效或文件不存在",NULL,0);
  147. return FALSE;
  148. }
  149. }
  150. BOOL Execute(const char* cExeName)
  151. {
  152. SHELLEXECUTEINFOA sei = {sizeof(SHELLEXECUTEINFOA)};
  153. sei.lpVerb = "runas";
  154. sei.lpFile = cExeName;
  155. sei.nShow = SW_SHOWNORMAL;
  156. if (!ShellExecuteExA(&sei))
  157. {
  158. DWORD dwError = GetLastError();
  159. char szError[128];
  160. memset(szError, '\0', 128);
  161. sprintf_s(szError, 128, "执行应用程序失败:%u", dwError);
  162. MessageBoxA(NULL, szError,NULL,0);
  163. return FALSE;
  164. }
  165. return TRUE;
  166. }
  167. BOOL RunSpshell()
  168. {
  169. char* pSpPath = new char[MAX_PATH];
  170. memset(pSpPath, 0, MAX_PATH);
  171. if (!GetSpShellPath(&pSpPath))
  172. {
  173. return FALSE;
  174. }
  175. if (!Execute(pSpPath))
  176. {
  177. return FALSE;
  178. }
  179. if (NULL != pSpPath)
  180. {
  181. free(pSpPath);
  182. pSpPath = NULL;
  183. }
  184. return TRUE;
  185. }
  186. //校正
  187. void checkActiveTxtAttr()
  188. {
  189. char vtmexePath[MAX_PATH] = {0};
  190. GetModuleFileNameA(NULL, vtmexePath, sizeof(vtmexePath));
  191. string strRunVtmPath = vtmexePath;
  192. int nPosVtm = strRunVtmPath.find_first_of("\\version\\VTM.exe");
  193. if(nPosVtm<0)
  194. {
  195. MessageBoxA(NULL,"VTM.exe 不在X:\\version\\VTM.exe下",NULL,0);
  196. return ;
  197. }
  198. int nPos = strRunVtmPath.find_last_of("\\");
  199. string strVersionDir = strRunVtmPath.substr(0, nPos);
  200. CString strActiveTxtPath=strVersionDir.c_str();//+"active.txt";
  201. strActiveTxtPath +="\\";
  202. strActiveTxtPath +="active.txt";
  203. CFileStatus rStatus;
  204. CFile::GetStatus(strActiveTxtPath,rStatus);//获得文件的属性设置
  205. //如果文件为只读的,将只读属性去掉
  206. if(rStatus.m_attribute&CFile::readOnly){
  207. rStatus.m_attribute=rStatus.m_attribute & 0x3E ;
  208. CFile::SetStatus(strActiveTxtPath, rStatus );//更改文件的属性设置
  209. }
  210. }
  211. //
  212. // 函数: InitInstance(HINSTANCE, int)
  213. //
  214. // 目的: 保存实例句柄并创建主窗口
  215. //
  216. // 注释:
  217. //
  218. // 在此函数中,我们在全局变量中保存实例句柄并
  219. // 创建和显示主程序窗口。
  220. //
  221. VOID HideTaskBar(BOOL bHide)
  222. {
  223. int nCmdShow;
  224. HWND hWnd;
  225. LPARAM lParam;
  226. hWnd = FindWindow(_T("Shell_TrayWnd"), NULL);
  227. if (bHide == TRUE) {
  228. nCmdShow = SW_HIDE;
  229. lParam = ABS_AUTOHIDE | ABS_ALWAYSONTOP;
  230. }
  231. else {
  232. nCmdShow = SW_SHOW;
  233. lParam = ABS_ALWAYSONTOP;
  234. }
  235. if (hWnd != NULL) {
  236. #ifdef DEVOPS_ON_PRD
  237. ShowWindow(hWnd, nCmdShow);
  238. #else
  239. APPBARDATA apBar;
  240. memset(&apBar, 0, sizeof(apBar));
  241. apBar.cbSize = sizeof(apBar);
  242. apBar.hWnd = hWnd;
  243. if (apBar.hWnd != NULL)
  244. {
  245. apBar.lParam = lParam;
  246. SHAppBarMessage(ABM_SETSTATE, &apBar);
  247. }
  248. #endif // DEVOPS_ON_PRD
  249. }
  250. }
  251. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  252. {
  253. // 校正active.txt的属性
  254. checkActiveTxtAttr();
  255. //启动框架
  256. if (RunSpshell()) {
  257. HideTaskBar(TRUE);
  258. }
  259. hInst = hInstance; // 将实例句柄存储在全局变量中
  260. /*hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  261. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  262. if (!hWnd)
  263. {
  264. return FALSE;
  265. }
  266. ShowWindow(hWnd, nCmdShow);
  267. UpdateWindow(hWnd);*/
  268. //MessageBoxA(NULL,"Hello",NULL,0);
  269. return FALSE;
  270. }
  271. //
  272. // 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
  273. //
  274. // 目的: 处理主窗口的消息。
  275. //
  276. // WM_COMMAND - 处理应用程序菜单
  277. // WM_PAINT - 绘制主窗口
  278. // WM_DESTROY - 发送退出消息并返回
  279. //
  280. //
  281. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  282. {
  283. int wmId, wmEvent;
  284. PAINTSTRUCT ps;
  285. HDC hdc;
  286. switch (message)
  287. {
  288. case WM_COMMAND:
  289. wmId = LOWORD(wParam);
  290. wmEvent = HIWORD(wParam);
  291. // 分析菜单选择:
  292. switch (wmId)
  293. {
  294. case IDM_ABOUT:
  295. DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  296. break;
  297. case IDM_EXIT:
  298. DestroyWindow(hWnd);
  299. break;
  300. default:
  301. return DefWindowProc(hWnd, message, wParam, lParam);
  302. }
  303. break;
  304. case WM_PAINT:
  305. hdc = BeginPaint(hWnd, &ps);
  306. // TODO: 在此添加任意绘图代码...
  307. EndPaint(hWnd, &ps);
  308. break;
  309. case WM_DESTROY:
  310. PostQuitMessage(0);
  311. break;
  312. default:
  313. return DefWindowProc(hWnd, message, wParam, lParam);
  314. }
  315. return 0;
  316. }
  317. // “关于”框的消息处理程序。
  318. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  319. {
  320. UNREFERENCED_PARAMETER(lParam);
  321. switch (message)
  322. {
  323. case WM_INITDIALOG:
  324. return (INT_PTR)TRUE;
  325. case WM_COMMAND:
  326. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  327. {
  328. EndDialog(hDlg, LOWORD(wParam));
  329. return (INT_PTR)TRUE;
  330. }
  331. break;
  332. }
  333. return (INT_PTR)FALSE;
  334. }