OSDN Git Service

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