import com.ms.com.*; import java.io.*; class MVariant implements Serializable { public Variant v; /** @dll.import("oleaut32.dll") */ public static native int VARIANT_UserSize(int ret[], int offset, Variant v); /** @dll.import("oleaut32.dll", ansi) */ public static native int VARIANT_UserMarshal(int ret[], byte[] buf, Variant v); /** @dll.import("oleaut32.dll", ansi) */ public static native int VARIANT_UserUnmarshal(int ret[], byte[] buf, Variant v); public MVariant() { v = new Variant(); } public MVariant(Variant theVar) { v = theVar; } public String toString() { return v.toString(); } private void writeObject(java.io.ObjectOutputStream oos) { try { Save(oos); } catch (Exception e) { e.printStackTrace(); } } private void readObject(java.io.ObjectInputStream ois) { try { Load(ois); } catch (Exception e) { e.printStackTrace(); } } public void Save(java.io.OutputStream os) throws java.io.IOException { DataOutputStream dos = new DataOutputStream(os); int l = VARIANT_UserSize(new int[1],0,v); byte buf[] = new byte[l]; VARIANT_UserMarshal(new int[1], buf, v); dos.writeInt(l); dos.write(buf); } public void Load(java.io.InputStream is) throws java.io.IOException { DataInputStream dis = new DataInputStream(is); int l = dis.readInt(); byte[] ba = new byte[l]; dis.read(ba); v = new Variant(); VARIANT_UserUnmarshal(new int[1], ba, v); } public static void main(String[] args) throws Exception { Variant v1 = new Variant("hi"); MVariant vs1 = new MVariant(v1); System.out.println(vs1); SafeArray sa = new SafeArray(Variant.VariantVariant, 2, 2); sa.setDouble(0,0,0.234); sa.setDouble(1,0,1.234); sa.setDouble(1,1,1.345); sa.setDouble(0,1,0.345); Variant v2 = new Variant(sa); MVariant vs2 = new MVariant(v2); System.out.print("[" + vs2.v.toSafeArray().getVariant(0,0) + " "); System.out.print(vs2.v.toSafeArray().getVariant(1,0) + " "); System.out.print(vs2.v.toSafeArray().getVariant(0,1) + " "); System.out.println(vs2.v.toSafeArray().getVariant(1,1) + "]"); FileOutputStream fos = new FileOutputStream("foo.foo"); vs1.Save(fos); vs2.Save(fos); fos.close(); MVariant vl1 = new MVariant(); MVariant vl2 = new MVariant(); FileInputStream fis = new FileInputStream("foo.foo"); vl1.Load(fis); vl2.Load(fis); System.out.println(vl1); System.out.print("[" + vl2.v.toSafeArray().getVariant(0,0) + " "); System.out.print(vl2.v.toSafeArray().getVariant(1,0) + " "); System.out.print(vl2.v.toSafeArray().getVariant(0,1) + " "); System.out.println(vl2.v.toSafeArray().getVariant(1,1) + "]"); // same thing with serialization fos = new FileOutputStream("foo.ser"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(vs1); oos.writeObject(vs2); oos.close(); fos.close(); fis = new FileInputStream("foo.ser"); ObjectInputStream ois = new ObjectInputStream(fis); MVariant vss1, vss2; vss1 = (MVariant)ois.readObject(); vss2 = (MVariant)ois.readObject(); ois.close(); fis.close(); System.out.println(vss1); System.out.print("[" + vss2.v.toSafeArray().getVariant(0,0) + " "); System.out.print(vss2.v.toSafeArray().getVariant(1,0) + " "); System.out.print(vss2.v.toSafeArray().getVariant(0,1) + " "); System.out.println(vss2.v.toSafeArray().getVariant(1,1) + "]"); } }