From 556416a7f5b787e2681d64fe367debf76b9a4828 Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Sat, 22 Sep 2012 22:42:57 +0000 Subject: [PATCH] HLLAPI - Tentando identificar porque o VB esta dando segfault --- src/include/pw3270/hllapi.h | 2 +- src/plugins/remotectl/hllapi.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------- src/plugins/remotectl/testprogram.c | 24 +++++++++++------------- 3 files changed, 75 insertions(+), 83 deletions(-) diff --git a/src/include/pw3270/hllapi.h b/src/include/pw3270/hllapi.h index 5eb30d1..64f7860 100644 --- a/src/include/pw3270/hllapi.h +++ b/src/include/pw3270/hllapi.h @@ -63,7 +63,7 @@ extern "C" { #ifdef _WIN32 // http://www.mingw.org/wiki/Visual_Basic_DLL - __declspec (dllexport) int __stdcall hllapi(LPWORD func, LPSTR str, LPWORD length, LPWORD rc); + __declspec (dllexport) int __stdcall hllapi(const LPWORD func, LPSTR str, LPWORD length, LPWORD rc); #else LIB3270_EXPORT int hllapi(const unsigned long *func, char *str, unsigned short *length, unsigned short *rc); #endif // _WIN32 diff --git a/src/plugins/remotectl/hllapi.c b/src/plugins/remotectl/hllapi.c index 722f311..ed265ae 100644 --- a/src/plugins/remotectl/hllapi.c +++ b/src/plugins/remotectl/hllapi.c @@ -79,18 +79,16 @@ } - static int run_query(unsigned long func, const char *arg, char *string, unsigned short length, unsigned short *rc) + static char * run_query(unsigned long func, const char *arg, size_t length, unsigned short *rc) { - int result = -1; + char *outBuffer = NULL; #ifdef WIN32 - if(length < 0 && string) - length = strlen(string); - if(hPipe == INVALID_HANDLE_VALUE) { - *rc = result = EPERM; + trace("%s: Invalid pipe handle",__FUNCTION__); + *rc = EPERM; } else { @@ -105,28 +103,28 @@ data->rc = *rc; data->len = length; - if(length > 0) - { - memset(data->string,0,length); - if(arg) - strncpy(data->string,arg,length); - } - + memcpy(data->string,arg,length); memset(buffer,0,HLLAPI_MAXLENGTH); + if(!TransactNamedPipe(hPipe,(LPVOID) data,cbSize,buffer,HLLAPI_MAXLENGTH,&cbSize,NULL)) { - *rc = result = GetLastError(); + trace("Error %d in TransactNamedPipe",(int) GetLastError()); + *rc = GetLastError(); } else { - int sz = length < buffer->len ? length : buffer->len; - *rc = buffer->rc; - if(string && sz > 0) - memcpy(string,buffer->string,sz); + trace("buffer->len=%d rc=%d",buffer->len,buffer->rc); + + if(buffer->len > 0) + { + outBuffer = malloc(buffer->len+1); + memcpy(outBuffer,buffer->string,buffer->len); + outBuffer[buffer->len] = 0; - result = 0; + trace("outBuffer=[%s]",outBuffer); + } } free(data); @@ -139,62 +137,70 @@ #endif // WIN32 - return result; + return outBuffer; + } + + static void copyString(char *str, unsigned short *length, const char *msg) + { + size_t len = strlen(msg); + + if(len > *length) + len = *length; + else + *length = len; + + memcpy(str,msg,*length); } #ifdef _WIN32 - __declspec (dllexport) int __stdcall hllapi(LPWORD func, LPSTR str, LPWORD length, LPWORD rc) + __declspec (dllexport) int __stdcall hllapi(LPWORD func, LPSTR buffer, LPWORD length, LPWORD rc) #else - LIB3270_EXPORT int hllapi(const unsigned long *func, char *str, unsigned short *length, unsigned short *rc) + LIB3270_EXPORT int hllapi(const unsigned long *func, char *buffer, unsigned short *length, unsigned short *rc) #endif // _WIN32 { - int result = 1; - char * arg; - - if(!length || *length > HLLAPI_MAXLENGTH) - return *rc = EINVAL; + char * inBuffer = NULL; + char * outBuffer = NULL; - if(length > 0) + if(*length <= 0 || *length > HLLAPI_MAXLENGTH) { - arg = malloc(*length+1); - strncpy(arg,str,(int) *length); - arg[(size_t) *length] = 0; - } - else - { - arg = malloc(1); - *arg = 0; + *rc = EINVAL; + return 0; } + // Copy input argument + inBuffer = malloc(*length+1); + memcpy(inBuffer,buffer,*length); + inBuffer[*length] = 0; + + // Clear output buffer + memset(buffer,' ',*length); + buffer[*length] = 0; + switch(*func) { case HLLAPI_CMD_CONNECTPS: - *rc = result = cmd_connect_ps(arg); - if(!result) + *rc = cmd_connect_ps(inBuffer); + if(!*rc) { - result = run_query(*func, arg, str, *length, rc); - if(result || *rc) + outBuffer = run_query(*func, inBuffer, *length, rc); + if(*rc) { - trace("Closing pipe rc=%d result=%d ",*rc,result); + trace("Closing pipe rc=%d",*rc); CloseHandle(hPipe); hPipe = INVALID_HANDLE_VALUE; } } - else - { - *rc = result; - } break; case HLLAPI_CMD_DISCONNECTPS: #ifdef WIN32 if(hPipe == INVALID_HANDLE_VALUE) { - *rc = result = EINVAL; + *rc = EINVAL; } else { - result = run_query(*func, arg, str, *length, rc); + outBuffer = run_query(*func, inBuffer, *length, rc); CloseHandle(hPipe); hPipe = INVALID_HANDLE_VALUE; } @@ -202,32 +208,20 @@ break; default: - result = run_query(*func, arg, str, *length, rc); + trace("Calling function %d",(int) *func); + outBuffer = run_query(*func, inBuffer, *length, rc); } - if(result && length && *length && str) - strncpy(str,strerror(result),*length); + if(*rc) + copyString(buffer,length,strerror(*rc)); + else if(outBuffer) + copyString(buffer,length,outBuffer); - str[*length] = 0; - -#ifdef DEBUG - { - FILE *arq = fopen("hllapi.dbg","a"); - char *ptr; - - for(ptr=str;*ptr;ptr++) - { - if(*ptr == ' ') - *ptr = '.'; - } - - fprintf(arq,"func: %d\nresult: %d\nrc: %d\nLength: %d\nstring: [%s]\n",*func,result,*rc, *length, str); - fclose(arq); - } -#endif // DEBUG + if(outBuffer) + free(outBuffer); - free(arg); - return result; + free(inBuffer); + return 0; } diff --git a/src/plugins/remotectl/testprogram.c b/src/plugins/remotectl/testprogram.c index 3af3547..a47f15a 100644 --- a/src/plugins/remotectl/testprogram.c +++ b/src/plugins/remotectl/testprogram.c @@ -40,18 +40,17 @@ char buffer[BUFFER_LENGTH]; unsigned short rc; unsigned short len; - int result; unsigned long fn; static const struct _cmd { const char * name; - unsigned long fn; + unsigned short fn; const char * arg; } cmd[] = { { "ConnectPS", HLLAPI_CMD_CONNECTPS, "pw3270A" }, - { "GetRevision", HLLAPI_CMD_GETREVISION, "" }, + { "GetRevision", HLLAPI_CMD_GETREVISION, " " }, { "InputString", HLLAPI_CMD_INPUTSTRING, "test" }, }; @@ -61,26 +60,25 @@ for(f=0;f< (sizeof(cmd)/sizeof(struct _cmd)); f++) { - len = BUFFER_LENGTH; - strcpy(buffer,cmd[f].arg); - result = hllapi(&cmd[f].fn,buffer,&len,&rc); - printf("%s exits with result=%d rc=%d\n[%s]\n",cmd[f].name,result,rc,buffer); - + len = strlen(cmd[f].arg); + memcpy(buffer,cmd[f].arg,len); + hllapi((LPWORD) &cmd[f].fn,buffer,&len,&rc); + printf("%s exits with rc=%d\n[%s]\n",cmd[f].name,rc,buffer); } len = 80; rc = 1040; fn = HLLAPI_CMD_COPYPSTOSTR; - result = hllapi(&fn,buffer,&len,&rc); - printf("%s exits with result=%d rc=%d\n%s\n","HLLAPI_CMD_COPYPSTOSTR",result,rc,buffer); + hllapi((LPWORD) &fn,buffer,&len,&rc); + printf("%s exits with rc=%d\n%s\n","HLLAPI_CMD_COPYPSTOSTR",rc,buffer); // Disconnect - len = BUFFER_LENGTH; + len = 10; rc = 1; fn = HLLAPI_CMD_DISCONNECTPS; *buffer = 0; - result = hllapi(&fn,buffer,&len,&rc); - printf("%s exits with %d [%s]\n","HLLAPI_CMD_DISCONNECTPS",result,buffer); + hllapi((LPWORD) &fn,buffer,&len,&rc); + printf("%s exits with rc=%d\n[%s]\n","HLLAPI_CMD_DISCONNECTPS",rc,buffer); return 0; } -- libgit2 0.21.2