Commit 388da1f5a6ad1462eba63859c4fe9fa2860133cc
1 parent
3e424bdd
Exists in
master
and in
5 other branches
Classe de acesso remoto também passa a permitir a criação de uma janela do emulador em windows
Showing
1 changed file
with
80 additions
and
14 deletions
Show diff stats
src/classlib/remote.cc
@@ -46,6 +46,7 @@ | @@ -46,6 +46,7 @@ | ||
46 | #if defined(WIN32) | 46 | #if defined(WIN32) |
47 | #include <windows.h> | 47 | #include <windows.h> |
48 | #include <pw3270/ipcpackets.h> | 48 | #include <pw3270/ipcpackets.h> |
49 | + #include <process.h> | ||
49 | #else | 50 | #else |
50 | #define HLLAPI_PACKET_IS_CONNECTED "isConnected" | 51 | #define HLLAPI_PACKET_IS_CONNECTED "isConnected" |
51 | #define HLLAPI_PACKET_GET_CSTATE "getConnectionState" | 52 | #define HLLAPI_PACKET_GET_CSTATE "getConnectionState" |
@@ -277,34 +278,99 @@ | @@ -277,34 +278,99 @@ | ||
277 | remote(const char *session) | 278 | remote(const char *session) |
278 | { | 279 | { |
279 | #if defined(WIN32) | 280 | #if defined(WIN32) |
280 | - static DWORD dwMode = PIPE_READMODE_MESSAGE; | ||
281 | - char buffer[4096]; | ||
282 | - char * str = strdup(session); | ||
283 | - char * ptr; | 281 | + static DWORD dwMode = PIPE_READMODE_MESSAGE; |
282 | + char buffer[4096]; | ||
283 | + char * str; | ||
284 | + char * ptr; | ||
285 | + time_t timer; | ||
286 | + WIN32_FIND_DATA FindFileData; | ||
287 | + | ||
284 | 288 | ||
285 | hPipe = INVALID_HANDLE_VALUE; | 289 | hPipe = INVALID_HANDLE_VALUE; |
286 | 290 | ||
287 | - for(ptr=str;*ptr;ptr++) | 291 | + if(strcasecmp(session,"start") == 0 || strcasecmp(session,"new") == 0) |
288 | { | 292 | { |
289 | - if(*ptr == ':') | ||
290 | - *ptr = '_'; | 293 | + // Start a new session |
294 | + char buffer[80]; | ||
295 | + char appName[4096]; | ||
296 | + HKEY hKey = 0; | ||
297 | + unsigned long datalen = 4096; | ||
298 | + STARTUPINFO si; | ||
299 | + PROCESS_INFORMATION pi; | ||
300 | + | ||
301 | + // Get application path | ||
302 | + *appName = 0; | ||
303 | + if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\pw3270",0,KEY_QUERY_VALUE,&hKey) == ERROR_SUCCESS) | ||
304 | + { | ||
305 | + unsigned long datatype; // #defined in winnt.h (predefined types 0-11) | ||
306 | + if(RegQueryValueExA(hKey,"appName",NULL,&datatype,(LPBYTE) appName,&datalen) != ERROR_SUCCESS) | ||
307 | + *appName = 0; | ||
308 | + RegCloseKey(hKey); | ||
309 | + } | ||
310 | + | ||
311 | + trace("%s appname=%s\n",__FUNCTION__,appName); | ||
312 | + | ||
313 | + snprintf(buffer,79,"%s --session=\"H%06d\"",appName,getpid()); | ||
314 | + | ||
315 | + ZeroMemory( &si, sizeof(si) ); | ||
316 | + si.cb = sizeof(si); | ||
317 | + ZeroMemory( &pi, sizeof(pi) ); | ||
318 | + | ||
319 | + // si.dwFlags = STARTF_PREVENTPINNING; | ||
320 | + trace("App: %s",appName); | ||
321 | + trace("CmdLine: %s",buffer); | ||
322 | + | ||
323 | + if(CreateProcess(NULL,buffer,NULL,NULL,0,NORMAL_PRIORITY_CLASS,NULL,NULL,&si,&pi)) | ||
324 | + { | ||
325 | + CloseHandle( pi.hProcess ); | ||
326 | + CloseHandle( pi.hThread ); | ||
327 | + } | ||
291 | else | 328 | else |
292 | - *ptr = tolower(*ptr); | 329 | + { |
330 | + throw exception("Can't start %s session",PACKAGE_NAME); | ||
331 | + return; | ||
332 | + } | ||
333 | + | ||
334 | + snprintf(buffer,4095,"H%06d_a",getpid()); | ||
335 | + str = strdup(buffer); | ||
336 | + | ||
337 | + } | ||
338 | + else | ||
339 | + { | ||
340 | + // Use an existing session | ||
341 | + str = strdup(session); | ||
342 | + | ||
343 | + // Convert session name | ||
344 | + for(ptr=str;*ptr;ptr++) | ||
345 | + { | ||
346 | + if(*ptr == ':') | ||
347 | + *ptr = '_'; | ||
348 | + else | ||
349 | + *ptr = tolower(*ptr); | ||
350 | + } | ||
293 | } | 351 | } |
294 | 352 | ||
295 | snprintf(buffer,4095,"\\\\.\\pipe\\%s",str); | 353 | snprintf(buffer,4095,"\\\\.\\pipe\\%s",str); |
296 | 354 | ||
297 | free(str); | 355 | free(str); |
298 | 356 | ||
299 | - if(!WaitNamedPipe(buffer,NMPWAIT_USE_DEFAULT_WAIT)) | 357 | + timer = time(0)+5; |
358 | + while(hPipe == INVALID_HANDLE_VALUE && time(0) < timer) | ||
300 | { | 359 | { |
301 | - exception e = exception(GetLastError(),"Service instance %s unavailable",str); | ||
302 | - free(str); | ||
303 | - throw e; | ||
304 | - return; | 360 | + Sleep(10); |
361 | + hPipe = FindFirstFile(buffer, &FindFileData); | ||
305 | } | 362 | } |
306 | 363 | ||
307 | - hPipe = CreateFile(buffer,GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL); | 364 | + if(hPipe != INVALID_HANDLE_VALUE) |
365 | + { | ||
366 | + CloseHandle(hPipe); | ||
367 | + hPipe = CreateFile(buffer,GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL); | ||
368 | + } | ||
369 | + else | ||
370 | + { | ||
371 | + throw exception(GetLastError(),"Timeout waiting for %s instance",PACKAGE_NAME); | ||
372 | + return; | ||
373 | + } | ||
308 | 374 | ||
309 | if(hPipe == INVALID_HANDLE_VALUE) | 375 | if(hPipe == INVALID_HANDLE_VALUE) |
310 | { | 376 | { |