14 Replies Latest reply on Apr 7, 2009 5:34 PM by mweiss

    Opeing a PDI file in processbook in .Net

    arash.khoddamy

      Hi

       

      I have tried the following but they all give me

       

      “Error Hresult E_Fail has been returned from a call to a COM component.”

       

      pbApplication = CType(application, PBObjLib.Application) 
      m_App = pbApplication.Application
      pbApplication.ActiveProcBook.Displays.Open(DisplayPath, True)
      m_App.Displays.Open(DisplayPath, True)
      m_App.Application.Displays.Open(DisplayPath, True)
      m_Dis.Application.Displays.Open(DisplayPath, True)
      pbApplication.Displays.Open(DisplayPath, True)
      m_App.ActiveProcBook.Displays.Open(DisplayPath, True)
      pbApplication.ProcBooks.Open(DisplayPath, True

       

      So I ended up using

       

      System.Diagnostics.Process.Start(DisplayPath)

       

      This works but its not very nice. Can anyone tell me what I am doing wrong?

        • Re: Opeing a PDI file in processbook in .Net

          For your addin are all the required DLLs present?  Seen this before (non-PI related) and it was because I was missing a DLL.

          • Re: Opeing a PDI file in processbook in .Net
            mweiss

            Try sending a literal one or zero for True and False for the last parameter of m_App.Displays.Open. I suspect the the VB.Net to COM translation of that parameter is not working as we would expect. Something like:

             

            m_App.Display.Open(DisplayPath, 1)

             

            Maybe try a 0 there as well, just to see if that helps.

              • Re: Opeing a PDI file in processbook in .Net
                arash.khoddamy

                Hi everyone first let me thank you for your help.

                 

                Also i just wanted to let you know that I tried putting 1 and 0 for true and false but it did not work.

                 

                As for the dlls as i far as i know i have all the ones that i need. Here are the OSI references that i use

                 

                Extensibility
                PBObjLib
                PiSDK
                PISDKCommon
                PITimerServer
                PISDKCommon
                PISDK.Controls.Pisdkctrldlg

                 

                Is there a Special dll that i need ?

                  • Re: Opeing a PDI file in processbook in .Net

                    OK - are you using templates from Download on vCampus?  I notice they are only intended for VS2005 - there may be hidden difference for VS2008.

                     

                    Also, do you have a reference to PBSymLib?  Have the references to ProcessBook been resolved correctly for your addin?

                      • Re: Opeing a PDI file in processbook in .Net
                        dhollebeek

                        If I read your code right ...

                         

                        Your first line is "pbApplication = CType(application, PBObjLib.Application)".  That takes a variable "application" (which we don't see where it comes from) and converts it to a PB application object (even if it isn't one).  You have to be careful because there are all sorts of "application" objects wandering around in various libraries.  To make things more confusing, in VB at least, PBObjLib.Application is both a type name and a global variable (I don't remember whether C# has the same confusion or not).  Try changing that line to "pbApplication = PBObjLib.Application".  If that doesn't work, there's something wrong with the references or types of variables (like pbApplication)

                         

                        .. just a guess ... this may be way off ...

                          • Re: Opeing a PDI file in processbook in .Net
                            arash.khoddamy

                            Let me see if I can answer all the questions below.
                            I got these templates from OSI Tech support over a year go. They look to be the same as the ones in vCampus.

                             

                            pbApplication is the same variable as m_App in the template i just changed the variable name 
                              Public pbApplication As PBObjLib.Application

                             

                             
                            When I change the line 
                              pbApplication = CType(application, PBObjLib.Application)
                            to 
                              pbApplication = PBObjLib.Application

                            I get the Error 'Application' is a type in 'PBObjLib' and cannot be used as an expression.

                             

                            I don’t know why this is not working because everything else seems to work. I can

                            • Get a display from PB.
                            • Count the Open displays.
                            • Get a list of open displays and switch between them..

                            Thanks in advance

                              • Re: Opeing a PDI file in processbook in .Net
                                dhollebeek

                                Oops, I forgot that PBObjlib.Application works in VBA but not VB (Mike reminded me).

                                 

                                If you are using the template that I think you are using, there should be a global variable called "pbapp" in the Connect module that gets set to the PBObjLib application when PB connects to your add-in.  So, try:

                                 

                                pbApplication = pbapp

                                 

                                or just use pbapp instead of pbApplication ...

                                  • Re: Opeing a PDI file in processbook in .Net
                                    arash.khoddamy

                                    I don't see a Pbapp

                                     

                                    There is m_app variable which is the same as my Pbapplication ( i just changed the name) that’s the same variable that is used in the OnConnection procedure but it uses the Ctype conversion that is not recommended

                                     

                                    Dim m_App As PBObjLib.Application

                                    Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection

                                      On Error GoTo error_handler
                                      m_App = CType(application, PBObjLib.Application)

                                    ...

                                      • Re: Opeing a PDI file in processbook in .Net
                                        mweiss

                                        You must use the application object passed to the add-in via the OnConnection event. Since this variable in typed as an Object that conversion with CType is required. This is exactly what the template we provide does and is not only fine, but required. We can't pass a more strongly typed parameter since the interfaces between add-ins and ProcessBook are actually a Microsoft standard add-in system (IExtensiblity, part of VBA) so it isn't aware of the specifics of OSIsoft applications.

                                         

                                        I created a new add-in based on the "toolbar" VB.NET template:

                                        Public Sub OnConnection(ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection

                                        On Error GoTo error_handler

                                        m_App = CType(application, PBObjLib.Application)


                                        ' New line I added!
                                        m_App.Displays.Open("e:\share\status.pdi", 1)

                                               
                                        'Region: Command Bar
                                        'this code attaches the add-in to the add-in command bar
                                        Dim bExist As Boolean
                                        bExist = False

                                        ...

                                        This successfully opened status.pdi at the point the add-in was loaded. Using m_App inside any method of the Connect class should be fine. Using m_App in other Classes, modules, etc of the VB.NET project should work, but I'm not an expert on VB.NET and how it handles scope to give any definitive recommendations on the best ways to do this. However, I suspect you would have to pass a reference to this variable or the entire connect class ("Me") to any other object you create.

                                         

                                        For example if you wanted the AddInDialog Form (created by the template) to use m_App you could add a property of type PBObjLib.Application to the AddInDialog class and set it to m_App in Connect's AddInClicked subroutine.

                                        ' AddIn Click Event Handler
                                        ' Displays Empty Dialog, Select Cancel to Close
                                        Public Sub AddInClicked(ByVal commandBarButton As PBObjLib.PBCommandBarButton, ByRef b1 As Boolean)
                                            Dim dlg As New AddInDialog
                                            dlg.PBApp = m_App
                                            If dlg.ShowDialog = DialogResult.Cancel Then
                                                dlg = Nothing
                                            End If
                                        End Sub

                                        You could alternatively pass this a constructor parameter, or dozens of other ways.

                                         

                                        I still suspect, however, the problem is with the two parameters you're passing to Displays.Open. Are you sure the file name you're passing is valid? Maybe try bringing up a message box to check the value? Does ProcessBook show a "Cannot Open Display" error message before the E_FAIL message?

                          • Re: Opeing a PDI file in processbook in .Net

                            Arash...found it.

                             

                            When you call Displays.Open from the "ActiveProcBook" it fails, call it directly from m_App and it works fine.  Must be a rooted thing with the automation, I will leave that one for OSI to explain...

                              • Re: Opeing a PDI file in processbook in .Net
                                dhollebeek

                                When you tried that, was there a ProcessBook (PIW file) open and active at the time?  Trying to use ActiveProcBook when a PIW is not the active window will always fail no matter what you are trying to do.

                                  • Re: Opeing a PDI file in processbook in .Net

                                    Using m_App.Displays.Open works whether there is a PIW open or not.
                                    Using m_App.ActiveProcBook.Displays.Open fails with or without a PIW open.

                                     

                                    The plot thickens...

                                      • Re: Opeing a PDI file in processbook in .Net
                                        arash.khoddamy

                                        HI everyone thanks for the responses

                                         

                                         using m_app.dispalys.open works for PDI files which is what i was trying to do.

                                         

                                        One thing i did notice thought and i should have seen it earlier is that the file that i was trying was a shortcut to a PDI file and it does not work.  As I remmeber in VBA you could open shorcuts the say way as a PDI file, is there something I have to do to open Shortcuts to PDI files

                                          • Re: Opeing a PDI file in processbook in .Net
                                            mweiss

                                            Arash Khoddamy

                                            HI everyone thanks for the responses

                                             

                                             using m_app.dispalys.open works for PDI files which is what i was trying to do.

                                             

                                            One thing i did notice thought and i should have seen it earlier is that the file that i was trying was a shortcut to a PDI file and it does not work.  As I remmeber in VBA you could open shorcuts the say way as a PDI file, is there something I have to do to open Shortcuts to PDI files

                                             

                                            I'm not aware of any support for Windows shortcuts in ProcessBook outside the File Open dialogs (for example the window the menu command File > Open displays). Dave and I confirmed that this does not work in automation. The Button symbol does not seem to support this either.

                                             

                                            The ActiveProcBook property and the Workbook.Displays collection are both tricky to use - if there's specific questions about them could we create a new discussion thread?