Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
Test polymorphism in reflection method invoke
  1. package y11.m04;
  2.  
  3. import java.lang.reflect.Method;
  4.  
  5. public class d28t01 {
  6.  
  7.     /**
  8.      * @param args
  9.      * @throws Exception
  10.      */
  11.     public static void main(String[] args) throws Exception {
  12.         // Test polymorphism in reflection
  13.         Object invoker = new Invoker();
  14.         Class clazz = invoker.getClass();
  15.  
  16.         Dialer p1 = new Phone("12345");
  17.         Phone p2 = new Phone("234567");
  18.         CellPhone p3 = new CellPhone("345678");
  19.  
  20.         // cause java.lang.NosuchMethodException
  21.         try {
  22.             Method m1 = clazz.getDeclaredMethod("use", p1.getClass());
  23.             m1.invoke(invoker, p1);
  24.         } catch (Exception e) {
  25.             e.printStackTrace();
  26.         }
  27.         try {
  28.             Method m2 = clazz.getDeclaredMethod("use", p2.getClass());
  29.             m2.invoke(invoker, p1);
  30.         } catch (Exception e) {
  31.             e.printStackTrace();
  32.         }
  33.         try {
  34.             Method m3 = clazz.getDeclaredMethod("use", p3.getClass());
  35.             m3.invoke(invoker, p1);
  36.         } catch (Exception e) {
  37.             e.printStackTrace();
  38.         }
  39.  
  40.         Method[] ms = clazz.getDeclaredMethods();
  41.         for (Method m : ms) {
  42.             if (m.getName().equals("use")) {
  43.                 Class[] paramTypes = m.getParameterTypes();
  44.                 if (paramTypes.length == 1 && paramTypes[0].isAssignableFrom(p1.getClass())) {
  45.                     m.invoke(invoker, p1);
  46.                 }
  47.             }
  48.  
  49.             if (m.getName().equals("use")) {
  50.                 Class[] paramTypes = m.getParameterTypes();
  51.                 if (paramTypes.length == 1 && paramTypes[0].isAssignableFrom(p2.getClass())) {
  52.                     m.invoke(invoker, p2);
  53.                 }
  54.             }
  55.  
  56.             if (m.getName().equals("use")) {
  57.                 Class[] paramTypes = m.getParameterTypes();
  58.                 if (paramTypes.length == 1 && paramTypes[0].isAssignableFrom(p3.getClass())) {
  59.                     m.invoke(invoker, p3);
  60.                 }
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66. class Invoker {
  67.     public void use(Dialer dialer) {
  68.         if (dialer != null)
  69.             dialer.call();
  70.     }
  71. }
  72.  
  73. interface Dialer {
  74.     void call();
  75. }
  76.  
  77. class Phone implements Dialer {
  78.     public String number;
  79.  
  80.     public Phone(String number) {
  81.         this.number = number;
  82.     }
  83.  
  84.     public void call() {
  85.         System.out.printf("Call %s%n", number);
  86.     }
  87. }
  88.  
  89. class CellPhone extends Phone {
  90.  
  91.     public CellPhone(String number) {
  92.         super(number);
  93.     }
  94.  
  95.     @Override
  96.     public void call() {
  97.         System.out.printf("Dial %s%n", number);
  98.     }
  99. }
Share folder info (netapi32)
  1. class MainConsole
  2. {
  3.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  4.     struct SHARE_INFO_2
  5.     {
  6.         [MarshalAs(UnmanagedType.LPWStr)]
  7.         public string shi2_netname;
  8.         public uint shi2_type;
  9.         [MarshalAs(UnmanagedType.LPWStr)]
  10.         public string shi2_remark;
  11.         public uint shi2_permissions;
  12.         public uint shi2_max_uses;
  13.         public uint shi2_current_uses;
  14.         [MarshalAs(UnmanagedType.LPWStr)]
  15.         public string shi2_path;
  16.         [MarshalAs(UnmanagedType.LPWStr)]
  17.         public string shi2_passwd;
  18.     }
  19.  
  20.     static string FormatMessage(int errCode)
  21.     {
  22.         switch (errCode)
  23.         {
  24.             case ERROR_ACCESS_DENIED: return "The user does not have access to the requested information.";
  25.             case ERROR_INVALID_LEVEL: return "The value specified for the level parameter is invalid.";
  26.             case ERROR_INVALID_PARAMETER: return "The specified parameter is invalid.";
  27.             case ERROR_MORE_DATA: return "More entries are available. Specify a large enough buffer to receive all entries.";
  28.             case ERROR_NOT_ENOUGH_MEMORY: return "Insufficient memory is available.";
  29.             case NERR_BufTooSmall: return "The supplied buffer is too small.";
  30.             case NERR_NetNameNotFound: return "The share name does not exist.";
  31.         };
  32.  
  33.         return null;
  34.     }
  35.  
  36.     [DllImport("Netapi32", CharSet = CharSet.Auto)]
  37.     static extern int NetApiBufferFree(IntPtr Buffer);
  38.  
  39.     [DllImport("Netapi32", CharSet = CharSet.Auto)]
  40.     static extern int NetShareGetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string netname, int level, ref IntPtr bufptr);
  41.  
  42.     /// <summary>
  43.     /// Retrieves the local path for the given server and share name.
  44.     /// </summary>
  45.     /// <remarks>serverName must start with \\</remarks>
  46.     static string NetShareGetPath(string serverName, string netName)
  47.     {
  48.         string path = null;
  49.         IntPtr ptr = IntPtr.Zero;
  50.  
  51.         int errCode = NetShareGetInfo(serverName, netName, 2, ref ptr);
  52.         if (errCode == NERR_Success)
  53.         {
  54.             SHARE_INFO_2 shareInfo = (SHARE_INFO_2)Marshal.PtrToStructure(ptr, typeof(SHARE_INFO_2));
  55.  
  56.             var members = from m in shareInfo.GetType().GetFields()
  57.                           select m;
  58.  
  59.             foreach (var m in members)
  60.             {
  61.                 Console.WriteLine("{0}={1}", m.Name, m.GetValue(shareInfo));
  62.             }
  63.  
  64.  
  65.             path = shareInfo.shi2_path;
  66.             NetApiBufferFree(ptr);
  67.         }
  68.         else
  69.             Console.WriteLine(FormatMessage(errCode));
  70.  
  71.         return path;
  72.     }
  73.  
  74.     /// <summary>
  75.     /// The Main method is the entry point of the program, where the program control starts and
  76.     /// ends.
  77.     /// </summary>
  78.     /// <param name="args"></param>
  79.     /// <returns></returns>
  80.     [STAThread]
  81.     static int Main(string[] args)
  82.     {
  83.         Console.WriteLine("path=" + NetShareGetPath(@"\\s3t21", "檔案測試區"));
  84.  
  85.         Console.Read();
  86.         return 0;
  87.     }
  88.  
  89.     const int ERROR_ACCESS_DENIED = 5;
  90.     const int ERROR_INVALID_LEVEL = 124; // unimplemented level for info
  91.     const int ERROR_INVALID_PARAMETER = 87;
  92.     const int ERROR_MORE_DATA = 234;
  93.     const int ERROR_NOT_ENOUGH_MEMORY = 8;
  94.     const int NERR_BufTooSmall = 2123; // The API return buffer is too small.
  95.     const int NERR_NetNameNotFound = 2310; // This shared resource does not exist.
  96.     const int NERR_Success = 0;
  97.  
  98. } // class MainConsole
http://pinvoke.net/default.aspx/netapi32.NetShareGetInfo
Get share folders from wmi
  1. ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from win32_share");
  2. foreach (ManagementObject share in searcher.Get())
  3. {
  4.     Console.WriteLine("==============");
  5.     ManagementBaseObject baseObj = share as ManagementBaseObject;
  6.  
  7.     PropertyDataCollection.PropertyDataEnumerator en = share.Properties.GetEnumerator();
  8.     while (en.MoveNext())
  9.     {
  10.         Console.WriteLine(en.Current.Name + "=" + en.Current.Value);
  11.     }
  12. }
Web based file manager
  1. //KeyNumber : 102(int)
  2. //ProductName : ad(string)
  3. //ProductVersion : ad1.0.2.1011010(string)
  4. //LicenseCount : 9999(int)
  5. //UserCount : 9999(int)
  6. Cryptor cryptor = new Cryptor();
  7. string key = HexEncoding.ToString(cryptor.EncryptString("102:ad:ad1.0.2.1011010:9999:9999"));
Tick
Start tick
Java Javascript .net
Tick = 0 (GMT+0) 1970/01/01 00:00:00 1970/01/01 00:00:00 1601/01/01 00:00:00
sec/tick 1000 1000 10000000
Convertion
tick=0\to tick Java Javascript .net
Java x 0 621356256000000000
Javascript 0 x 621356256000000000
.net -11644473600000 -11644473600000 x
  1. // Convert c# date to javascript
  2. var ticks = (DateTime.Now.Ticks - 621356256000000000) / 10000;
  1. // Convert javascript date to c#
  2. var ticks = (((new Date()).getTime() * 10000) + 621355968000000000);