home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / COM / CustomMarshal / client / CustSample.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  11.1 KB  |  428 lines

  1. //=========================================================================
  2. // A simple Java console app that can be run using JVIEW. This app
  3. // exercises the sample hook classes by passing them down to COM and
  4. // from COM back to Java, in various ways.
  5. //
  6. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  7. //
  8. //=========================================================================
  9.  
  10.  
  11.  
  12. import java.awt.Point;
  13. import java.awt.Rectangle;
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20. class CustSample
  21. {
  22.     static ICustSample obj;
  23.  
  24.  
  25.  
  26.     public static void main(String[] args) {
  27.  
  28.         CCustSample cobj;
  29.  
  30.         startuptests();
  31.  
  32.         try {
  33.             cobj = new CCustSample();
  34.         } catch (Throwable t) {
  35.             System.out.println(t);
  36.             System.out.println("Could not instantiate CCustSample(). Likely causes:");
  37.             System.out.println("  1. CustSample.dll not registered. Try \"regsvr32 CustSample.dll\".");
  38.             System.out.println("  2. CCustSample.class not found. Check classpath.");
  39.             throw new RuntimeException("Quitting.");
  40.         }
  41.  
  42.         try {
  43.           obj = (ICustSample) cobj;
  44.         } catch (Throwable t) {
  45.             System.out.println(t);
  46.             System.out.println("Could not cast to ICustSample. Likely causes:");
  47.             System.out.println("  1. ICustSample.class not found. Check classpath.");
  48.             System.out.println("  2. One or more *Marshaler.class's not found. Check classpath.");
  49.             System.out.println("  3. One or more *Marshaler.dll's not found. Check path.");
  50.             System.out.println("  4. Installed version of MSJAVA does not support custom type marshaling.");
  51.  
  52.             throw new RuntimeException("Quitting.");
  53.         }
  54.  
  55.  
  56.         DoFixedPt_JavaToCom();
  57.         obj.PassFixedPtsFromComToJava(new callbacks());
  58.  
  59.         DoMyVariant_JavaToCom();
  60.         obj.PassMyVariantFromComToJava(new callbacks());
  61.  
  62.  
  63.         DoPoint_JavaToCom();
  64.         obj.PassPointFromComToJava(new callbacks());
  65.  
  66.         DoRect_JavaToCom();
  67.         obj.PassRectFromComToJava(new callbacks());
  68.  
  69.         DoAnsi_JavaToCom();
  70.         obj.PassAnsiFromComToJava(new callbacks());
  71.      }
  72.  
  73.  
  74.  
  75.     public static void DoAnsi_JavaToCom()
  76.     {
  77.         {
  78.             obj.PassPStrIn("If you can read this, C PassPStrIn works.");
  79.         }
  80.         {
  81.             String s = obj.PassPPStrOutRetVal();
  82.             println(s);
  83.         }
  84.         {
  85.             String as[] = {"If you can read this, C PassPPStrIn works."};
  86.             obj.PassPPStrIn(as);
  87.         }
  88.         {
  89.             String as[] = {null};
  90.             obj.PassPPStrOut(as);
  91.             println(as[0]);
  92.         }
  93.         {
  94.             String as[] = {"If you can read this, 'in' of C pass PPStrInOut works"};
  95.             obj.PassPPStrInOut(as);
  96.             println(as[0]);
  97.         }
  98.     }
  99.  
  100.     public static void DoRect_JavaToCom()
  101.     {
  102.  
  103.         {
  104.            Rectangle r = new Rectangle(5,6,7,8);
  105.            println(r);
  106.            obj.PassRectIn(r);
  107.         }
  108.  
  109.         {
  110.             Rectangle r = obj.PassPRectOutRetVal();
  111.             println(r);
  112.         }
  113.  
  114.         {
  115.            Rectangle r = new Rectangle(20,30,40,50);
  116.            println(r);
  117.            obj.PassPRectIn(r);
  118.         }
  119.  
  120.         {
  121.             Rectangle ar[] = {null};
  122.             obj.PassPRectOut(ar);
  123.             println(ar[0]);
  124.         }
  125.  
  126.         {
  127.             Rectangle ar[] = {new Rectangle(1,1,1,1)};
  128.             println(ar[0]);
  129.             obj.PassPRectInOut(ar);
  130.             println(ar[0]);
  131.         }
  132.  
  133.         {
  134.             Rectangle r = obj.PassPPRectOutRetVal();
  135.             println(r);
  136.         }
  137.  
  138.         {
  139.             Rectangle ar[] = {new Rectangle(5,5,5,5)};
  140.             println(ar[0]);
  141.             obj.PassPPRectIn(ar);
  142.         }
  143.         {
  144.             Rectangle ar[] = {null};
  145.             obj.PassPPRectOut(ar);
  146.             println(ar[0]);
  147.         }
  148.         {
  149.            Rectangle ar[] = {new Rectangle(7,7,7,7)};
  150.            obj.PassPPRectInOut(ar);
  151.            println(ar[0]);
  152.         }
  153.     }
  154.  
  155.  
  156.     public static void DoPoint_JavaToCom()
  157.     {
  158.         {
  159.             Point p = new Point(-1,-1);
  160.             obj.PassPPointOut(p);
  161.             println(p);
  162.         }
  163.         {
  164.            Point p = new Point(10,11);
  165.            obj.PassPPointInOut(p);
  166.            println(p);
  167.         }
  168.     }
  169.  
  170.     public static void DoFixedPt_JavaToCom()
  171.     {
  172.  
  173.         //HRESULT PassFixedPtIn([in] FIXED fp);
  174.         obj.PassFixedPtIn(4.5);
  175.  
  176.         //HRESULT PassPFixedPtOutRetVal([out,retval] FIXED *pfp);
  177.         System.out.println(obj.PassPFixedPtOutRetVal());
  178.  
  179.         //HRESULT PassPFixedPtIn([in] FIXED *pfp);
  180.         obj.PassPFixedPtIn(8.5);
  181.  
  182.  
  183.         //HRESULT PassPFixedPtOut([out] FIXED *pfp);
  184.         {
  185.             double ad[] = {0.0};
  186.             obj.PassPFixedPtOut(ad);
  187.             System.out.println(ad[0]);
  188.         }
  189.  
  190.         //HRESULT PassPFixedPtInOut([out] FIXED *pfp);
  191.         {
  192.             double ad[] = {8.7};
  193.             System.out.println("Before: " + ad[0]);
  194.             obj.PassPFixedPtInOut(ad);
  195.             System.out.println("After:  " + ad[0]);
  196.         }
  197.  
  198.  
  199.     }
  200.  
  201.     public static void DoMyVariant_JavaToCom()
  202.     {
  203.         obj.PassMyVariantIn("If you can read this, PassMyVariantIn succeeded.");
  204.  
  205.         System.out.println(obj.PassPMyVariantOutRetVal());
  206.  
  207.         obj.PassPMyVariantIn("If you can read this, PassPMyVariantIn succeeded.");
  208.  
  209.         {
  210.             String as[] = {null};
  211.             obj.PassPMyVariantOut(as);
  212.             System.out.println(as[0]);
  213.         }
  214.  
  215.         {
  216.             String as[] = {"If you can read this, the 'in' part of PassPMyVariantInOut works."};
  217.             obj.PassPMyVariantInOut(as);
  218.             System.out.println(as[0]);
  219.         }
  220.     }
  221.  
  222.     private static void println(Object o) { System.out.println(o); }
  223.     private static void println(double o) { System.out.println(o); }
  224.     private static void println(int o)    { System.out.println(o); }
  225.  
  226.     private static void startuptests()
  227.     {
  228.     }
  229.  
  230.  
  231.  
  232. }
  233.  
  234.  
  235.  
  236. class callbacks implements ICustSample
  237. {
  238.  
  239.  
  240.     public void PassFixedPtIn(double fp)
  241.     {
  242.         println("Java PassFixedPtIn called: " + fp);
  243.     }
  244.   
  245.     public double PassPFixedPtOutRetVal()
  246.     {
  247.         println("Java PassPFixedPtOutRetVal called. Returning 8.5");
  248.         return 8.5;
  249.     }
  250.  
  251.     public void PassPFixedPtIn(double pfp)
  252.     {
  253.         println("Java PassFixedPtIn called: " + pfp);
  254.     }
  255.   
  256.     public void PassPFixedPtOut(double[] pfp)
  257.     {
  258.         pfp[0] = 3.25;
  259.         println("Java PassPFixedPtOut called. Returning " + pfp[0]);
  260.     }
  261.  
  262.     public void PassPFixedPtInOut(double[] pfp)
  263.     {
  264.         println("Java PassPFixedPtInOut called. Got " + pfp[0] + ". Adding 2.0");
  265.         pfp[0] += 2.0;
  266.     }
  267.   
  268.     public void PassFixedPtsFromComToJava(ICustSample pCustSample)
  269.     {
  270.         throw new RuntimeException("Don't call me.");
  271.     }
  272.  
  273.     public void PassMyVariantIn(String v)
  274.     {
  275.         println("Java PassMyVariantIn called: " + v);
  276.     }
  277.  
  278.     public String PassPMyVariantOutRetVal()
  279.     {
  280.         println("Java PassPMyVariantOutRetVal called.");
  281.         return "If you can read this, Java PassPMyVariantOutRetVal worked."; 
  282.     }
  283.   
  284.     public void PassPMyVariantIn(String pv)
  285.     {
  286.         println("Java PassPMyVariantIn called: " + pv);
  287.     }
  288.   
  289.     public void PassPMyVariantOut(String[] pv)
  290.     {
  291.         println("Java PassPMyVariantOut called.");
  292.         pv[0] = "If you can read this, Java PassPMyVariantOut worked."; 
  293.     }
  294.   
  295.     public void PassPMyVariantInOut(String[] pv)
  296.     {
  297.         println("Java PassPMyVariantInOut called: " + pv[0]);
  298.         pv[0] = "If you can read this, the 'out' part of Java PassPMyVariantInOut worked."; 
  299.     }
  300.  
  301.     public void PassMyVariantFromComToJava(ICustSample pCustSample)
  302.     {
  303.         throw new RuntimeException("Don't call me.");
  304.     }
  305.  
  306.     public void PassPPointOut(Point p)
  307.     {
  308.         println("Java PassPPointOut called. Returning [5,6].");
  309.         p.x = 5;
  310.         p.y = 6;
  311.     }
  312.  
  313.     public void PassPPointInOut(Point p)
  314.     {
  315.         println("Java PassPPointInOut called (" + p + "). Translating x&y by 3.");
  316.         p.x += 3;
  317.         p.y += 3;
  318.     }
  319.  
  320.     public void PassPointFromComToJava(ICustSample pCustSample)
  321.     {
  322.         throw new RuntimeException("Don't call me.");
  323.     }
  324.  
  325.     public void PassRectIn(java.awt.Rectangle r)
  326.     {
  327.         println("Java PassRectIn called: " + r);
  328.     }
  329.  
  330.   
  331.     public java.awt.Rectangle PassPRectOutRetVal()
  332.     {
  333.         Rectangle r = new Rectangle(6,7,8,9);
  334.         println("Java PassPRectOutRetVal called. Returning " + r);
  335.         return r;
  336.     }
  337.   
  338.     public void PassPRectIn(java.awt.Rectangle pr)
  339.     {
  340.        println("Java PassPRectIn called: " + pr);
  341.     }
  342.  
  343.     public void PassPRectOut(java.awt.Rectangle[] pr)
  344.     {
  345.         Rectangle r = new Rectangle(16,17,18,19);
  346.         println("Java PassPRectOut called. Returning " + r);
  347.         pr[0] = r;
  348.     }
  349.   
  350.     public void PassPRectInOut(java.awt.Rectangle[] pr)
  351.     {
  352.         println("Java PassPRectInOut called: " + pr[0]);
  353.         Rectangle r = new Rectangle(26,27,28,29);
  354.         println("Returning " + r);
  355.         pr[0] = r;
  356.     }
  357.   
  358.     public java.awt.Rectangle PassPPRectOutRetVal()
  359.     {
  360.         Rectangle r = new Rectangle(36,37,38,39);
  361.         println("Java PassPPRectOutRetVal called. Returning " + r);
  362.         return r;
  363.     }
  364.   
  365.     public void PassPPRectIn(java.awt.Rectangle[] ppr)
  366.     {
  367.         println("Java PassPPRectIn called: " + ppr[0]);
  368.     }
  369.   
  370.     public void PassPPRectOut(java.awt.Rectangle[] ppr)
  371.     {
  372.         Rectangle r = new Rectangle(46,47,48,49);
  373.         println("Java PassPPRectOut called. Returning " + r);
  374.         ppr[0] = r;
  375.     }
  376.     public void PassPPRectInOut(java.awt.Rectangle[] ppr)
  377.     {
  378.         println("Java PassPPRectInOut called: + " + ppr[0]);
  379.         Rectangle r = new Rectangle(56,57,58,59);
  380.         println("Returning " + r);
  381.         ppr[0] = r;
  382.  
  383.     }
  384.  
  385.     public void PassRectFromComToJava(ICustSample pCustSample)
  386.     {
  387.         throw new RuntimeException("Don't call me.");
  388.     }
  389.  
  390.     public void PassPStrIn(String pstr)
  391.     {
  392.         println("Java PassPStrIn called: " + pstr);
  393.     }
  394.  
  395.     public String PassPPStrOutRetVal()
  396.     {
  397.         return "If you can read this, Java PassPStrOutRetVal worked.";
  398.     }
  399.  
  400.     public void PassPPStrIn(String[] ppstr)
  401.     {
  402.         println("Java PassPPStrIn called: " + ppstr[0]);
  403.     }
  404.  
  405.     public void PassPPStrOut(String[] ppstr)
  406.     {
  407.         ppstr[0] = "If you can read this, Java PassPStrOut worked.";
  408.     }
  409.  
  410.     public void PassPPStrInOut(String[] ppstr)
  411.     {
  412.         println("Java PassPPStrInOut called: " + ppstr[0]);
  413.         ppstr[0] = "If you can read this, 'out' part of Java PassPStrInOut worked.";
  414.     }
  415.  
  416.  
  417.     public void PassAnsiFromComToJava(ICustSample pCustSample)
  418.     {
  419.         throw new RuntimeException("Don't call me.");
  420.     }
  421.  
  422.     private void println(Object o) { System.out.println(o); }
  423.     private void println(double o) { System.out.println(o); }
  424.     private void println(int o)    { System.out.println(o); }
  425.  
  426.  
  427. }
  428.