BCX COM Errors and Status

BCX_COM_ERROR function

Purpose:

BCX_COM_ERROR returns status of the COM error indicator.

Syntax:

RetBool = BCX_COM_ERROR

Return Value:

  • Data type: BOOL
    RetBool The status of the COM error indicator. If a COM error occurs, TRUE is returned. If there is no error, FALSE is returned.

Parameters:

  • None.

Example:

CLS
   
DIM app AS OBJECT
DIM hRslt AS HRESULT
   
COMSET app = CREATEOBJECT("HeyDoofus.ThisIsWrong")
  
hRslt = BCX_COM_ERROR
   
IF hRslt THEN
  PRINT "COM ERROR!"
  END
END IF
    
COMSET app = NOTHING ' remember to call Set xxx = Nothing to release resources used by COM object!!!

BCX_SHOW_COM_ERRORS statement

Purpose:

BCX_SHOW_COM_ERRORS controls behavior of error reporting routine. If set to TRUE, a dialog box message will popup on each COM error with descriptive string from last COM error. If set to FALSE(default), the dialog box message will NOT popup, but error information still can be retrieved using one of the error reporting functions.

Syntax:

BCX_SHOW_COM_ERRORS(boolParam AS BOOL)

Parameters:

  • Data type: BOOL
    boolParam TRUE or FALSE specifying whether or not to show COM errors.

Remarks:

If the COM code is used in applications similar to cgi exe, that run on web servers or remote computers, popping of dialog box error messages on that computer is not wanted.

Example:

CLS
BCX_SHOW_COM_ERRORS(TRUE) ' Display error

DIM RetStr$
DIM app AS OBJECT
app = CREATEOBJECT("HeyDoofus.ThisIsWrong")
COMSET app = NOTHING ' remember to call Set xxx = Nothing to release resources used by COM object!!!

BCX_GET_COM_ERROR_CODE function

Purpose:

BCX_GET_COM_ERROR_CODE returns a HRESULT value specifying an error.

Syntax:

Ret_HRESULT = BCX_GET_COM_ERROR_CODE()

Return Value:

  • Data type: HRESULT
    Ret_HRESULT A numeric value specifying an error.

Parameters:

  • None.

Example:

CLS
  
DIM app AS OBJECT
DIM hRslt AS HRESULT
  
app = CREATEOBJECT("HeyDoofus.ThisIsWrong")
 
hRslt = BCX_COM_ERROR
  
IF hRslt THEN
  PRINT "COM ERROR CODE: ", BCX_GET_COM_ERROR_CODE()
  END
END IF
   
COMSET app = NOTHING ' remember to call Set xxx = Nothing to release resources used by COM object!!!

BCX_GET_COM_ERROR_DESC$ function

Purpose:

BCX_GET_COM_ERROR_DESC$ returns a descriptive string associated with the latest COM error.

Syntax:

RetStr = BCX_GET_COM_ERROR_DESC$

Return Value:

  • Data type: STRING
    RetStr Description of the latest COM error.

Parameters:

  • None.

Example:

CLS
DIM RetStr$
DIM app AS OBJECT
app = CREATEOBJECT("HeyDoofus.ThisIsWrong")
  
IF BCX_COM_ERROR THEN
  RetStr$ = BCX_GET_COM_ERROR_DESC$
  PRINT RetStr$
  END
END IF
 
COMSET app = NOTHING ' remember to call Set xxx = Nothing to release resources used by COM object!!!

Remarks:

👉 The PRINT cannot be used directly with BCX_GET_COM_ERROR_DESC$. An intermediate variable, like RetStr$ in the above example, must be used.


BCX_GET_COM_SUCCESS function

Purpose:

BCX_GET_COM_SUCCESS is opposite of BCX_COM_ERROR function. If a COM error does not occur, TRUE is returned. If there is an error, FALSE is returned.

Syntax:

RetBool = BCX_GET_COM_SUCCESS

Return Value:

  • Data type: BOOL
    RetBool The status of the COM error indicator. If a COM error does not occur, TRUE is returned. If there is an error, FALSE is returned.

Parameters:

  • None.

Example:

CLS
DIM RetStr$

DIM app AS OBJECT
app = CREATEOBJECT("Excel.Application")

IF BCX_GET_COM_SUCCESS THEN
  RetStr$ = "Excel will be visible for 2 sec, and than it will close."
  MSGBOX RetStr$
  END
END IF

IF BCX_COM_ERROR THEN
  RetStr$ = BCX_GET_COM_ERROR_DESC$
  PRINT RetStr$
  END
END IF
 
app.visible = true

SLEEP(2000) ' Excel will be visible for 2 sec, and than it will close.

app.activeworkbook.saved = true ' don't prompt to save workbook

app.quit

COMSET app = NOTHING ' remember to call Set xxx = Nothing to release resources used by COM object!!!

BCX_GET_COM_STATUS function

Purpose:

BCX_GET_COM_STATUS will return TRUE if an OBJECT is in a transaction and FALSE if it is not in a transaction. This is a useful function for debugging applications using COM.

Syntax:

 RetBool = BCX_GET_COM_STATUS(&ObjPtr)

Return Value:

  • Data type: BOOL
    RetBool The status of the transaction state of the COM OBJECT; TRUE if an OBJECT is in a transaction and FALSE if it is not in a transaction.

Parameters:

  • Data type: PTR
    &ObjPtr A pointer to the address of a COM OBJECT.

Example:

BCX_SHOW_COM_ERRORS(TRUE)

DIM RetBool
DIM app AS OBJECT
COMSET app = CREATEOBJECT("Excel.Application")

app.workbooks.add
app.visible = true
app.ActiveSheet.Cells(3,1).Value="Hello"
app.ActiveSheet.Cells(4,1).Value="From BCX"
app.ActiveSheet.Cells(5,1).Value="Console program!"

RetBool = BCX_GET_COM_STATUS(&app)
IF RetBool <> 0 THEN
  MSGBOX "Loaded app.ActiveSheet.Cells"
END IF

DIM temp_var$

temp_var$ = app.ActiveSheet.Cells(3,1).Value
MSGBOX temp_var$, "value of cell(3,1)", 4096

RetBool = BCX_GET_COM_STATUS(&app)
IF RetBool <> 0 THEN
  MSGBOX "Displayed value of cell(3,1)"
END IF

MSGBOX "BCX COM Example!" & CRLF$ _
& "Using Office automation to manipulate Excel." & CRLF$ _
& "Program will close Excel in 1 second.","finished!", 4096

SLEEP(1000)

app.activeworkbook.saved = true
app.quit
COMSET app = NOTHING

RetBool = BCX_GET_COM_STATUS(&app)
IF RetBool = 0 THEN
  MSGBOX "Nothing means Nothing"
END IF

ISOBJECT function

Purpose:

ISOBJECT will return TRUE if a COM OBJECT exists and FALSE if it does not exist.

Syntax:

RetBool = ISOBJECT(Object)

Return Value:

Parameters:

Example:

$BCXVERSION "7.5.2"

GLOBAL WordApp AS OBJECT
COMSET WordApp = CREATEOBJECT("Word.Application")
PRINT ISOBJECT(WordApp) ' PRINTS "1" (if you have MS Word installed) 
               
COMSET WordApp = NOTHING
COMSET WordApp = CREATEOBJECT("Weird.Application")
PRINT ISOBJECT(WordApp)   'PRINTS "0" because "Word." is misspelled. 
SET WordApp = NOTHING

BCX_CLSID$ function

Purpose:

BCX_CLSID$ will check if the specified COM OBJECT is properly registered on the system.

Syntax:

CLSID_GUID = BCX_CLSID$(Moniker AS STRING)

Return Value:

  • Data type: STRING
    CLSID_GUID If the Moniker is registered, a CLSID, in the form of a GUID 38-character string, is returned. Otherwise, the return is the message
    Failed to get CLSID
    

Parameters:

Remarks:

BCX_CLSID$ is also useful for resolving different versions of COM servers, such as "Word.Application" vs. "Word.Application.8".

Example:

PRINT BCX_CLSID$("word.application")  ' "{000209FF-0000-0000-C000-000000000046}" 
PRINT BCX_CLSID$("fu.bar.2")          '      Intentionally force an error 
PRINT BCX_CLSID$("excel.application") ' "{00024500-0000-0000-C000-000000000046}"


GETOBJECT function

Purpose:

GETOBJECT will return an instance of a COM OBJECT.

Syntax:

COMObject = GETOBJECT(Moniker AS STRING)

Return Value:

Parameters:

Example:

BCX_SHOW_COM_ERRORS(TRUE)
CLS

DIM strComputer$
DIM temp_str$
DIM objWMIService AS OBJECT
DIM colItems AS OBJECT

strComputer$ = "."
temp_str$ = "winmgmts:\\" & strComputer & "\root\cimv2"
SET objWMIService = GETOBJECT(temp_str$)

CALL PrintProcessorInfo()
CALL PrintSysInfo()
CALL PrintBiosInfo()
CALL PrintVideoInfo()
CALL PrintOpSysInfo()
CALL PrintDiskInfo()
CALL PrintNetworkInfo()
PRINT "*****************************************************"

SET objWMIService = NOTHING

PRINT " "
PRINT "Demo finished! Press any key to exit ..."
KEYPRESS

' ------------------------------------------------------------------------------------------------- 

SUB PrintProcessorInfo ()
  PRINT " "
  PRINT "*****************************************************"
  PrintItem("System Info: ", "Processors", 14, 0)
  PRINT "*****************************************************"
  SET colItems = objWMIService.ExecQuery("Select * from Win32_Processor")
  FOR Each objItem IN colItems
    temp_str$ = objItem.Name
    PrintItem("Processor Name : ", TRIM$(temp_str$), 14,0)
    temp_str$ = objItem.DeviceID
    PrintItem("Processor ID: ", TRIM$(temp_str$), 14,0)
    temp_str$ = objItem.CurrentClockSpeed
    PrintItem("Current Clock Speed: ", TRIM$(temp_str$) + " MHz", 14,0)
    temp_str$ = objItem.MaxClockSpeed
    PrintItem("Maximum Clock Speed: ", TRIM$(temp_str$) + " MHz", 14,0)
    temp_str$ = objItem.L2CacheSize
    PrintItem("L2 Cache Size: ", TRIM$(temp_str$), 14,0)
    temp_str$ = objItem.Family
    PrintItem("Processor Family: ", TRIM$(temp_str$), 14,0)
  NEXT
  SET colItems = NOTHING
END SUB

' ------------------------------------------------------------------------------------------------- 

SUB PrintSysInfo ()
  PRINT " "
  PRINT "*****************************************************"
  PrintItem("System Info: ", "General", 11, 0)
  PRINT "*****************************************************"
  DIM temp_lng AS LONG_PTR
  SET colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
  FOR Each objItem IN colItems
    temp_str$ = objItem.Name
    PrintItem("Name: ", TRIM$(temp_str$), 11,0)
    temp_str$ = objItem.manufacturer
    PrintItem("Manufacturer: ", TRIM$(temp_str$), 11,0)
    temp_str$ = objItem.model
    PrintItem("Model: ", TRIM$(temp_str$), 11,0)
    temp_lng = (VT_R4)objItem.TotalPhysicalMemory
    temp_lng = temp_lng / 1024 ' GET SIZE IN MB 
    temp_str$ = STR$(temp_lng) + " KB"
    PrintItem("Total Physical Memory: ", TRIM$(temp_str$), 11,0)
    temp_str$ = objItem.NumberOfProcessors
    PrintItem("Number Of Processors: ", TRIM$(temp_str$), 11,0)
  NEXT
  SET colItems = NOTHING
END SUB

' ------------------------------------------------------------------------------------------------- 

SUB PrintBiosInfo ()
  PRINT " "
  PRINT "*****************************************************"
  PrintItem("System Info: ", "BIOS", 12, 0)
  PRINT "*****************************************************"
  SET colItems = objWMIService.ExecQuery("Select * from Win32_bios")
  FOR Each objItem IN colItems
    temp_str$ = objItem.SerialNumber
    PrintItem("BIOS Serial Number: ", TRIM$(temp_str$), 12,0)
    temp_str$ = objItem.SMBIOSBIOSVersion
    PrintItem("SMBIOS Version: ", TRIM$(temp_str$), 12,0)
    temp_str$ = objItem.Version
    PrintItem("BIOS Version: ", TRIM$(temp_str$), 12,0)
  NEXT
  SET colItems = NOTHING
END SUB

' ------------------------------------------------------------------------------------------------- 

SUB PrintVideoInfo ()
  PRINT " "
  PRINT "*****************************************************"
  PrintItem("System Info: ", "Video Controller", 13, 0)
  PRINT "*****************************************************"
  SET colItems = objWMIService.ExecQuery("Select * from Win32_VideoController")
  FOR Each objItem IN colItems
    temp_str$ = objItem.Description
    PrintItem("Video Controller Name: ", TRIM$(temp_str$), 13,0)
    temp_str$ = objItem.VideoModeDescription
    PrintItem("Video Controller Mode: ", TRIM$(temp_str$), 13,0)
  NEXT
  SET colItems = NOTHING
END SUB

' ------------------------------------------------------------------------------------------------- 

SUB PrintOpSysInfo ()
  PRINT " "
  PRINT "*****************************************************"
  PrintItem("System Info: ", "Operating System", 10, 0)
  PRINT "*****************************************************"
  DIM temp_lng AS LONG_PTR
  SET colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
  FOR Each objItem IN colItems
    temp_str$ = objItem.Caption
    PrintItem("Operating System: ", TRIM$(temp_str$), 10,0)
    temp_str$ = objItem.Version
    PrintItem("Version: ", TRIM$(temp_str$), 10,0)
  NEXT
  SET colItems = NOTHING
END SUB

' ------------------------------------------------------------------------------------------------- 

SUB PrintDiskInfo ()
  PRINT " "
  PRINT "*****************************************************"
  PrintItem("System Info: ", "Disk Drives", 15, 0)
  PRINT "*****************************************************"
  DIM temp_dw AS DWORD_PTR
  SET colItems = objWMIService.ExecQuery("Select * From Win32_DiskDrive")
  FOR Each objItem IN colItems
    PRINT "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
    temp_str$ = objItem.DeviceID
    PrintItem("Device ID: ", TRIM$(temp_str$), 15,0)
    temp_dw = (VT_R8)objItem.size
    'temp_dw = temp_dw 
    temp_str$ = STR$(temp_dw)
    PrintItem("Size: ", TRIM$(temp_str$), 15,0)
    temp_str$ = objItem.Manufacturer
    PrintItem("Manufacturer: ", TRIM$(temp_str$), 15,0)
    temp_str$ = objItem.model
    PrintItem("Model: ", TRIM$(temp_str$), 15,0)
    PRINT "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
  NEXT
  SET colItems = NOTHING
END SUB

' ------------------------------------------------------------------------------------------------- 

SUB PrintNetworkInfo ()
  PRINT " "
  PRINT "*****************************************************"
  PrintItem("System Info: ", "Network Adapter", 9, 0)
  PRINT "*****************************************************"
  DIM temp_dw AS DWORD_PTR
  SET colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter")
  FOR Each objItem IN colItems
    temp_str$ = objItem.ProductName
    PrintItem("Network Adapter: ", TRIM$(temp_str$), 9,0)
  NEXT
  SET colItems = NOTHING
END SUB

' ------------------------------------------------------------------------------------------------- 

SUB PrintItem (Desc$, value$, color1%, color2%)
  COLOR  7,0
  PRINT Desc$;
  COLOR color1, color2
  PRINT value$
  COLOR  7,0
END SUB

For more examples of the BCX COM functions see the COM directory at the https://bcxbasiccoders.com/archives/YahooGroups/Com/ website.

Related topics: Object data type definition | CREATEOBJECT | List of all COM Interface Functions