OSDN Git Service

first commit
[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                 goto nliteend;
54                 
55         } catch(nlite::Exception &e){
56                 tstring errMessage(TEXT("\83G\83\89\81[\83\81\83b\83Z\81[\83W:"));
57                 errMessage += e.what();
58                 errMessage += TEXT("\r\n\8ds\94Ô\8d\86:");
59                 std::wostringstream num;
60                 num << e.getLineNo();
61                 errMessage += num.str();
62                 errMessage += TEXT("\r\n\83t\83@\83C\83\8b\96¼:");
63                 errMessage += e.getFileName();
64                 
65                 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);
66
67                 goto nlitefinally;
68         }catch(std::exception &e){
69                 
70                 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);
71
72                 goto nlitefinally;
73         } 
74
75 nlitefinally:
76         wnd.OnException();
77         wnd.DestroyWindow();
78
79
80 nliteend:
81
82         _Module.RemoveMessageLoop();
83         FinalizeNlite();
84         _Module.Term();
85
86
87         return nRet;
88 }
89
90 static BOOL InitializeNlite(){
91
92         BOOL rslt = FALSE;
93
94         try{
95
96                 nlite::appInfo = &CApplicationInfo::getInstance();
97                 INT_PTR err;
98                 INT_PTR count = 0;
99
100                 //WSAData\82ð\8f\89\8aú\89»
101                 while((err = WSAStartup(MAKEWORD(2,0),&wsaData)) == WSASYSNOTREADY){
102
103                         count++;
104                         if(count >= 5)break;
105                         Sleep(500);
106
107
108
109                 }
110                 
111                 if(err != ERROR_SUCCESS){
112
113                         throw AppInitException(__LINE__,TEXT(__FILE__));
114                 }
115
116                 const CApplicationInfo &appInfo = CApplicationInfo::getInstance();
117                 tstring userAgent;
118                 userAgent.reserve(256);
119                 userAgent = appInfo.getProcuctName();
120                 userAgent += TEXT("/");
121                 userAgent += appInfo.getProductVersion();
122
123
124                 hInternet = WinHttpOpen(userAgent.c_str(),WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,(LPCWSTR)WINHTTP_NO_PROXY_NAME,(LPCWSTR)WINHTTP_NO_PROXY_BYPASS,0);
125
126                 if(hInternet == NULL){
127
128                         throw AppInitException(__LINE__,TEXT(__FILE__));
129
130                 }
131
132                 InitializeNLIB(hInternet);
133
134                 if(SHGetFolderPathW(0,CSIDL_LOCAL_APPDATA,0,SHGFP_TYPE_CURRENT,localAppDataPath) != S_OK){
135
136                         throw AppInitException(__LINE__,TEXT(__FILE__));
137                 }
138
139
140                 SystemParametersInfo (SPI_SETLISTBOXSMOOTHSCROLLING,FALSE,FALSE,0);
141
142
143         }catch (AppInitException e){
144
145                 return rslt;
146
147         }
148
149
150
151         rslt = TRUE;
152
153
154         return rslt;
155 }
156
157
158  VOID FinalizeNlite(){
159
160          wnd.Finalaize();
161          FinalizeNLIB();
162          WinHttpCloseHandle(hInternet);
163          WSACleanup();
164
165          return;
166  }