diff --git a/client/src/session/local/session.cc b/client/src/session/local/session.cc index 4772470..824a8df 100644 --- a/client/src/session/local/session.cc +++ b/client/src/session/local/session.cc @@ -65,28 +65,35 @@ namespace TN3270 { #ifdef _WIN32 - static void write_log(const char *msg, int rc = (int) GetLastError()) { - HANDLE hEventLog = RegisterEventSource(NULL, PACKAGE_NAME); + string getUserName() { - debug(msg," rc=",rc); + char username[UNLEN + 1]; + DWORD szName = sizeof(username); - if(hEventLog) { + memset(username,0,UNLEN + 1); + + if(!GetUserName(username, &szName)) { + return "?"; + } - char username[UNLEN + 1]; - DWORD szName = sizeof(username); + return string(username); - memset(username,0,szName); + } - if(!GetUserName(username, &szName)) { - strncpy(username,"?",UNLEN); - } + static void writeLog(HANDLE hEventLog, const char *msg, int rc = (int) GetLastError()) { + + + debug(msg," rc=",rc); + + if(hEventLog) { + string username = getUserName(); char lasterror[1024]; snprintf(lasterror,sizeof(lasterror),"The error code was %d",rc); const char *outMsg[] = { - username, + username.c_str(), msg, lasterror }; @@ -97,38 +104,83 @@ 1, 0, NULL, - 3, + (sizeof(outMsg) / sizeof(outMsg[0])), 0, outMsg, NULL ); - DeregisterEventSource(hEventLog); - } } - static HKEY openKey() { + static string getInstallLocation(HANDLE hEventLog) { LSTATUS rc; HKEY hKey = 0; - rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\pw3270",0,KEY_QUERY_VALUE,&hKey); - if(rc == ERROR_SUCCESS) { - return hKey; - } + static const char * keys[] = { +#ifdef LIB3270_NAME + "Software\\" LIB3270_STRINGIZE_VALUE_OF(LIB3270_NAME), + "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" LIB3270_STRINGIZE_VALUE_OF(LIB3270_NAME), +#endif + "Software\\pw3270", + "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\pw3270" + }; - write_log("Can't open HKLM\\Software\\pw3270", (int) rc); + size_t ix; - rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\pw3270",0,KEY_QUERY_VALUE,&hKey); - if(rc == ERROR_SUCCESS) { - return hKey; - } + string installLocation; + + for(ix = 0; installLocation.empty() && ix < (sizeof(keys)/sizeof(keys[0])); ix++) { + + debug(ix,"=",keys[ix]); + + rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,keys[ix],0,KEY_QUERY_VALUE,&hKey); + if(rc == ERROR_SUCCESS) { + + unsigned long datatype; // #defined in winnt.h (predefined types 0-11) + char datadir[4096]; + unsigned long datalen = sizeof(datadir); - write_log("Can't open HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\pw3270", (int) rc); + memset(datadir,0,datalen); - return 0; + rc = RegQueryValueExA(hKey,"InstallLocation",NULL,&datatype,(LPBYTE) datadir,&datalen); + if(rc == ERROR_SUCCESS) { + + debug("Found: ",datadir); + + installLocation.assign(datadir); + string username = getUserName(); + + const char *outMsg[] = { + username.c_str(), + "Found LIB3270 installation", + keys[ix], + installLocation.c_str() + }; + + ReportEvent( + hEventLog, + EVENTLOG_INFORMATION_TYPE, + 1, + 0, + NULL, + (sizeof(outMsg) / sizeof(outMsg[0])), + 0, + outMsg, + NULL + ); + + } + + RegCloseKey(hKey); + + } + + } + + return installLocation; } #endif // _WIN32 @@ -143,42 +195,31 @@ if(!initialized) { - // Get application DATADIR - LSTATUS rc; - - char datadir[4096]; - HKEY hKey = openKey(); - unsigned long datalen = sizeof(datadir); - - memset(datadir,0,sizeof(datadir)); +#ifdef LIB3270_NAME + HANDLE hEventLog = RegisterEventSource(NULL, LIB3270_STRINGIZE_VALUE_OF(LIB3270_NAME)); +#else + HANDLE hEventLog = RegisterEventSource(NULL, PACKAGE_NAME); +#endif // LIB3270_NAME - if(hKey) { + string installLocation = getInstallLocation(hEventLog); - unsigned long datatype; // #defined in winnt.h (predefined types 0-11) + if(installLocation.empty()) { - rc = RegQueryValueExA(hKey,"InstallLocation",NULL,&datatype,(LPBYTE) datadir,&datalen); - if(rc != ERROR_SUCCESS) { + writeLog(hEventLog, "Can't identify lib3270 installation path"); - // Can't get DATADIR - write_log("Can't get Install Location", (int) rc); + } else { - *datadir = 0; - } - RegCloseKey(hKey); + // TODO: Understand why SetDefaultDllDirectories & AddDllDirectory causes failure in DNS resolution. - } -#ifdef DEBUG - debug("Datadir=\"",datadir,"\""); -#endif // DEBUG - - if(*datadir) { + SetCurrentDirectory(installLocation.c_str()); +/* wchar_t *path = (wchar_t *) malloc(sizeof(datadir)*sizeof(wchar_t)); mbstowcs(path, datadir, 4095); if(!SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_USER_DIRS)) { - write_log("SetDefaultDllDirectories has failed"); + writeLog(hEventLog,"SetDefaultDllDirectories has failed"); } #ifdef DEBUG else { @@ -190,7 +231,7 @@ string msg = "Can't add "; msg += datadir; msg += " to directory path"; - write_log(msg.c_str()); + writeLog(hEventLog,msg.c_str()); } #ifdef DEBUG else { @@ -199,9 +240,11 @@ #endif free(path); +*/ } initialized = true; + DeregisterEventSource(hEventLog); } -- libgit2 0.21.2