OSDN Git Service

2012/01/13 17:42:02
[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                 InitCommonControls();
26
27                 if(_Module.Init(NULL, hInstance) != S_OK){
28                         throw AppInitException(__LINE__,TEXT(__FILE__));
29                 }
30                         
31                 if(InitializeNlite() != TRUE){
32                         throw AppInitException(__LINE__,TEXT(__FILE__));
33                 }
34                 
35
36                 
37                 
38                 CMessageLoop theLoop;
39                 _Module.AddMessageLoop(&theLoop);
40
41
42                 wnd.Initialize(localAppDataPath);
43                 wnd.CreateEx(0,0,WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN);
44                 wnd.ShowWindow(nCmdShow);
45                 wnd.UpdateWindow();
46                 
47                 _heapmin();
48
49                 
50
51                 nRet = theLoop.Run();
52
53                 vcassert(_heapchk() == _HEAPOK,TEXT("\83q\81[\83v\83G\83\89\81[\82Å\82·"));
54
55                 goto nliteend;
56                 
57         } catch(nlite::Exception &e){
58                 CNLiteString errMessage(TEXT("\83G\83\89\81[\83\81\83b\83Z\81[\83W:"));
59                 errMessage += e.what();
60                 errMessage += TEXT("\r\n\8ds\94Ô\8d\86:");
61                 std::wostringstream num;
62                 num << e.getLineNo();
63                 errMessage += num.str();
64                 errMessage += TEXT("\r\n\83t\83@\83C\83\8b\96¼:");
65                 errMessage += e.getFileName();
66                 
67                 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);
68                 errprint(TEXT("\83n\83\93\83h\83\8b\82³\82ê\82Ä\82¢\82È\82¢\83G\83\89\81[:%s"),errMessage.c_str());
69
70                 goto nlitefinally;
71         }catch(std::exception &e){
72                 
73                 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);
74                 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());
75
76                 goto nlitefinally;
77         } 
78
79 nlitefinally:
80         wnd.OnException();
81         wnd.DestroyWindow();
82
83
84 nliteend:
85
86         _Module.RemoveMessageLoop();
87         FinalizeNlite();
88         _Module.Term();
89
90
91         return nRet;
92 }
93
94 static BOOL InitializeNlite(){
95
96         BOOL rslt = FALSE;
97
98         try{
99
100                 nlite::appInfo = &CApplicationInfo::getInstance();
101                 INT_PTR err;
102                 INT_PTR count = 0;
103
104                 //WSAData\82ð\8f\89\8aú\89»
105                 while((err = WSAStartup(MAKEWORD(2,0),&wsaData)) == WSASYSNOTREADY){
106
107                         count++;
108                         if(count >= 5)break;
109                         Sleep(500);
110
111
112
113                 }
114                 
115                 if(err != ERROR_SUCCESS){
116
117                         throw AppInitException(__LINE__,TEXT(__FILE__));
118                 }
119
120                 const CApplicationInfo &appInfo = CApplicationInfo::getInstance();
121                 CNLiteString userAgent;
122                 userAgent.reserve(256);
123                 userAgent = appInfo.getProcuctName();
124                 userAgent += TEXT("/");
125                 userAgent += appInfo.getProductVersion();
126
127
128                 hInternet = WinHttpOpen(userAgent.c_str(),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__));
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__));
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  }