Commit 556416a7f5b787e2681d64fe367debf76b9a4828
1 parent
0c837bc7
Exists in
master
and in
5 other branches
HLLAPI - Tentando identificar porque o VB esta dando segfault
Showing
3 changed files
with
75 additions
and
83 deletions
Show diff stats
src/include/pw3270/hllapi.h
| ... | ... | @@ -63,7 +63,7 @@ extern "C" { |
| 63 | 63 | |
| 64 | 64 | #ifdef _WIN32 |
| 65 | 65 | // http://www.mingw.org/wiki/Visual_Basic_DLL |
| 66 | - __declspec (dllexport) int __stdcall hllapi(LPWORD func, LPSTR str, LPWORD length, LPWORD rc); | |
| 66 | + __declspec (dllexport) int __stdcall hllapi(const LPWORD func, LPSTR str, LPWORD length, LPWORD rc); | |
| 67 | 67 | #else |
| 68 | 68 | LIB3270_EXPORT int hllapi(const unsigned long *func, char *str, unsigned short *length, unsigned short *rc); |
| 69 | 69 | #endif // _WIN32 | ... | ... |
src/plugins/remotectl/hllapi.c
| ... | ... | @@ -79,18 +79,16 @@ |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | |
| 82 | - static int run_query(unsigned long func, const char *arg, char *string, unsigned short length, unsigned short *rc) | |
| 82 | + static char * run_query(unsigned long func, const char *arg, size_t length, unsigned short *rc) | |
| 83 | 83 | { |
| 84 | - int result = -1; | |
| 84 | + char *outBuffer = NULL; | |
| 85 | 85 | |
| 86 | 86 | #ifdef WIN32 |
| 87 | 87 | |
| 88 | - if(length < 0 && string) | |
| 89 | - length = strlen(string); | |
| 90 | - | |
| 91 | 88 | if(hPipe == INVALID_HANDLE_VALUE) |
| 92 | 89 | { |
| 93 | - *rc = result = EPERM; | |
| 90 | + trace("%s: Invalid pipe handle",__FUNCTION__); | |
| 91 | + *rc = EPERM; | |
| 94 | 92 | } |
| 95 | 93 | else |
| 96 | 94 | { |
| ... | ... | @@ -105,28 +103,28 @@ |
| 105 | 103 | data->rc = *rc; |
| 106 | 104 | data->len = length; |
| 107 | 105 | |
| 108 | - if(length > 0) | |
| 109 | - { | |
| 110 | - memset(data->string,0,length); | |
| 111 | - if(arg) | |
| 112 | - strncpy(data->string,arg,length); | |
| 113 | - } | |
| 114 | - | |
| 106 | + memcpy(data->string,arg,length); | |
| 115 | 107 | memset(buffer,0,HLLAPI_MAXLENGTH); |
| 108 | + | |
| 116 | 109 | if(!TransactNamedPipe(hPipe,(LPVOID) data,cbSize,buffer,HLLAPI_MAXLENGTH,&cbSize,NULL)) |
| 117 | 110 | { |
| 118 | - *rc = result = GetLastError(); | |
| 111 | + trace("Error %d in TransactNamedPipe",(int) GetLastError()); | |
| 112 | + *rc = GetLastError(); | |
| 119 | 113 | } |
| 120 | 114 | else |
| 121 | 115 | { |
| 122 | - int sz = length < buffer->len ? length : buffer->len; | |
| 123 | - | |
| 124 | 116 | *rc = buffer->rc; |
| 125 | 117 | |
| 126 | - if(string && sz > 0) | |
| 127 | - memcpy(string,buffer->string,sz); | |
| 118 | + trace("buffer->len=%d rc=%d",buffer->len,buffer->rc); | |
| 119 | + | |
| 120 | + if(buffer->len > 0) | |
| 121 | + { | |
| 122 | + outBuffer = malloc(buffer->len+1); | |
| 123 | + memcpy(outBuffer,buffer->string,buffer->len); | |
| 124 | + outBuffer[buffer->len] = 0; | |
| 128 | 125 | |
| 129 | - result = 0; | |
| 126 | + trace("outBuffer=[%s]",outBuffer); | |
| 127 | + } | |
| 130 | 128 | } |
| 131 | 129 | |
| 132 | 130 | free(data); |
| ... | ... | @@ -139,62 +137,70 @@ |
| 139 | 137 | |
| 140 | 138 | #endif // WIN32 |
| 141 | 139 | |
| 142 | - return result; | |
| 140 | + return outBuffer; | |
| 141 | + } | |
| 142 | + | |
| 143 | + static void copyString(char *str, unsigned short *length, const char *msg) | |
| 144 | + { | |
| 145 | + size_t len = strlen(msg); | |
| 146 | + | |
| 147 | + if(len > *length) | |
| 148 | + len = *length; | |
| 149 | + else | |
| 150 | + *length = len; | |
| 151 | + | |
| 152 | + memcpy(str,msg,*length); | |
| 143 | 153 | } |
| 144 | 154 | |
| 145 | 155 | #ifdef _WIN32 |
| 146 | - __declspec (dllexport) int __stdcall hllapi(LPWORD func, LPSTR str, LPWORD length, LPWORD rc) | |
| 156 | + __declspec (dllexport) int __stdcall hllapi(LPWORD func, LPSTR buffer, LPWORD length, LPWORD rc) | |
| 147 | 157 | #else |
| 148 | - LIB3270_EXPORT int hllapi(const unsigned long *func, char *str, unsigned short *length, unsigned short *rc) | |
| 158 | + LIB3270_EXPORT int hllapi(const unsigned long *func, char *buffer, unsigned short *length, unsigned short *rc) | |
| 149 | 159 | #endif // _WIN32 |
| 150 | 160 | { |
| 151 | - int result = 1; | |
| 152 | - char * arg; | |
| 153 | - | |
| 154 | - if(!length || *length > HLLAPI_MAXLENGTH) | |
| 155 | - return *rc = EINVAL; | |
| 161 | + char * inBuffer = NULL; | |
| 162 | + char * outBuffer = NULL; | |
| 156 | 163 | |
| 157 | - if(length > 0) | |
| 164 | + if(*length <= 0 || *length > HLLAPI_MAXLENGTH) | |
| 158 | 165 | { |
| 159 | - arg = malloc(*length+1); | |
| 160 | - strncpy(arg,str,(int) *length); | |
| 161 | - arg[(size_t) *length] = 0; | |
| 162 | - } | |
| 163 | - else | |
| 164 | - { | |
| 165 | - arg = malloc(1); | |
| 166 | - *arg = 0; | |
| 166 | + *rc = EINVAL; | |
| 167 | + return 0; | |
| 167 | 168 | } |
| 168 | 169 | |
| 170 | + // Copy input argument | |
| 171 | + inBuffer = malloc(*length+1); | |
| 172 | + memcpy(inBuffer,buffer,*length); | |
| 173 | + inBuffer[*length] = 0; | |
| 174 | + | |
| 175 | + // Clear output buffer | |
| 176 | + memset(buffer,' ',*length); | |
| 177 | + buffer[*length] = 0; | |
| 178 | + | |
| 169 | 179 | switch(*func) |
| 170 | 180 | { |
| 171 | 181 | case HLLAPI_CMD_CONNECTPS: |
| 172 | - *rc = result = cmd_connect_ps(arg); | |
| 173 | - if(!result) | |
| 182 | + *rc = cmd_connect_ps(inBuffer); | |
| 183 | + if(!*rc) | |
| 174 | 184 | { |
| 175 | - result = run_query(*func, arg, str, *length, rc); | |
| 176 | - if(result || *rc) | |
| 185 | + outBuffer = run_query(*func, inBuffer, *length, rc); | |
| 186 | + if(*rc) | |
| 177 | 187 | { |
| 178 | - trace("Closing pipe rc=%d result=%d ",*rc,result); | |
| 188 | + trace("Closing pipe rc=%d",*rc); | |
| 179 | 189 | CloseHandle(hPipe); |
| 180 | 190 | hPipe = INVALID_HANDLE_VALUE; |
| 181 | 191 | } |
| 182 | 192 | } |
| 183 | - else | |
| 184 | - { | |
| 185 | - *rc = result; | |
| 186 | - } | |
| 187 | 193 | break; |
| 188 | 194 | |
| 189 | 195 | case HLLAPI_CMD_DISCONNECTPS: |
| 190 | 196 | #ifdef WIN32 |
| 191 | 197 | if(hPipe == INVALID_HANDLE_VALUE) |
| 192 | 198 | { |
| 193 | - *rc = result = EINVAL; | |
| 199 | + *rc = EINVAL; | |
| 194 | 200 | } |
| 195 | 201 | else |
| 196 | 202 | { |
| 197 | - result = run_query(*func, arg, str, *length, rc); | |
| 203 | + outBuffer = run_query(*func, inBuffer, *length, rc); | |
| 198 | 204 | CloseHandle(hPipe); |
| 199 | 205 | hPipe = INVALID_HANDLE_VALUE; |
| 200 | 206 | } |
| ... | ... | @@ -202,32 +208,20 @@ |
| 202 | 208 | break; |
| 203 | 209 | |
| 204 | 210 | default: |
| 205 | - result = run_query(*func, arg, str, *length, rc); | |
| 211 | + trace("Calling function %d",(int) *func); | |
| 212 | + outBuffer = run_query(*func, inBuffer, *length, rc); | |
| 206 | 213 | } |
| 207 | 214 | |
| 208 | - if(result && length && *length && str) | |
| 209 | - strncpy(str,strerror(result),*length); | |
| 215 | + if(*rc) | |
| 216 | + copyString(buffer,length,strerror(*rc)); | |
| 217 | + else if(outBuffer) | |
| 218 | + copyString(buffer,length,outBuffer); | |
| 210 | 219 | |
| 211 | - str[*length] = 0; | |
| 212 | - | |
| 213 | -#ifdef DEBUG | |
| 214 | - { | |
| 215 | - FILE *arq = fopen("hllapi.dbg","a"); | |
| 216 | - char *ptr; | |
| 217 | - | |
| 218 | - for(ptr=str;*ptr;ptr++) | |
| 219 | - { | |
| 220 | - if(*ptr == ' ') | |
| 221 | - *ptr = '.'; | |
| 222 | - } | |
| 223 | - | |
| 224 | - fprintf(arq,"func: %d\nresult: %d\nrc: %d\nLength: %d\nstring: [%s]\n",*func,result,*rc, *length, str); | |
| 225 | - fclose(arq); | |
| 226 | - } | |
| 227 | -#endif // DEBUG | |
| 220 | + if(outBuffer) | |
| 221 | + free(outBuffer); | |
| 228 | 222 | |
| 229 | - free(arg); | |
| 230 | - return result; | |
| 223 | + free(inBuffer); | |
| 224 | + return 0; | |
| 231 | 225 | } |
| 232 | 226 | |
| 233 | 227 | ... | ... |
src/plugins/remotectl/testprogram.c
| ... | ... | @@ -40,18 +40,17 @@ |
| 40 | 40 | char buffer[BUFFER_LENGTH]; |
| 41 | 41 | unsigned short rc; |
| 42 | 42 | unsigned short len; |
| 43 | - int result; | |
| 44 | 43 | unsigned long fn; |
| 45 | 44 | |
| 46 | 45 | static const struct _cmd |
| 47 | 46 | { |
| 48 | 47 | const char * name; |
| 49 | - unsigned long fn; | |
| 48 | + unsigned short fn; | |
| 50 | 49 | const char * arg; |
| 51 | 50 | } cmd[] = |
| 52 | 51 | { |
| 53 | 52 | { "ConnectPS", HLLAPI_CMD_CONNECTPS, "pw3270A" }, |
| 54 | - { "GetRevision", HLLAPI_CMD_GETREVISION, "" }, | |
| 53 | + { "GetRevision", HLLAPI_CMD_GETREVISION, " " }, | |
| 55 | 54 | { "InputString", HLLAPI_CMD_INPUTSTRING, "test" }, |
| 56 | 55 | |
| 57 | 56 | }; |
| ... | ... | @@ -61,26 +60,25 @@ |
| 61 | 60 | |
| 62 | 61 | for(f=0;f< (sizeof(cmd)/sizeof(struct _cmd)); f++) |
| 63 | 62 | { |
| 64 | - len = BUFFER_LENGTH; | |
| 65 | - strcpy(buffer,cmd[f].arg); | |
| 66 | - result = hllapi(&cmd[f].fn,buffer,&len,&rc); | |
| 67 | - printf("%s exits with result=%d rc=%d\n[%s]\n",cmd[f].name,result,rc,buffer); | |
| 68 | - | |
| 63 | + len = strlen(cmd[f].arg); | |
| 64 | + memcpy(buffer,cmd[f].arg,len); | |
| 65 | + hllapi((LPWORD) &cmd[f].fn,buffer,&len,&rc); | |
| 66 | + printf("%s exits with rc=%d\n[%s]\n",cmd[f].name,rc,buffer); | |
| 69 | 67 | } |
| 70 | 68 | |
| 71 | 69 | len = 80; |
| 72 | 70 | rc = 1040; |
| 73 | 71 | fn = HLLAPI_CMD_COPYPSTOSTR; |
| 74 | - result = hllapi(&fn,buffer,&len,&rc); | |
| 75 | - printf("%s exits with result=%d rc=%d\n%s\n","HLLAPI_CMD_COPYPSTOSTR",result,rc,buffer); | |
| 72 | + hllapi((LPWORD) &fn,buffer,&len,&rc); | |
| 73 | + printf("%s exits with rc=%d\n%s\n","HLLAPI_CMD_COPYPSTOSTR",rc,buffer); | |
| 76 | 74 | |
| 77 | 75 | // Disconnect |
| 78 | - len = BUFFER_LENGTH; | |
| 76 | + len = 10; | |
| 79 | 77 | rc = 1; |
| 80 | 78 | fn = HLLAPI_CMD_DISCONNECTPS; |
| 81 | 79 | *buffer = 0; |
| 82 | - result = hllapi(&fn,buffer,&len,&rc); | |
| 83 | - printf("%s exits with %d [%s]\n","HLLAPI_CMD_DISCONNECTPS",result,buffer); | |
| 80 | + hllapi((LPWORD) &fn,buffer,&len,&rc); | |
| 81 | + printf("%s exits with rc=%d\n[%s]\n","HLLAPI_CMD_DISCONNECTPS",rc,buffer); | |
| 84 | 82 | |
| 85 | 83 | return 0; |
| 86 | 84 | } | ... | ... |