cancel
Showing results for 
Search instead for 
Did you mean: 

SAP BUSINESS ONE DI API crashes Application

Klaus_Frick
Active Participant
0 Kudos
Establishing 2 Connections with DI API (Version FP2402 HF1) at the same time is working properly. Disconnecting each Connections is also working correct, but when you set the second Company Object to Nothing, then the App (e.g. Excel) crashes.
 
With older versions of DI API (e.g. 9.3) it was not a problem to have more than one connection working properly.
 
Does anyone have the same problem with FP2402 or with other versions?
 
Or maybe someone already has a solution for this?
 
If anyone want to try, then here some VBA code to adapt easily within e.g. Excel, Word or Access.
 
Best Regards
Klaus
 
Private Sub SAP_DIAPI_Connect_Disconnect_TEST()
    'Code more or less from SDK Documentation REFDI.chm
 
    Dim ErrorCode As Long, ErrorMessage As String
    Dim pServer As String, pCompanyDB1 As String, pCompanyDB2 As String, pSBO_User As String, pSBO_Passwort As String
    Dim pUseTrusted As Boolean, pDBServerType As Long
    Dim oCompany1 As Object, oCompany2 As Object
    
    pServer = "b1server": pUseTrusted = False: pDBServerType = 15
    pSBO_User = "manager"
    pSBO_Passwort = "1234"
            
    pCompanyDB1 = "companyDB1": pCompanyDB2 = "companyDB2"
            
    '***********************************************************
    'CONNECTION 1 TO DB1
    '***********************************************************
    Set oCompany1 = CreateObject("SAPbobsCOM.Company")
    oCompany1.UseTrusted = pUseTrusted
    oCompany1.DbServerType = pDBServerType
    oCompany1.Server = pServer
            
    oCompany1.SLDServer = "https://" & pServer & ":40000"
    oCompany1.CompanyDB = pCompanyDB1
    oCompany1.UserName = pSBO_User
    oCompany1.Password = pSBO_Passwort
    
    ErrorCode = oCompany1.Connect
    If ErrorCode <> 0 Then
        oCompany1.GetLastError ErrorCode, ErrorMessage
        ErrorMessage = oCompany1.GetLastErrorDescription()
        Debug.Print pCompanyDB1 & vbTab & "-> Not Connected : " & ErrorCode & " " & ErrorMessage
    Else
        Debug.Print pCompanyDB1 & vbTab & "-> Connected"
    End If
        
    
    '***********************************************************
    'CONNECTION 2 TO DB2
    '***********************************************************
    Set oCompany2 = CreateObject("SAPbobsCOM.Company")
    oCompany2.UseTrusted = pUseTrusted
    oCompany2.DbServerType = pDBServerType
    oCompany2.Server = pServer
            
    oCompany2.SLDServer = "https://" & pServer & ":40000"
    
    oCompany2.CompanyDB = pCompanyDB2
    
    oCompany2.UserName = pSBO_User
    oCompany2.Password = pSBO_Passwort
            
    ErrorCode = oCompany2.Connect
    If ErrorCode <> 0 Then
        oCompany2.GetLastError ErrorCode, ErrorMessage
        ErrorMessage = oCompany2.GetLastErrorDescription()
        Debug.Print pCompanyDB2 & vbTab & "-> Not Connected : " & ErrorCode & " " & ErrorMessage
    Else
        Debug.Print pCompanyDB2 & vbTab & "-> Connected"
    End If
    
    Debug.Assert False
    '***********************************************************
    'START DISCONNECTING
    '***********************************************************
    If Not oCompany1 Is Nothing Then
        oCompany1.Disconnect
        Set oCompany1 = Nothing
    End If
    
    If Not oCompany2 Is Nothing Then
        oCompany2.Disconnect
        Set oCompany2 = Nothing     '<-- here Excel crashes
    End If
    
End Sub
ANKIT_CHAUHAN
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Klaus_Frick,

Do you have Microsoft Visual Studio to test it as it is not crashing for me as you have explained?

Can it be related to Excel or word whatever you are using?

Kind regards,

ANKIT CHAUHAN

SAP Business One Support

Accepted Solutions (0)

Answers (2)

Answers (2)

Johan_H
Active Contributor

Hi Klaus,

Could you please try this:

If Not oCompany1 Is Nothing Then
   oCompany1.Disconnect
   System.Runtime.InteropServices.Marshal.ReleaseComObject(oCompany1)
   Set oCompany1 = Nothing
End If
If Not oCompany2 Is Nothing Then
   oCompany2.Disconnect
   System.Runtime.InteropServices.Marshal.ReleaseComObject(oCompany2)
   Set oCompany2 = Nothing
End If

Regards,

Johan

Klaus_Frick
Active Participant
0 Kudos

Hi Johan

Thank you for your (maybe) possible solution. Unfortunatly it is a bit tricky to use your .NET solution within VBA. But I will try to build a DLL and use that for ReleaseComObject, when I find some time. Are you thinking about a problem with Garbage Collection?

I had the possibility to try it on a FP2208 HF2 Release and there it worked without any problem. So it seems, that this is a problem within at least the current Release FP2402 HF1.

I also forgot to mention, that the sequence of disconnecting and setting to nothing does matter. If you do both in reversed order, then it does work. In older versions this did not matter...

Best Regards,

Klaus

Johan_H
Active Contributor
0 Kudos

My thought was, that (if I am not horribly mistaken) Excel also uses com. At least in my .NET application my Excel driver is com.

Also, using releaseComObject is not necessary but is recommended with .NET at least.

 

Klaus_Frick
Active Participant
0 Kudos

Hi ANKIT_CHAUHAN

At the moment, I cannot test with Visual Studio. But as it worked at least till Version FP2208 HF2, I would assume, that the problem is DI API in at least Version FP2402 HF1. I use Access (64bit) in many projects back since SBO 9.2 and never had this issue. I tested it with all Microsoft Office Apps with VBA and they all crashed.

Best Regards

Klaus