OSDN Git Service

2012/01/28 2:50:13
[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 using namespace nlite;
7 CAppModule _Module;
8
9
10
11 namespace nlite{
12
13 Property nliteProperty; 
14
15 const CApplicationInfo * appInfo = NULL;
16
17 }
18 static TCHAR localAppDataPath[_MAX_PATH];
19
20 static WSAData wsaData;                                                 //winsockdata
21 static HINTERNET hInternet = NULL;
22
23 static BOOL InitializeNlite();                                  //\83A\83v\83\8a\83P\81[\83V\83\87\83\93\8f\89\8aú\89»
24 static VOID FinalizeNlite();                                    //\83A\83v\83\8a\83P\81[\83V\83\87\83\93\8fI\97¹\8f\88\97\9d
25 static CNliteMainFrame wnd;                                             //\83\81\83C\83\93\83t\83\8c\81[\83\80\83E\83B\83\93\83h\83E
26 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE,
27         LPTSTR lpCmdLine, int nCmdShow)
28 {
29    
30         int nRet = -1;
31         try{
32                 InitCommonControls();
33
34                 if(_Module.Init(NULL, hInstance) != S_OK){
35                         throw AppInitException(__LINE__,TEXT(__FILE__),TEXT(__FUNCTION__));
36                 }
37                         
38                 if(InitializeNlite() != TRUE){
39                         throw AppInitException(__LINE__,TEXT(__FILE__),TEXT(__FUNCTION__));
40                 }
41                 
42                 
43
44                 
45                 
46                 CMessageLoop theLoop;
47                 _Module.AddMessageLoop(&theLoop);
48
49
50                 wnd.Initialize(localAppDataPath);
51                 wnd.CreateEx(0,0,WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN);
52
53                 wnd.ShowWindow(nCmdShow);
54                 wnd.UpdateWindow();
55                 
56                 _heapmin();
57
58                 
59                 
60                 nRet = theLoop.Run();
61
62                 vcassert(_heapchk() == _HEAPOK,TEXT("\83q\81[\83v\83G\83\89\81[\82Å\82·"));
63
64                 goto nliteend;
65                 
66         } catch(nlite::Exception &e){
67                 
68                 UnexpectedErrorMessageShow(TEXT("\83n\83\93\83h\83\8b\82³\82ê\82Ä\82¢\82È\82¢\83G\83\89\81["),e.what(),e.getLineNo(),e.getFileName(),e.getFunctionName());
69                 
70
71                 goto nlitefinally;
72         }catch(std::exception &e){
73                 UnexpectedErrorMessageShow(TEXT("\83n\83\93\83h\83\8b\82³\82ê\82Ä\82¢\82È\82¢\83G\83\89\81["),CComBSTR(e.what()),-1,TEXT("\94­\90\83t\83@\83C\83\8b\82ª\93Á\92è\8fo\97\88\82Ü\82¹\82ñ"),TEXT("\94­\90\8aÖ\90\94\82ª\93Á\92è\82Å\82«\82Ü\82¹\82ñ"));
74
75                 
76
77                 goto nlitefinally;
78         } 
79
80 nlitefinally:
81         wnd.OnException();
82         wnd.DestroyWindow();
83
84
85 nliteend:
86
87         _Module.RemoveMessageLoop();
88         FinalizeNlite();
89         _Module.Term();
90
91
92         return nRet;
93 }
94
95 static BOOL InitializeNlite(){
96
97         BOOL rslt = FALSE;
98
99         try{
100                 
101                 nlite::appInfo = &CApplicationInfo::getInstance();
102                 INT_PTR err;
103                 INT_PTR count = 0;
104
105                 //WSAData\82ð\8f\89\8aú\89»
106                 while((err = WSAStartup(MAKEWORD(2,0),&wsaData)) == WSASYSNOTREADY){
107
108                         count++;
109                         if(count >= 5)break;
110                         Sleep(500);
111
112
113
114                 }
115                 
116                 if(err != ERROR_SUCCESS){
117
118                         throw AppInitException(__LINE__,TEXT(__FILE__),TEXT(__FUNCTION__));
119                 }
120
121                 const CApplicationInfo &appInfo = CApplicationInfo::getInstance();
122                 CNLiteString userAgent;
123                 userAgent = appInfo.getProcuctName();
124                 userAgent += TEXT("/");
125                 userAgent += appInfo.getProductVersion();
126
127
128                 hInternet = WinHttpOpen(userAgent,WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,(LPCWSTR)WINHTTP_NO_PROXY_NAME,(LPCWSTR)WINHTTP_NO_PROXY_BYPASS,0);
129
130                 if(hInternet == NULL){
131
132                         throw AppInitException(__LINE__,TEXT(__FILE__),TEXT(__FUNCTION__));
133
134                 }
135
136                 InitializeNLIB(hInternet);
137
138                 if(SHGetFolderPathW(0,CSIDL_LOCAL_APPDATA,0,SHGFP_TYPE_CURRENT,localAppDataPath) != S_OK){
139
140                         throw AppInitException(__LINE__,TEXT(__FILE__),TEXT(__FUNCTION__));
141                 }
142
143
144                 SystemParametersInfo (SPI_SETLISTBOXSMOOTHSCROLLING,FALSE,FALSE,0);
145
146
147         }catch (AppInitException e){
148
149                 return rslt;
150
151         }
152
153
154
155         rslt = TRUE;
156
157
158         return rslt;
159 }
160
161
162  VOID FinalizeNlite(){
163
164          wnd.Finalaize();
165          FinalizeNLIB();
166          WinHttpCloseHandle(hInternet);
167          WSACleanup();
168
169          return;
170  }
171
172  VOID nlite::UnexpectedErrorMessageShow(LPCTSTR headMessage,LPCTSTR summary,UINT line,LPCTSTR fileName,LPCTSTR function){
173         tstring errMessage;
174         errMessage = headMessage;
175         errMessage += TEXT("\91z\92è\82³\82ê\82Ä\82¢\82È\82¢\83G\83\89\81[\82ª\94­\90\82µ\82Ü\82µ\82½\81B\83A\83v\83\8a\83P\81[\83V\83\87\83\93\94z\92u\83t\83H\83\8b\83_\82É\8dì\90¬\82³\82ê\82½\83\8d\83O\83t\83@\83C\83\8b\82ð\93n\82µ\82Ä\82à\82ç\82¦\82é\82Æ\8aJ\94­\8eÒ\82ª\94ñ\8fí\82É\8aì\82Ñ\82Ü\82·");
176         errMessage += TEXT("\r\n\83G\83\89\81[\83\81\83b\83Z\81[\83W:");
177         errMessage += summary;
178         DebugOutW(fileName,function,line,DUMPING__DEBUGER__ | LINEFEAD__DEBUGER__ ,ERRORMODE__DEBUGER__,TEXT("%s"),errMessage);
179         ::MessageBox(NULL,errMessage.c_str(),TEXT("\91z\92è\82³\82ê\82Ä\82¢\82È\82¢\83G\83\89\81["),MB_ICONERROR | MB_OK);
180         
181
182
183          return;
184  }