HelloWorld 善戰者,求之于勢,不責于人;故能擇人而任勢。

          知止而后有定,定而后能靜,靜而后能安,安而后能慮,慮而后能得。物有本末,事有終始。知所先后,則近道矣。

            BlogJava :: 首頁 ::  :: 聯系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 40 評論 :: 0 Trackbacks

          import java.lang.reflect.Field;
          import java.lang.reflect.Method;
          import java.util.Iterator;
          import java.util.Vector;

          public class FreeDynamicDll {
           static {
            // 首先確保這些dll文件存在
            System.load("c:/test/Decode.dll");
           }

           /**
            * 卸載已經裝載的dll
            *
            * @param dllName
            *            庫名,如Decode.dll
            */
           

           private synchronized void freeDll(String dllName) {
            try {
             ClassLoader classLoader = this.getClass().getClassLoader();
             Field field = ClassLoader.class.getDeclaredField("nativeLibraries");
             field.setAccessible(true);
             Vector<Object> libs = (Vector<Object>) field.get(classLoader);
             Iterator<Object> it = libs.iterator();
             Object o;
             while (it.hasNext()) {
              o = it.next();
              Field[] fs = o.getClass().getDeclaredFields();
              boolean hasInit = false;
              for (int k = 0; k < fs.length; k++) {
               if (fs[k].getName().equals("name")) {
                fs[k].setAccessible(true);
                String dllPath = fs[k].get(o).toString();
                if (dllPath.endsWith(dllName)) {
                 hasInit = true;
                }
               }
              }
              if (hasInit) {
               Method finalize = o.getClass().getDeclaredMethod(
                 "finalize", new Class[0]);
               finalize.setAccessible(true);
               finalize.invoke(o, new Object[0]);
               it.remove();       
               libs.remove(o);
              }
             }

            } catch (Exception e) {
             e.printStackTrace();
            }
           }

           
           public static void main(String args[]) {
            FreeDynamicDll t = new FreeDynamicDll();
            t.freeDll("Decode.dll");
           }
          }



          </script>

          posted on 2007-11-26 14:31 helloworld2008 閱讀(1314) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 滕州市| 枣强县| 连江县| 靖西县| 博野县| 桐乡市| 湟中县| 镇巴县| 麟游县| 将乐县| 中江县| 宕昌县| 成安县| 静海县| 桐城市| 大庆市| 集安市| 武乡县| 灵寿县| 九台市| 兴隆县| 莎车县| 隆子县| 江达县| 谷城县| 翼城县| 尖扎县| 大田县| 河间市| 淮滨县| 奎屯市| 衡东县| 玉树县| 陆丰市| 鹿泉市| 简阳市| 谢通门县| 恩平市| 兴文县| 伊吾县| 阜康市|