OSDN Git Service

2011/12/13 1:00:45
[nlite/nlite.git] / nlite / nlite.cpp
1 // nlite.cpp : \83A\83v\83\8a\83P\81[\83V\83\87\83\93\82Ì\83G\83\93\83g\83\8a \83|\83C\83\93\83g\82ð\92è\8b`\82µ\82Ü\82·\81B
2 //
3
4 #include "stdafx.h"
5 #include "nlite_include.h"
6
7
8 CAppModule _Module;
9
10 const CApplicationInfo* nlite::appInfo = NULL;
11 static TCHAR localAppDataPath[_MAX_PATH];
12
13 static WSAData wsaData;                                                 //winsockdata
14 static HINTERNET hInternet = NULL;
15
16 static BOOL InitializeNlite();                                  //\83A\83v\83\8a\83P\81[\83V\83\87\83\93\8f\89\8aú\89»
17 static VOID FinalizeNlite();                                    //\83A\83v\83\8a\83P\81[\83V\83\87\83\93\8fI\97¹\8f\88\97\9d
18 static CNliteMainFrame wnd;                                             //\83\81\83C\83\93\83t\83\8c\81[\83\80\83E\83B\83\93\83h\83E
19 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE,
20         LPTSTR lpCmdLine, int nCmdShow)
21 {
22    
23         int nRet = -1;
24         try{
25                 if(_Module.Init(NULL, hInstance) != S_OK){
26                         throw AppInitException(__LINE__,TEXT(__FILE__));
27                 }
28                         
29                 if(InitializeNlite() != TRUE){
30                         throw AppInitException(__LINE__,TEXT(__FILE__));
31                 }
32                 
33
34                 
35                 
36                 CMessageLoop theLoop;
37                 _Module.AddMessageLoop(&theLoop);
38
39
40
41                 wnd.CreateEx(0,0,WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN);
42                 wnd.ShowWindow(nCmdShow);
43                 wnd.UpdateWindow();
44                 wnd.Initialize(localAppDataPath);
45                 _heapmin();
46
47                 
48
49                 nRet = theLoop.Run();
50
51                 vcassert(_heapchk() == _HEAPOK,TEXT("\83q\81[\83v\83G\83\89\81[\82Å\82·"));
52
53                 
54         } catch(nlite::Exception &e){
55                 tstring errMessage(TEXT("\83G\83\89\81[\83\81\83b\83Z\81[\83W:"));
56                 errMessage += e.what();
57                 errMessage += TEXT("\r\n\8ds\94Ô\8d\86:");
58                 std::wostringstream num;
59                 num << e.getLineNo();
60                 errMessage += num.str();
61                 errMessage += TEXT("\r\n\83t\83@\83C\83\8b\96¼:");
62                 errMessage += e.getFileName();
63                 
64                 MessageBox(NULL,errMessage.c_str(),TEXT("\83n\83\93\83h\83\8b\82³\82ê\82Ä\82È\82¢\83G\83\89\81["),MB_ICONERROR | MB_OK);
65         }catch(std::exception &e){
66                 
67                 MessageBoxA(NULL,e.what(),"\83n\83\93\83h\83\8b\82³\82ê\82Ä\82È\82¢\95s\96¾\82Ì\83G\83\89\81[",MB_ICONERROR | MB_OK);
68         } 
69         _Module.RemoveMessageLoop();
70         FinalizeNlite();
71         _Module.Term();
72
73
74         return nRet;
75 }
76
77 static BOOL InitializeNlite(){
78
79         BOOL rslt = FALSE;
80
81         try{
82
83                 nlite::appInfo = &CApplicationInfo::getInstance();
84                 INT_PTR err;
85                 INT_PTR count = 0;
86
87                 //WSAData\82ð\8f\89\8aú\89»
88                 while((err = WSAStartup(MAKEWORD(2,0),&wsaData)) == WSASYSNOTREADY){
89
90                         count++;
91                         if(count >= 5)break;
92                         Sleep(500);
93
94
95
96                 }
97                 
98                 if(err != ERROR_SUCCESS){
99
100                         throw AppInitException(__LINE__,TEXT(__FILE__));
101                 }
102
103                 const CApplicationInfo &appInfo = CApplicationInfo::getInstance();
104                 tstring userAgent;
105                 userAgent.reserve(256);
106                 userAgent = appInfo.getProcuctName();
107                 userAgent += TEXT("/");
108                 userAgent += appInfo.getProductVersion();
109
110
111                 hInternet = WinHttpOpen(userAgent.c_str(),WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,(LPCWSTR)WINHTTP_NO_PROXY_NAME,(LPCWSTR)WINHTTP_NO_PROXY_BYPASS,0);
112
113                 if(hInternet == NULL){
114
115                         throw AppInitException(__LINE__,TEXT(__FILE__));
116
117                 }
118
119                 InitializeNLIB(hInternet);
120
121                 if(SHGetFolderPathW(0,CSIDL_LOCAL_APPDATA,0,SHGFP_TYPE_CURRENT,localAppDataPath) != S_OK){
122
123                         throw AppInitException(__LINE__,TEXT(__FILE__));
124                 }
125
126
127                 
128
129
130         }catch (AppInitException e){
131
132                 return rslt;
133
134         }
135
136
137
138         rslt = TRUE;
139
140
141         return rslt;
142 }
143
144
145  VOID FinalizeNlite(){
146
147          wnd.Finalaize();
148          FinalizeNLIB();
149          WinHttpCloseHandle(hInternet);
150          WSACleanup();
151
152          return;
153  }