Plex 2E

 View Only
  • 1.  Model API Java / C++ / C#

    Posted Mar 21, 2016 03:33 AM

    Just for the record and if someone wants to create a Model API Eclipse plugin successfully called model api via all the supported desktop variants including java

     

    StellaTools.PlexAPILibViaSrc.IPlexAPI.GetPID

    Untitled.png

    Java

    import com.jacob.activeX.ActiveXComponent;
    import com.jacob.com.Dispatch;
    import com.jacob.com.LibraryLoader;
    import com.jacob.com.Variant;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import javax.swing.*;
    
    
                double PID = 0;
                String libFile = System.getProperty("os.arch").equals("amd64") ? "C:\\JavaTraining\\jacob-1.18\\jacob-1.18\\jacob-1.18-x64.dll" : "C:\\JavaTraining\\jacob-1.18\\jacob-1.18\\jacob-1.18-x86.dll";
                try {
                    InputStream inputStream = new FileInputStream(libFile);
                    File temporaryDll = File.createTempFile("JACOB_PlexAPILib", ".dll");
                    FileOutputStream outputStream = new FileOutputStream(temporaryDll);
                    byte[] array = new byte[8192];
                    for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)) {
                        outputStream.write(array, 0, i);
                    }
                    outputStream.close();
                    System.setProperty(LibraryLoader.JACOB_DLL_PATH, temporaryDll.getAbsolutePath());
                    LibraryLoader.loadJacobLibrary();
                    ActiveXComponent myInstanceOfPlexAPI = new ActiveXComponent("PlexAPI.Server");
                    Variant processId = new Variant(0, true);
                    Dispatch.call(myInstanceOfPlexAPI, "GetPID", processId);
                    processId.changeType((short) 3);
                    PID = processId.getInt();
             &(1:).setValue(new java.lang.Double(PID));
                    temporaryDll.deleteOnExit();
                } catch (Exception e) {
                    JOptionPane.showMessageDialog
                    (null,"Exception: " + e.getMessage(),"Exception calling PlexAPI.GetPID", JOptionPane.ERROR_MESSAGE);
                }
    

     

    C++

    #include "import.h"
    {
      CoInitialize(NULL);
      long objref; 
      HRESULT result;
      // BSTR text = 0;
    
    
      try {
      // Instantiate AND IF NO PLEX.EXE EXISTS IT STARTS ONE (NOT THE BEHAVIOUR WE WANT)
      IPlexAPIPtr ModelAPI(__uuidof(PlexAPI));
      // call method IPlexAPI.GetPID
        result = ModelAPI->GetPID(&objref);
      &(1:) = objref; 
      // result = ModelAPI->GetErrorMessage(result, &text);
      result = ModelAPI->Release(); 
      }
      catch (_com_error e) { // Such as "The RPC server is unavaliable"
        AfxMessageBox(e.ErrorMessage());
      }
      CoUninitialize();
    }
    

     

    C#

    using PlexAPILib;
    using System.Windows.Forms;
    
    
    try {
    int PID = 0;
    //Instantiate a version of the Model API in memory and refer to it as "ModelAPI" from now on.
    PlexAPI ModelAPI = new PlexAPILib.PlexAPI(); 
    ModelAPI.GetPID(ref PID);
    &(1:).assign(new ObLongDblFld(PID));
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
    // [PLEX0059] Error while invoking the function Local.STGetPID_ObFnc.
    // Unable to cast COM object of type 'PlexAPILib.PlexAPIClass' to interface type 'PlexAPILib.IPlexAPI'. 
    // This operation failed because the QueryInterface call on the COM component for the interface with IID '{AB540011-90A2-11D4-A84A-0008C78C4894}' 
    // failed due to the following error: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA).
    MessageBox.Show("Exception: " + ex.Message, "Exception calling PlexAPI.GetPID", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    
    
    //Console.WriteLine(ex.Message);