//-Begin----------------------------------------------------------------
//-
//- Example program how to use VB.NET via PowerShell inside Java
//-
//- Author: Stefan Schnell
//-
//----------------------------------------------------------------------

//-Import---------------------------------------------------------------
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.*;
import java.io.*;

//-Class----------------------------------------------------------------
public class RunVBSharp {

  //-Constants----------------------------------------------------------
  public static final int OUTPUT_CONSOLE = 0;
  public static final int OUTPUT_WINDOW = 1;
  public static final int OUTPUT_BUFFER = 2;

  //-ReadFile-----------------------------------------------------------
  public static String ReadFile(String FileName) {

    char[] FileContent = new char[0];
    try {
      String FileNamePath = System.getProperty("user.dir") +
        FileName;
      File PSFile = new File(FileNamePath);
      FileReader PSFileRead = new FileReader(PSFile);
      FileContent = new char[(int) PSFile.length()];
      PSFileRead.read(FileContent);
      PSFileRead.close();
    }
    catch (Exception e) {
      System.out.println("Can't read file");
    }
    return String.valueOf(FileContent);
  }

  //-Main---------------------------------------------------------------
  public static void main(String[] args) {

    ComThread.InitSTA();

    ActiveXComponent PS = new ActiveXComponent("SAPIEN.ActiveXPoSHV3");
    try {

      if (PS.invoke("Init", false).changeType(Variant.VariantInt).getInt() != 0) {
        throw new RuntimeException();
      }

      if (PS.getProperty("IsPowerShellInstalled").changeType(Variant.VariantInt).getInt() == 0) {
        throw new RuntimeException();
      }

      PS.setProperty("OutputMode", OUTPUT_CONSOLE);

      //-Read Visual Basic dotNET file----------------------------------
      String VBCode = ReadFile("\\src\\de\\stschnell\\Test.vb");
      if (VBCode.length() == 0) {
        throw new RuntimeException();
      }

      //-Concatenate PowerShell command with VB code--------------------
      String PSCmd = "$VBCode = @\"\r\n";
      PSCmd += VBCode + "\r\n";
      PSCmd += "\"@;\r\n";
      PSCmd += "Add-Type -TypeDefinition $VBCode " +
        "-Language VisualBasic\r\n";
      PSCmd += "$VB = new-Object VBCode.VB\r\n";

      //-Call the VB methods on different ways--------------------------

      //-Static call with return value----------------------------------
      PSCmd += "[VBCode.VB]::Hello1()\r\n";

      //-Instance call with return value--------------------------------
      PSCmd += "$VB.Hello2(\"Stefan\")\r\n";

      //-Instance call with MessageBox----------------------------------
      PSCmd += "$VB.Hello3(\"Stefan\")\r\n";      

      PS.invoke("Execute", PSCmd);

    }

    catch (Exception e) {
      System.out.println("An exception occurred");
    }

    finally {
      ComThread.Release();
      System.exit(0);
    }

  }

}

//-End------------------------------------------------------------------