Commit 4b0274f710811083abba17b47d8f3787df056ba0

Authored by Perry Werneck
1 parent 66d31e1f
Exists in master and in 1 other branch develop

Working in the win32 delayed loading mechanism for lib3270.

Showing 1 changed file with 94 additions and 51 deletions   Show diff stats
client/src/session/local/session.cc
... ... @@ -65,28 +65,35 @@
65 65 namespace TN3270 {
66 66  
67 67 #ifdef _WIN32
68   - static void write_log(const char *msg, int rc = (int) GetLastError()) {
69 68  
70   - HANDLE hEventLog = RegisterEventSource(NULL, PACKAGE_NAME);
  69 + string getUserName() {
71 70  
72   - debug(msg," rc=",rc);
  71 + char username[UNLEN + 1];
  72 + DWORD szName = sizeof(username);
73 73  
74   - if(hEventLog) {
  74 + memset(username,0,UNLEN + 1);
  75 +
  76 + if(!GetUserName(username, &szName)) {
  77 + return "?";
  78 + }
75 79  
76   - char username[UNLEN + 1];
77   - DWORD szName = sizeof(username);
  80 + return string(username);
78 81  
79   - memset(username,0,szName);
  82 + }
80 83  
81   - if(!GetUserName(username, &szName)) {
82   - strncpy(username,"?",UNLEN);
83   - }
  84 + static void writeLog(HANDLE hEventLog, const char *msg, int rc = (int) GetLastError()) {
  85 +
  86 +
  87 + debug(msg," rc=",rc);
  88 +
  89 + if(hEventLog) {
84 90  
  91 + string username = getUserName();
85 92 char lasterror[1024];
86 93 snprintf(lasterror,sizeof(lasterror),"The error code was %d",rc);
87 94  
88 95 const char *outMsg[] = {
89   - username,
  96 + username.c_str(),
90 97 msg,
91 98 lasterror
92 99 };
... ... @@ -97,38 +104,83 @@
97 104 1,
98 105 0,
99 106 NULL,
100   - 3,
  107 + (sizeof(outMsg) / sizeof(outMsg[0])),
101 108 0,
102 109 outMsg,
103 110 NULL
104 111 );
105 112  
106   - DeregisterEventSource(hEventLog);
107   -
108 113 }
109 114  
110 115 }
111 116  
112   - static HKEY openKey() {
  117 + static string getInstallLocation(HANDLE hEventLog) {
113 118  
114 119 LSTATUS rc;
115 120 HKEY hKey = 0;
116 121  
117   - rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\pw3270",0,KEY_QUERY_VALUE,&hKey);
118   - if(rc == ERROR_SUCCESS) {
119   - return hKey;
120   - }
  122 + static const char * keys[] = {
  123 +#ifdef LIB3270_NAME
  124 + "Software\\" LIB3270_STRINGIZE_VALUE_OF(LIB3270_NAME),
  125 + "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" LIB3270_STRINGIZE_VALUE_OF(LIB3270_NAME),
  126 +#endif
  127 + "Software\\pw3270",
  128 + "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\pw3270"
  129 + };
121 130  
122   - write_log("Can't open HKLM\\Software\\pw3270", (int) rc);
  131 + size_t ix;
123 132  
124   - rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\pw3270",0,KEY_QUERY_VALUE,&hKey);
125   - if(rc == ERROR_SUCCESS) {
126   - return hKey;
127   - }
  133 + string installLocation;
  134 +
  135 + for(ix = 0; installLocation.empty() && ix < (sizeof(keys)/sizeof(keys[0])); ix++) {
  136 +
  137 + debug(ix,"=",keys[ix]);
  138 +
  139 + rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,keys[ix],0,KEY_QUERY_VALUE,&hKey);
  140 + if(rc == ERROR_SUCCESS) {
  141 +
  142 + unsigned long datatype; // #defined in winnt.h (predefined types 0-11)
  143 + char datadir[4096];
  144 + unsigned long datalen = sizeof(datadir);
128 145  
129   - write_log("Can't open HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\pw3270", (int) rc);
  146 + memset(datadir,0,datalen);
130 147  
131   - return 0;
  148 + rc = RegQueryValueExA(hKey,"InstallLocation",NULL,&datatype,(LPBYTE) datadir,&datalen);
  149 + if(rc == ERROR_SUCCESS) {
  150 +
  151 + debug("Found: ",datadir);
  152 +
  153 + installLocation.assign(datadir);
  154 + string username = getUserName();
  155 +
  156 + const char *outMsg[] = {
  157 + username.c_str(),
  158 + "Found LIB3270 installation",
  159 + keys[ix],
  160 + installLocation.c_str()
  161 + };
  162 +
  163 + ReportEvent(
  164 + hEventLog,
  165 + EVENTLOG_INFORMATION_TYPE,
  166 + 1,
  167 + 0,
  168 + NULL,
  169 + (sizeof(outMsg) / sizeof(outMsg[0])),
  170 + 0,
  171 + outMsg,
  172 + NULL
  173 + );
  174 +
  175 + }
  176 +
  177 + RegCloseKey(hKey);
  178 +
  179 + }
  180 +
  181 + }
  182 +
  183 + return installLocation;
132 184 }
133 185  
134 186 #endif // _WIN32
... ... @@ -143,42 +195,31 @@
143 195  
144 196 if(!initialized) {
145 197  
146   - // Get application DATADIR
147   - LSTATUS rc;
148   -
149   - char datadir[4096];
150   - HKEY hKey = openKey();
151   - unsigned long datalen = sizeof(datadir);
152   -
153   - memset(datadir,0,sizeof(datadir));
  198 +#ifdef LIB3270_NAME
  199 + HANDLE hEventLog = RegisterEventSource(NULL, LIB3270_STRINGIZE_VALUE_OF(LIB3270_NAME));
  200 +#else
  201 + HANDLE hEventLog = RegisterEventSource(NULL, PACKAGE_NAME);
  202 +#endif // LIB3270_NAME
154 203  
155   - if(hKey) {
  204 + string installLocation = getInstallLocation(hEventLog);
156 205  
157   - unsigned long datatype; // #defined in winnt.h (predefined types 0-11)
  206 + if(installLocation.empty()) {
158 207  
159   - rc = RegQueryValueExA(hKey,"InstallLocation",NULL,&datatype,(LPBYTE) datadir,&datalen);
160   - if(rc != ERROR_SUCCESS) {
  208 + writeLog(hEventLog, "Can't identify lib3270 installation path");
161 209  
162   - // Can't get DATADIR
163   - write_log("Can't get Install Location", (int) rc);
  210 + } else {
164 211  
165   - *datadir = 0;
166   - }
167   - RegCloseKey(hKey);
  212 + // TODO: Understand why SetDefaultDllDirectories & AddDllDirectory causes failure in DNS resolution.
168 213  
169   - }
170 214  
171   -#ifdef DEBUG
172   - debug("Datadir=\"",datadir,"\"");
173   -#endif // DEBUG
174   -
175   - if(*datadir) {
  215 + SetCurrentDirectory(installLocation.c_str());
176 216  
  217 +/*
177 218 wchar_t *path = (wchar_t *) malloc(sizeof(datadir)*sizeof(wchar_t));
178 219 mbstowcs(path, datadir, 4095);
179 220  
180 221 if(!SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_USER_DIRS)) {
181   - write_log("SetDefaultDllDirectories has failed");
  222 + writeLog(hEventLog,"SetDefaultDllDirectories has failed");
182 223 }
183 224 #ifdef DEBUG
184 225 else {
... ... @@ -190,7 +231,7 @@
190 231 string msg = "Can't add ";
191 232 msg += datadir;
192 233 msg += " to directory path";
193   - write_log(msg.c_str());
  234 + writeLog(hEventLog,msg.c_str());
194 235 }
195 236 #ifdef DEBUG
196 237 else {
... ... @@ -199,9 +240,11 @@
199 240 #endif
200 241 free(path);
201 242  
  243 +*/
202 244 }
203 245  
204 246 initialized = true;
  247 + DeregisterEventSource(hEventLog);
205 248  
206 249 }
207 250  
... ...