From 56fde83fbea652a89a183115700e9f76567e6c9f Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Fri, 12 Jul 2013 11:29:13 +0000 Subject: [PATCH] Melhorando tratamento de erros em windows --- src/classlib/exception.cc | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- src/classlib/remote.cc | 69 ++++++++++++++++++++++++++++++++++++++++++--------------------------- src/include/pw3270/class.h | 5 +++++ 3 files changed, 97 insertions(+), 36 deletions(-) diff --git a/src/classlib/exception.cc b/src/classlib/exception.cc index 0a982b2..b39eedd 100644 --- a/src/classlib/exception.cc +++ b/src/classlib/exception.cc @@ -38,22 +38,63 @@ namespace PW3270_NAMESPACE { - exception::exception(int syscode) - { + exception::exception(int syscode) + { snprintf(this->msg,4095,"%s",strerror(syscode)); - } + } - exception::exception(const char *fmt, ...) - { + exception::exception(const char *fmt, ...) + { va_list arg_ptr; va_start(arg_ptr, fmt); vsnprintf(this->msg,4095,fmt,arg_ptr); va_end(arg_ptr); - } + } - const char * exception::what() const throw() - { +#ifdef WIN32 + exception::exception(DWORD error, const char *fmt, ...) + { + LPVOID lpMsgBuf = 0; + char * ptr; + size_t szPrefix; + + va_list arg_ptr; + va_start(arg_ptr, fmt); + vsnprintf(this->msg,4095,fmt,arg_ptr); + va_end(arg_ptr); + + szPrefix = strlen(this->msg); + + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL); + + for(ptr= (char *) lpMsgBuf;*ptr && *ptr != '\n';ptr++); + *ptr = 0; + + snprintf(this->msg+szPrefix,4095-szPrefix,": %s (rc=%d)",(char *) lpMsgBuf,(int) error); + + LocalFree(lpMsgBuf); + + } +#else + exception::exception(int error, const char *fmt, ...) + { + size_t szPrefix; + + va_list arg_ptr; + va_start(arg_ptr, fmt); + vsnprintf(this->msg,4095,fmt,arg_ptr); + va_end(arg_ptr); + + szPrefix = strlen(this->msg); + + snprintf(this->msg+szPrefix,4095-szPrefix,": %s (rc=%d)",strerror(error),(int) error); + + } +#endif // WIN32 + + const char * exception::what() const throw() + { return this->msg; - } + } } diff --git a/src/classlib/remote.cc b/src/classlib/remote.cc index 8c12de7..e695184 100644 --- a/src/classlib/remote.cc +++ b/src/classlib/remote.cc @@ -71,9 +71,36 @@ if(TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL)) return response.rc; - throw exception("Error %d in TransactNamedPipe",GetLastError()); + throw exception(GetLastError(),"%s","Transaction error"); } + string * query_string(void *query, size_t szQuery, size_t len) + { + struct hllapi_packet_text * response; + DWORD cbSize = sizeof(struct hllapi_packet_text)+len; + string * s; + + response = (struct hllapi_packet_text *) malloc(cbSize+2); + memset(response,0,cbSize+2); + + if(TransactNamedPipe(hPipe,(LPVOID) query, szQuery, &response, cbSize, &cbSize,NULL)) + { + if(response->packet_id) + s = new string(""); + else + s = new string(response->text); + } + else + { + s = new string(""); + } + + free(response); + + return s; + } + + #elif defined(HAVE_DBUS) DBusConnection * conn; @@ -209,7 +236,9 @@ if(!WaitNamedPipe(buffer,NMPWAIT_USE_DEFAULT_WAIT)) { - throw exception("Invalid service instance: %s",name); + exception e = exception(GetLastError(),"Service instance %s unavailable",str); + free(str); + throw e; return; } @@ -221,7 +250,7 @@ } else if(!SetNamedPipeHandleState(hPipe,&dwMode,NULL,NULL)) { - exception e = exception("%s","Can´t set pipe state"); + exception e = exception(GetLastError(),"%s","Can´t set pipe state"); CloseHandle(hPipe); hPipe = INVALID_HANDLE_VALUE; throw e; @@ -309,6 +338,7 @@ exception e = exception("DBUS Connection Error (%s)", err.message); dbus_error_free(&err); throw e; + return; } if(!conn) @@ -449,12 +479,12 @@ if(TransactNamedPipe(hPipe,(LPVOID) pkt, cbSize, &response, sizeof(response), &cbSize,NULL)) { free(pkt); - return response.rc + return response.rc; } free(pkt); - throw exception("Transaction error %d",GetLastError()); + throw exception(GetLastError(),"%s","Transaction error"); #elif defined(HAVE_DBUS) @@ -616,29 +646,9 @@ { #if defined(WIN32) - struct hllapi_packet_query_at query = { HLLAPI_PACKET_GET_TEXT_AT, (unsigned short) row, (unsigned short) col, (unsigned short) sz }; - struct hllapi_packet_text * response; - DWORD cbSize = sizeof(struct hllapi_packet_text)+sz; - string * s; - - response = (struct hllapi_packet_text *) malloc(cbSize+2); - memset(response,0,cbSize+2); - - if(TransactNamedPipe(hPipe,(LPVOID) &query, sizeof(struct hllapi_packet_query_at), &response, cbSize, &cbSize,NULL)) - { - if(response->packet_id) - s = new string(""); - else - s = new string(response->text); - } - else - { - s = new string(""); - } - - free(response); + struct hllapi_packet_query_at query = { HLLAPI_PACKET_GET_TEXT_AT, (unsigned short) row, (unsigned short) col, (unsigned short) sz }; - return s; + return query_string(&query,sizeof(query),sz); #elif defined(HAVE_DBUS) @@ -766,8 +776,13 @@ string * get_text(int baddr, size_t len) { +#if defined(WIN32) + struct hllapi_packet_query_offset query = { HLLAPI_PACKET_GET_TEXT_AT_OFFSET, (unsigned short) baddr, (unsigned short) len }; + return query_string(&query,sizeof(query),len); +#else #warning IMPLEMENTAR return NULL; +#endif } diff --git a/src/include/pw3270/class.h b/src/include/pw3270/class.h index d726c0a..6e1a0c2 100644 --- a/src/include/pw3270/class.h +++ b/src/include/pw3270/class.h @@ -64,6 +64,10 @@ exception(int syserror = errno); exception(const char *fmt, ...); +#ifdef WIN32 + exception(DWORD error, const char *fmt, ...); +#endif // WIN32 + virtual const char * what() const throw(); private: @@ -71,6 +75,7 @@ }; + #if defined (HAVE_GNUC_VISIBILITY) class __attribute__((visibility("default"))) session #elif defined(WIN32) -- libgit2 0.21.2