1 package com4j; 2 3 import java.io.File ; 4 import java.lang.reflect.Proxy ; 5 import java.net.URL ; 6 7 15 public abstract class COM4J { 16 private COM4J() {} 18 36 public static<T extends Com4jObject> 37 T createInstance( Class <T> primaryInterface, GUID clsid ) throws ComException { 38 return createInstance(primaryInterface,clsid.toString()); 39 } 40 41 61 public static<T extends Com4jObject> 62 T createInstance( Class <T> primaryInterface, String clsid ) throws ComException { 63 64 return new CreateInstanceTask<T>(clsid,primaryInterface).execute(); 66 } 67 68 private static class CreateInstanceTask<T extends Com4jObject> extends Task<T> { 69 private final String clsid; 70 private final Class <T> intf; 71 72 public CreateInstanceTask(String clsid, Class <T> intf) { 73 this.clsid = clsid; 74 this.intf = intf; 75 } 76 77 public T call() { 78 GUID iid = getIID(intf); 79 return Wrapper.create( intf, Native.createInstance(clsid,iid.v[0],iid.v[1]) ); 80 } 81 } 82 83 96 public static GUID getIID( Class <? extends Com4jObject> _interface ) { 97 IID iid = _interface.getAnnotation(IID.class); 98 if(iid==null) 99 throw new IllegalArgumentException (_interface.getName()+" doesn't have @IID annotation"); 100 return new GUID(iid.value()); 101 } 102 103 109 public static Com4jObject loadTypeLibrary( final File typeLibraryFile ) { 110 return new Task<Com4jObject>() { 111 public Com4jObject call() { 112 return Wrapper.create( 113 Native.loadTypeLibrary(typeLibraryFile.getAbsolutePath())); 114 } 115 }.execute(); 116 } 117 118 121 public static final GUID IID_IUnknown = new GUID("{00000000-0000-0000-C000-000000000046}"); 122 123 126 public static final GUID IID_IDispatch = new GUID("{00020400-0000-0000-C000-000000000046}"); 127 128 129 130 142 public static void addListener( ComObjectListener listener ) { 143 ComThread.get().addListener(listener); 144 } 145 146 157 public static void removeListener( ComObjectListener listener ) { 158 ComThread.get().removeListener(listener); 159 } 160 161 static int queryInterface( int ptr, GUID iid ) { 162 return Native.queryInterface(ptr,iid.v[0],iid.v[1]); 163 } 164 165 static Wrapper unwrap( Com4jObject obj ) { 166 if( obj instanceof Wrapper ) 167 return (Wrapper)obj; 168 else 169 return (Wrapper)Proxy.getInvocationHandler(obj); 170 } 171 172 static int getPtr( Com4jObject obj ) { 174 if(obj==null) return 0; 175 return unwrap(obj).getPtr(); 176 } 177 178 static { 179 loadNativeLibrary(); 180 Native.init(); 181 } 182 183 private static void loadNativeLibrary() { 184 try { 185 System.loadLibrary("com4j"); 188 return; 189 } catch( Throwable t ) { 190 ; 191 } 192 193 URL res = COM4J.class.getClassLoader().getResource("com4j/COM4J.class"); 195 String url = res.toExternalForm(); 196 if(url.startsWith("jar://")) { 197 int idx = url.lastIndexOf('!'); 198 String filePortion = url.substring(6,idx); 199 if(filePortion.startsWith("file://")) { 200 File jarFile = new File (filePortion.substring(7)); 201 File dllFile = new File (jarFile.getParentFile(),"com4j.dll"); 202 System.load(dllFile.getPath()); 203 return; 204 } 205 } 206 207 throw new UnsatisfiedLinkError ("Unable to load com4j.dll"); 208 } 209 } 210 | Popular Tags |