OSDN Git Service

2012/01/20 14:20:14
[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                 
66                 
67                 MessageBox(NULL,e.what(),TEXT("\83n\83\93\83h\83\8b\82³\82ê\82Ä\82È\82¢\83G\83\89\81["),MB_ICONERROR | MB_OK);
68                 errprint(TEXT(NLITE_ERR_MESSAGE_FORMAT),e.what(),e.getLineNo(),e.getFileName());
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,NLITE_ERR_MESSAGE_FORMAT,e.what(),__LINE__,__FILE__);
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 = appInfo.getProcuctName();
123                 userAgent += TEXT("/");
124                 userAgent += appInfo.getProductVersion();
125
126
127                 hInternet = WinHttpOpen(userAgent,WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,(LPCWSTR)WINHTTP_NO_PROXY_NAME,(LPCWSTR)WINHTTP_NO_PROXY_BYPASS,0);
128
129                 if(hInternet == NULL){
130
131                         throw AppInitException(__LINE__,TEXT(__FILE__));
132
133                 }
134
135                 InitializeNLIB(hInternet);
136
137                 if(SHGetFolderPathW(0,CSIDL_LOCAL_APPDATA,0,SHGFP_TYPE_CURRENT,localAppDataPath) != S_OK){
138
139                         throw AppInitException(__LINE__,TEXT(__FILE__));
140                 }
141
142
143                 SystemParametersInfo (SPI_SETLISTBOXSMOOTHSCROLLING,FALSE,FALSE,0);
144
145
146         }catch (AppInitException e){
147
148                 return rslt;
149
150         }
151
152
153
154         rslt = TRUE;
155
156
157         return rslt;
158 }
159
160
161  VOID FinalizeNlite(){
162
163          wnd.Finalaize();
164          FinalizeNLIB();
165          WinHttpCloseHandle(hInternet);
166          WSACleanup();
167
168          return;
169  }
170