Tag Archives: di api

SAP:Unable to cast COM object of type ‘System.__ComObject’ to interface type ‘SAPbobsCOM.ICompany’

SAP Business One DI API

I kept getting the above error when attempting to generate documents on a thread other than the main thread. Here’s the error message in full:

Unable to cast COM object of type ‘System.__ComObject’ to interface type ‘SAPbobsCOM.ICompany’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{3BA8DAED-5B33-4CE4-A4B8-B4308D86E524}’ failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
 
[quads id=1]

Turns out that running the following command on the client machine did the trick:

C:\SAP\SAP Business One DI API\DI API 88>regsvr32 SAPbobsCOM88.dll

What was weird here was that previous calls to the COM object on the main thread work fine. Any subsequent calls on a different thread threw the exception.

Incidentally, I have also spotted this error when using some 3rd party (and SAP – Certified) add-ons. In all cases, the above DLL registration command resolved the issue. Your mileage may vary, however.

For more information on the SAP Business One DI API, check out the following link over at the SBO website:

http://scn.sap.com/docs/DOC-7722

SAP Business One Connection

SAPbobsCOM Company

In the first of (hopefully) many (providing I keep the momentum going) posts regarding coding, I have documented the VB.NET code required to connect and return a SAP Business One Company. So here we go.

    Private Function Company(server As String, userName As String, SecurePW As Security.SecureString, _
                             companyDB As String, dbUsername As String, dbPassword As String, _
                             ByRef errorMsg As String) As SAPbobsCOM.Company

        Company = New SAPbobsCOM.Company

        ' Get the secure password 
        Dim ptr As IntPtr = Runtime.InteropServices.Marshal.SecureStringToBSTR(SecurePW)

        ' Set up the connection to SAP 
        Dim returnCode, errorCode As Integer
        With Company
            .Server = server
            .CompanyDB = companyDB
            .UserName = userName
            .Password = Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr)
            .language = SAPbobsCOM.BoSuppLangs.ln_English
            .DbUserName = dbUsername
            .DbPassword = dbPassword
            .UseTrusted = False
            .DbServerType = SAPbobsCOM.BoDataServerTypes.dst_MSSQL2008

        End With

        ' Try and connect to the database 

        returnCode = Company.Connect
        If returnCode = 0 AndAlso Company.Connected = True Then
            ' Success! 
        Else
            Company.GetLastError(errorCode, errorMsg)
            If Company.Connected Then Company.Disconnect()
            Company = Nothing
        End If

    End Function

We’re assuming that we are connecting to a SQL Server 2008 instance. Also, the SAP User password is passed in via a SecureString for maximum security. I’ll post the code for a login form that exposes the SecureString password at a later date.