Ver Mensaje Individual
  #4
Antiguo 19 de junio de 2025, 16:50
Jorgeanna
Forero Junior
Última Actividad 25.01.2026 23:59
Posts Posts: 24
Likes enviados Enviados: 0
Likes recibidos Recibidos: 39

Por si ayuda en algo, te paso un script, que llamado de powercobol, devuelve el resultado de una operacion aritmetica utilizando ROUND_HALF_EVEN para el resultado, y otra que llama y ejecuta un link web, nada que extraordinario pero que te facilitara ver la mecanica de llamadas y respuestas via COM, tenia mas funciones que entregaban diferentes resultados, pero para mejor analisis las exclui y veas que no es dificil interacturar desde powercobol.
Código PYTHON:
  1. import win32com.client as wc
  2. import win32com.server.register
  3. import time
  4. import sys
  5. from decimal import *
  6. import os
  7. import winreg
  8. import pythoncom
  9.  
  10. class MODULOSCOM:
  11.     _public_methods_ = ['Calculo', 'Navegador']
  12.     _reg_progid_ = "MODULOS.COM"
  13.     _reg_clsid_ = "{80317A85-F824-45CD-82AC-3B3546C359EE}"
  14.     _reg_desc_ = "Servidor COM Python Para PowerVISUAL"
  15.  
  16.     def Navegador(self, Texto):
  17.         import webbrowser
  18.         webbrowser.open(Texto)
  19.         return True
  20.  
  21.     def Calculo(self, NumeroA, NumeroB, Operador, CantidadDecimales=2):
  22.         if type(NumeroA) != Decimal:
  23.             try:
  24.                 NumeroA = Decimal(str(NumeroA))
  25.             except:
  26.                 return 0
  27.         if type(NumeroB) != Decimal:
  28.             try:
  29.                 NumeroB = Decimal(str(NumeroB))
  30.             except:
  31.                 return 0
  32.  
  33.         if Operador == '+':
  34.             Operacion = NumeroA + NumeroB
  35.         elif Operador == '-':
  36.             Operacion = NumeroA - NumeroB
  37.         elif Operador == '*':
  38.             Operacion = NumeroA * NumeroB
  39.         elif Operador == '/':
  40.             try:
  41.                 Operacion = Decimal(str(NumeroA)) / Decimal(NumeroB)
  42.             except:
  43.                 return 0
  44.  
  45.         ResultadoPrevio = Decimal(str(Operacion))
  46.         Resultado = ResultadoPrevio.quantize(Decimal('1') / Decimal('10') ** Decimal(str(CantidadDecimales)),
  47.                                              rounding=ROUND_HALF_EVEN)
  48.  
  49.         return Resultado
  50.  
  51. def remove_registry_entry(clsid):
  52.     try:
  53.         reg_key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, f"CLSID\\{clsid}", 0, winreg.KEY_SET_VALUE)
  54.         winreg.DeleteKey(reg_key, "")
  55.         print(f"Clave de registro de {clsid} eliminada.")
  56.         winreg.CloseKey(reg_key)
  57.     except Exception as e:
  58.         print(f"Error al eliminar la clave de registro: {e}")
  59.  
  60. def check_registry_clsid(clsid):
  61.     try:
  62.         reg_key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, f"CLSID\\{clsid}")
  63.         winreg.CloseKey(reg_key)
  64.         return True
  65.     except FileNotFoundError:
  66.         return False
  67.  
  68. if __name__ == "__main__":
  69.     if len(sys.argv) > 1 and sys.argv[1].lower() == "register":
  70.         # Registra el servidor COM
  71.         win32com.server.register.UseCommandLine(MODULOSCOM)
  72.         print("Servidor COM registrado.")
  73.     elif len(sys.argv) > 1 and sys.argv[1].lower() == "unregister":
  74.         try:
  75.             print("Desregistrando el servidor COM...")
  76.  
  77.             # Intentamos desregistrar usando UseCommandLine
  78.             win32com.server.register.UseCommandLine(MODULOSCOM, unregister=True)
  79.             print("Desregistro usando UseCommandLine completado.")
  80.  
  81.             # Ahora eliminamos manualmente la clave del registro
  82.             remove_registry_entry(MODULOSCOM._reg_clsid_)
  83.  
  84.             # Verificamos si la clave ha sido eliminada
  85.             if not check_registry_clsid(MODULOSCOM._reg_clsid_):
  86.                 print("Servidor COM desregistrado correctamente.")
  87.             else:
  88.                 print("El servidor COM sigue registrado.")
  89.  
  90.             # Liberar las instancias COM si están en memoria
  91.             pythoncom.CoUninitialize()
  92.             print("Instancias COM liberadas.")
  93.  
  94.         except Exception as e:
  95.             print(f"Error al desregistrar el servidor COM: {e}")
  96.     else:
  97.         print("Usar 'register' o 'unregister' como argumento.")

desde POWERCOBOL
Código COBOL:
  1.  ENVIRONMENT     DIVISION.
  2.  DATA            DIVISION.
  3.  WORKING-STORAGE SECTION.
  4.  01 Objeto      pic x(14) value "MODULOS.COM".
  5.  01 ModulosCom      object reference COM.
  6.  01 NumeroA     Pic X(15).
  7.  01 NumeroB     Pic X(15).
  8.  01 Operador        Pic X.
  9.  01 Resultado       Pic X(15).
  10.  PROCEDURE       DIVISION.
  11.      invoke COM "CREATE-OBJECT" using Objeto returning ModulosCom. *> creo el objeto
  12.      
  13.      move "Text" OF CmText3 to NumeroA
  14.      move "Text" OF CmText5 to NumeroB
  15.      move "Text" OF CmText4 to Operador
  16.      invoke ModulosCom "Calculo" using NumeroA NumeroB Operador returning Resultado
  17.      
  18.      move Resultado to "Caption" OF CmStatic6
  19.      move Resultado to "Text" OF pasartexto
  20. *    INVOKE pow-self "DisplayMessage" USING  resultado
  21.      
  22.      set ModulosCom to null
LLAMADO AL NAVEGADOR VIA COM

Código COBOL:
  1.  ENVIRONMENT     DIVISION.
  2.  DATA            DIVISION.
  3.  WORKING-STORAGE SECTION.
  4.  01 Objeto      pic x(14) value "MODULOS.COM".
  5.  01 ModulosCom      object reference COM.
  6.  01 cuit                  pic x(11).
  7.  01 Resultado       Pic X(15).
  8.  01 Texto                 pic x(100).
  9.  PROCEDURE       DIVISION.
  10.      invoke COM "CREATE-OBJECT" using Objeto returning ModulosCom. *> creo el objeto
  11.      move spaces to texto
  12.      move "Text" OF dire to texto
  13.       invoke ModulosCom "Navegador" using texto returning Resultado
  14.      
  15.      set ModulosCom to null

Espero que te sea util la información, no hay mucho en la web que nos pueda ayudar.
Suerte.
Jorgeanna is offline   Responder Con Cita