OSDN Git Service

2011/11/26 18:00:26
[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
8 CAppModule _Module;
9
10 const nlite::CApplicationInfo* appInfo = NULL;
11
12 static WSAData wsaData;                                                 //winsockdata
13 static HINTERNET hInternet = NULL;
14
15 static BOOL InitializeNlite();                                  //\83A\83v\83\8a\83P\81[\83V\83\87\83\93\8f\89\8aú\89»
16 static VOID FinalizeNlite();                                    //\83A\83v\83\8a\83P\81[\83V\83\87\83\93\8fI\97¹\8f\88\97\9d
17 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE,
18         LPTSTR lpCmdLine, int nCmdShow)
19 {
20    
21         int nRet = -1;
22         try{
23                 if(_Module.Init(NULL, hInstance) != S_OK){
24                         throw AppInitException(__LINE__,TEXT(__FILE__));
25                 }
26                         
27                 if(InitializeNlite() != TRUE){
28                         throw AppInitException(__LINE__,TEXT(__FILE__));
29                 }
30                 
31
32                 
33                 
34                 CMessageLoop theLoop;
35                 _Module.AddMessageLoop(&theLoop);
36
37                 // \93Æ\8e©\83E\83B\83\93\83h\83E\82ð\8dì\90¬
38                 CNliteMainFrame wnd;
39                 wnd.CreateEx(0,0,WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN);
40                 wnd.ShowWindow(nCmdShow);
41                 wnd.UpdateWindow();
42
43                 _heapmin();
44
45                 
46
47                 nRet = theLoop.Run();
48
49                 vcassert(_heapchk() == _HEAPOK,TEXT("\83q\81[\83v\83G\83\89\81[\82Å\82·"));
50
51                 
52         } catch(nlite::Exception &e){
53                 tstring errMessage(TEXT("\83G\83\89\81[\83\81\83b\83Z\81[\83W:"));
54                 errMessage += e.what();
55                 errMessage += TEXT("\r\n\8ds\94Ô\8d\86:");
56                 std::wostringstream num;
57                 num << e.getLineNo();
58                 errMessage += num.str();
59                 errMessage += TEXT("\r\n\83t\83@\83C\83\8b\96¼:");
60                 errMessage += e.getFileName();
61                 
62                 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);
63         }catch(std::exception &e){
64                 
65                 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);
66         } 
67         _Module.RemoveMessageLoop();
68         FinalizeNlite();
69         _Module.Term();
70
71
72         return nRet;
73 }
74
75 static BOOL InitializeNlite(){
76
77         BOOL rslt = FALSE;
78
79         try{
80
81                 appInfo = &CApplicationInfo::getInstance();
82                 INT_PTR err;
83                 INT_PTR count = 0;
84
85                 //WSAData\82ð\8f\89\8aú\89»
86                 while((err = WSAStartup(MAKEWORD(2,0),&wsaData)) == WSASYSNOTREADY){
87
88                         count++;
89                         if(count >= 5)break;
90                         Sleep(500);
91
92
93
94                 }
95                 
96                 if(err != ERROR_SUCCESS){
97
98                         throw AppInitException(__LINE__,TEXT(__FILE__));
99                 }
100
101                 const CApplicationInfo &appInfo = CApplicationInfo::getInstance();
102                 tstring userAgent;
103                 userAgent.reserve(256);
104                 userAgent = appInfo.getProcuctName();
105                 userAgent += TEXT("/");
106                 userAgent += appInfo.getProductVersion();
107
108
109                 hInternet = WinHttpOpen(userAgent.c_str(),WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,(LPCWSTR)WINHTTP_NO_PROXY_NAME,(LPCWSTR)WINHTTP_NO_PROXY_BYPASS,0);
110
111                 if(hInternet == NULL){
112
113                         throw AppInitException(__LINE__,TEXT(__FILE__));
114
115                 }
116
117                 InitializeNLIB(hInternet);
118
119         }catch (AppInitException e){
120
121                 return rslt;
122
123         }
124
125
126
127         rslt = TRUE;
128
129
130         return rslt;
131 }
132
133
134  VOID FinalizeNlite(){
135
136          FinalizeNLIB();
137          WinHttpCloseHandle(hInternet);
138          WSACleanup();
139
140          return;
141  }