Commit 03a2aca3d53b02bd726a8ce671d31be08f57707c

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

Fixing windows load of the IPC library.

client/lib3270++.cbp
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 <CodeBlocks_project_file> 2 <CodeBlocks_project_file>
3 <FileVersion major="1" minor="6" /> 3 <FileVersion major="1" minor="6" />
4 <Project> 4 <Project>
5 - <Option title="C++ Bindings for lib3270" /> 5 + <Option title="IPC Client library for lib3270/pw3270" />
6 <Option makefile_is_custom="1" /> 6 <Option makefile_is_custom="1" />
7 <Option pch_mode="2" /> 7 <Option pch_mode="2" />
8 <Option compiler="gcc" /> 8 <Option compiler="gcc" />
@@ -53,10 +53,6 @@ @@ -53,10 +53,6 @@
53 <Unit filename="src/core/windows/resources.rc.in" /> 53 <Unit filename="src/core/windows/resources.rc.in" />
54 <Unit filename="src/core/windows/session.cc" /> 54 <Unit filename="src/core/windows/session.cc" />
55 <Unit filename="src/include/ipc-client-internals.h" /> 55 <Unit filename="src/include/ipc-client-internals.h" />
56 - <Unit filename="src/os/linux/request.cc" />  
57 - <Unit filename="src/os/linux/session.cc" />  
58 - <Unit filename="src/os/windows/request.cc" />  
59 - <Unit filename="src/os/windows/session.cc" />  
60 <Unit filename="src/session/local/events.cc" /> 56 <Unit filename="src/session/local/events.cc" />
61 <Unit filename="src/session/local/session.cc" /> 57 <Unit filename="src/session/local/session.cc" />
62 <Unit filename="src/session/remote/session.cc" /> 58 <Unit filename="src/session/remote/session.cc" />
client/src/session/local/session.cc
@@ -36,6 +36,15 @@ @@ -36,6 +36,15 @@
36 * 36 *
37 */ 37 */
38 38
  39 + #ifdef _WIN32
  40 +
  41 + #include <winsock2.h>
  42 + #include <windows.h>
  43 + #include <lmcons.h>
  44 + #include <libloaderapi.h>
  45 +
  46 + #endif // _WIN32
  47 +
39 #include <ipc-client-internals.h> 48 #include <ipc-client-internals.h>
40 #include <lib3270/actions.h> 49 #include <lib3270/actions.h>
41 #include <lib3270/properties.h> 50 #include <lib3270/properties.h>
@@ -45,6 +54,8 @@ @@ -45,6 +54,8 @@
45 54
46 extern "C" { 55 extern "C" {
47 #include <lib3270/session.h> 56 #include <lib3270/session.h>
  57 +
  58 +
48 } 59 }
49 60
50 using std::string; 61 using std::string;
@@ -53,10 +64,138 @@ @@ -53,10 +64,138 @@
53 64
54 namespace TN3270 { 65 namespace TN3270 {
55 66
  67 + #ifdef _WIN32
  68 + static void write_log(const char *msg, int rc = (int) GetLastError()) {
  69 +
  70 + HANDLE hEventLog = RegisterEventSource(NULL, PACKAGE_NAME);
  71 +
  72 + if(hEventLog) {
  73 +
  74 + char username[UNLEN + 1];
  75 + DWORD szName = sizeof(username);
  76 +
  77 + memset(username,0,szName);
  78 +
  79 + if(!GetUserName(username, &szName)) {
  80 + strncpy(username,"?",UNLEN);
  81 + }
  82 +
  83 + char lasterror[1024];
  84 + snprintf(lasterror,sizeof(lasterror),"The error code was %d",rc);
  85 +
  86 + const char *outMsg[] = {
  87 + username,
  88 + msg,
  89 + lasterror
  90 + };
  91 +
  92 + ReportEvent(
  93 + hEventLog,
  94 + EVENTLOG_ERROR_TYPE,
  95 + 1,
  96 + 0,
  97 + NULL,
  98 + 3,
  99 + 0,
  100 + outMsg,
  101 + NULL
  102 + );
  103 +
  104 + DeregisterEventSource(hEventLog);
  105 +
  106 + }
  107 +
  108 + }
  109 + #endif // _WIN32
  110 +
56 Local::Session::Session() : Abstract::Session() { 111 Local::Session::Session() : Abstract::Session() {
57 112
58 std::lock_guard<std::mutex> lock(sync); 113 std::lock_guard<std::mutex> lock(sync);
59 114
  115 +#ifdef _WIN32
  116 + {
  117 + static bool initialized = false;
  118 +
  119 + if(!initialized) {
  120 +
  121 + // Get application DATADIR
  122 +
  123 + // https://github.com/curl/curl/blob/master/lib/system_win32.c
  124 +
  125 + char datadir[4096];
  126 + HKEY hKey = 0;
  127 + unsigned long datalen = sizeof(datadir);
  128 +
  129 + memset(datadir,0,sizeof(datadir));
  130 +
  131 + LSTATUS rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\pw3270",0,KEY_QUERY_VALUE,&hKey);
  132 + if(rc == ERROR_SUCCESS) {
  133 +
  134 + unsigned long datatype; // #defined in winnt.h (predefined types 0-11)
  135 +
  136 + if(RegQueryValueExA(hKey,"InstallLocation",NULL,&datatype,(LPBYTE) datadir,&datalen) != ERROR_SUCCESS) {
  137 +
  138 + // Can't get DATADIR
  139 +
  140 + *datadir = 0;
  141 + }
  142 +
  143 + RegCloseKey(hKey);
  144 +
  145 + } else {
  146 +
  147 + write_log("Can't open HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\pw3270", (int) rc);
  148 +
  149 + }
  150 +
  151 + if(*datadir) {
  152 +
  153 + HMODULE kernel =
  154 + LoadLibrary("kernel32.dll");
  155 +
  156 + if(kernel) {
  157 +
  158 + HANDLE WINAPI (*AddDllDirectory)(PCWSTR) =
  159 + (HANDLE WINAPI (*)(PCWSTR)) GetProcAddress(kernel,"AddDllDirectory");
  160 +
  161 + //BOOL WINAPI (*RemoveDllDirectory)(HANDLE) =
  162 + // (BOOL WINAPI (*)(HANDLE)) GetProcAddress(kernel,"RemoveDllDirectory");
  163 +
  164 + if(AddDllDirectory) {
  165 +
  166 + wchar_t *path = (wchar_t *) malloc(sizeof(datadir)*sizeof(wchar_t));
  167 + mbstowcs(path, datadir, 4095);
  168 +
  169 + if(!AddDllDirectory(path)) {
  170 + write_log("AddDllDirectory has failed");
  171 + }
  172 +
  173 + free(path);
  174 +
  175 + } else {
  176 +
  177 + write_log("Can't find AddDllDirectory@kernel32.dll");
  178 +
  179 + }
  180 +
  181 + FreeLibrary(kernel);
  182 +
  183 +
  184 + } else {
  185 +
  186 + write_log("Can't load kernel32.dll");
  187 +
  188 + }
  189 +
  190 + }
  191 +
  192 + initialized = true;
  193 +
  194 + }
  195 +
  196 + }
  197 +#endif // _WIN32
  198 +
60 this->hSession = lib3270_session_new(""); 199 this->hSession = lib3270_session_new("");
61 lib3270_set_user_data(this->hSession,(void *) this); 200 lib3270_set_user_data(this->hSession,(void *) this);
62 setCharSet(lib3270_get_display_charset(this->hSession)); 201 setCharSet(lib3270_get_display_charset(this->hSession));