Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Trojan Server - CDDriveEvents.cs

CDDriveEvents.cs

Caricato da:
Scarica il programma completo

  1. //
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE. IT CAN BE DISTRIBUTED FREE OF CHARGE AS LONG AS THIS HEADER
  7. //  REMAINS UNCHANGED.
  8. //
  9. //  Email:  yetiicb@hotmail.com
  10. //
  11. //  Copyright (C) 2002-2003 Idael Cardoso.
  12. //
  13.  
  14. using System;
  15. using System.Runtime.InteropServices;
  16. using System.Windows.Forms;
  17.  
  18. namespace Ripper
  19. {
  20.   public class DataReadEventArgs : EventArgs
  21.   {
  22.     private byte[] m_Data;
  23.     private uint m_DataSize;
  24.     public DataReadEventArgs(byte[] data, uint size)
  25.     {
  26.       m_Data = data;
  27.       m_DataSize = size;
  28.     }
  29.     public byte[] Data
  30.     {
  31.       get
  32.       {
  33.         return m_Data;
  34.       }
  35.     }
  36.     public uint DataSize
  37.     {
  38.       get
  39.       {
  40.         return m_DataSize;
  41.       }
  42.     }
  43.   }
  44.  
  45.   public class ReadProgressEventArgs : EventArgs
  46.   {
  47.     private uint m_Bytes2Read;
  48.     private uint m_BytesRead;
  49.     private bool m_CancelRead = false;
  50.     public ReadProgressEventArgs(uint bytes2read, uint bytesread)
  51.     {
  52.       m_Bytes2Read = bytes2read;
  53.       m_BytesRead = bytesread;
  54.     }
  55.     public uint Bytes2Read
  56.     {
  57.       get
  58.       {
  59.         return m_Bytes2Read;
  60.       }
  61.     }
  62.     public uint BytesRead
  63.     {
  64.       get
  65.       {
  66.         return m_BytesRead;
  67.       }
  68.     }
  69.     public bool CancelRead
  70.     {
  71.       get
  72.       {
  73.         return m_CancelRead;
  74.       }
  75.       set
  76.       {
  77.         m_CancelRead = value;
  78.       }
  79.     }
  80.   }
  81.  
  82.   internal enum DeviceChangeEventType {DeviceInserted, DeviceRemoved};
  83.   internal class DeviceChangeEventArgs : EventArgs
  84.   {
  85.     private DeviceChangeEventType m_Type;
  86.     private char m_Drive;
  87.     public DeviceChangeEventArgs(char drive, DeviceChangeEventType type)
  88.     {
  89.       m_Drive = drive;
  90.       m_Type = type;
  91.     }
  92.     public char Drive
  93.     {
  94.       get
  95.       {
  96.         return m_Drive;
  97.       }
  98.     }
  99.     public DeviceChangeEventType ChangeType
  100.     {
  101.       get
  102.       {
  103.         return m_Type;
  104.       }
  105.     }
  106.   }
  107.   public delegate void CdDataReadEventHandler(object sender, DataReadEventArgs ea);
  108.   public delegate void CdReadProgressEventHandler(object sender, ReadProgressEventArgs ea);
  109.   internal delegate void DeviceChangeEventHandler(object sender, DeviceChangeEventArgs ea);
  110.  
  111.   internal enum DeviceType : uint
  112.   {
  113.     DBT_DEVTYP_OEM = 0x00000000,      // oem-defined device type
  114.     DBT_DEVTYP_DEVNODE = 0x00000001,  // devnode number
  115.     DBT_DEVTYP_VOLUME = 0x00000002,   // logical volume
  116.     DBT_DEVTYP_PORT = 0x00000003,     // serial, parallel
  117.     DBT_DEVTYP_NET = 0x00000004       // network resource
  118.   }
  119.  
  120.   internal enum VolumeChangeFlags : ushort
  121.   {
  122.     DBTF_MEDIA = 0x0001,          // media comings and goings
  123.     DBTF_NET   = 0x0002           // network volume
  124.   }
  125.  
  126.   [StructLayout(LayoutKind.Sequential)]
  127.   internal struct DEV_BROADCAST_HDR
  128.   {
  129.     public uint dbch_size;
  130.     public DeviceType dbch_devicetype;
  131.     uint dbch_reserved;
  132.   }
  133.  
  134.   [StructLayout(LayoutKind.Sequential)]
  135.   internal struct DEV_BROADCAST_VOLUME
  136.   {
  137.     public uint dbcv_size;
  138.     public DeviceType dbcv_devicetype;
  139.     uint dbcv_reserved;
  140.     uint dbcv_unitmask;
  141.     public char[] Drives
  142.     {
  143.       get
  144.       {
  145.         string drvs = "";
  146.         for (char c = 'A'; c <= 'Z'; c++)
  147.         {
  148.           if ( (dbcv_unitmask & (1 << (c - 'A'))) != 0 )
  149.           {
  150.             drvs += c;
  151.           }
  152.         }
  153.         return drvs.ToCharArray();
  154.       }
  155.     }
  156.     public VolumeChangeFlags dbcv_flags;
  157.   }
  158.    
  159.   internal class DeviceChangeNotificationWindow : NativeWindow
  160.   {
  161.     public event DeviceChangeEventHandler DeviceChange;
  162.  
  163.     const int WS_EX_TOOLWINDOW = 0x80;
  164.     const int WS_POPUP = unchecked((int)0x80000000);
  165.    
  166.     const int WM_DEVICECHANGE = 0x0219;
  167.  
  168.     const int DBT_APPYBEGIN = 0x0000;
  169.     const int DBT_APPYEND = 0x0001;
  170.     const int DBT_DEVNODES_CHANGED = 0x0007;
  171.     const int DBT_QUERYCHANGECONFIG = 0x0017;
  172.     const int DBT_CONFIGCHANGED = 0x0018;
  173.     const int DBT_CONFIGCHANGECANCELED = 0x0019;
  174.     const int DBT_MONITORCHANGE = 0x001B;
  175.     const int DBT_SHELLLOGGEDON = 0x0020;
  176.     const int DBT_CONFIGMGAPI32 = 0x0022;
  177.     const int DBT_VXDINITCOMPLETE = 0x0023;
  178.     const int DBT_VOLLOCKQUERYLOCK = 0x8041;
  179.     const int DBT_VOLLOCKLOCKTAKEN = 0x8042;
  180.     const int DBT_VOLLOCKLOCKFAILED = 0x8043;
  181.     const int DBT_VOLLOCKQUERYUNLOCK = 0x8044;
  182.     const int DBT_VOLLOCKLOCKRELEASED = 0x8045;
  183.     const int DBT_VOLLOCKUNLOCKFAILED = 0x8046;
  184.     const int DBT_DEVICEARRIVAL = 0x8000;
  185.     const int DBT_DEVICEQUERYREMOVE = 0x8001;
  186.     const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
  187.     const int DBT_DEVICEREMOVEPENDING = 0x8003;
  188.     const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
  189.     const int DBT_DEVICETYPESPECIFIC = 0x8005;
  190.  
  191.     public DeviceChangeNotificationWindow()
  192.     {
  193.       CreateParams Params = new CreateParams();
  194.       Params.ExStyle = WS_EX_TOOLWINDOW;
  195.       Params.Style = WS_POPUP;
  196.       CreateHandle(Params);
  197.     }
  198.  
  199.     private void OnCDChange(DeviceChangeEventArgs ea)
  200.     {
  201.       if (DeviceChange != null)
  202.       {
  203.         DeviceChange(this, ea);
  204.       }
  205.     }
  206.     private void OnDeviceChange(DEV_BROADCAST_VOLUME DevDesc, DeviceChangeEventType EventType)
  207.     {
  208.       if (DeviceChange != null)
  209.       {
  210.         foreach (char ch in DevDesc.Drives)
  211.         {
  212.           DeviceChangeEventArgs a = new DeviceChangeEventArgs(ch, EventType);
  213.           DeviceChange(this, a);
  214.         }
  215.       }
  216.     }
  217.  
  218.     protected override void WndProc(ref Message m)
  219.     {
  220.       if ( m.Msg == WM_DEVICECHANGE )
  221.       {
  222.         DEV_BROADCAST_HDR head;
  223.         switch ( m.WParam.ToInt32() )
  224.         {
  225.           /*case DBT_DEVNODES_CHANGED :
  226.             break;
  227.           case DBT_CONFIGCHANGED :
  228.             break;*/
  229.           case DBT_DEVICEARRIVAL :
  230.             head = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
  231.             if ( head.dbch_devicetype == DeviceType.DBT_DEVTYP_VOLUME )
  232.             {
  233.               DEV_BROADCAST_VOLUME DevDesc = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
  234.               if ( DevDesc.dbcv_flags == VolumeChangeFlags.DBTF_MEDIA )
  235.               {
  236.                 OnDeviceChange(DevDesc, DeviceChangeEventType.DeviceInserted);
  237.               }
  238.             }
  239.             break;
  240.           /*case DBT_DEVICEQUERYREMOVE :
  241.             break;
  242.           case DBT_DEVICEQUERYREMOVEFAILED :
  243.             break;
  244.           case DBT_DEVICEREMOVEPENDING :
  245.             break;*/
  246.           case DBT_DEVICEREMOVECOMPLETE :
  247.             head = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
  248.             if ( head.dbch_devicetype == DeviceType.DBT_DEVTYP_VOLUME )
  249.             {
  250.               DEV_BROADCAST_VOLUME DevDesc = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCAST_VOLUME));
  251.               if ( DevDesc.dbcv_flags == VolumeChangeFlags.DBTF_MEDIA )
  252.               {
  253.                 OnDeviceChange(DevDesc, DeviceChangeEventType.DeviceRemoved);
  254.               }
  255.             }
  256.             break;
  257.           /*case DBT_DEVICETYPESPECIFIC :
  258.             break;*/
  259.         }
  260.       }
  261.     base.WndProc (ref m);
  262.     }
  263.   }
  264.  
  265.  
  266. }