elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Estamos en la red social de Mastodon


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  (SOLUCIONADO SRC)Ayuda pasar codigo a VB.Net: Seriales Pen-Drives vb6
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: (SOLUCIONADO SRC)Ayuda pasar codigo a VB.Net: Seriales Pen-Drives vb6  (Leído 4,255 veces)
Maurice_Lupin


Desconectado Desconectado

Mensajes: 356

GPS


Ver Perfil WWW
(SOLUCIONADO SRC)Ayuda pasar codigo a VB.Net: Seriales Pen-Drives vb6
« en: 10 Noviembre 2011, 17:32 pm »

Estoy pasando un code a VB.net 2005 de Sergio Desanti (Hasseds) del foro VB6 : Seriales

Pen-Drives
http://foro.elhacker.net/programacion_visual_basic/seriales_de_pendrives_conectados_src-t331333.0.html

EL problema es que entra en un bucle interminable, y tampoco estoy seguro si estoy utilizando bien las

funciones del API

Código
  1.  
  2.    Imports System.Runtime.InteropServices    
  3.  
  4.    <StructLayout(LayoutKind.Sequential)> _
  5.    public structure SP_DEVICE_INTERFACE_DATA
  6.        public cbSize as Long
  7.        public InterfaceClassGuid as GUID
  8.        public flags as Long
  9.        public IntPtr as Long
  10.    End Structure
  11.  
  12.    <StructLayout(LayoutKind.Sequential)> _
  13.    Public Structure SP_DEVINFO_DATA
  14.     Public cbSize As UInteger
  15.     Public ClassGuid As Guid
  16. Public DevInst As UInteger
  17.     Public Reserved As IntPtr
  18.    End Structure
  19.  
  20.  
  21.   <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
  22.   Public Structure SP_DEVICE_INTERFACE_DETAIL_DATA
  23.   Public cbSize As UInt32
  24. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
  25. Public strDevicePath As String
  26.   End Structure
  27.  
  28.    ' SetupDiGetClassDevs: Retorna informacion sobre el dispositivo.
  29.    <DllImport("setupapi.dll",EntryPoint:="SetupDiGetClassDevsW", SetLastError:=True, _
  30.    CharSet:=CharSet.Unicode, ExactSpelling:=True, PreserveSig:=True, _
  31.    CallingConvention:=CallingConvention.Winapi)> _
  32.    Private Shared Function SetupDiGetClassDevs( _
  33.    ByRef ClassGuid As GUID, _
  34.    ByVal Enumerator As Integer, _
  35.    ByVal hwndParent As Integer, _
  36.    ByVal Flags As Integer) As Integer
  37.    End Function    
  38.  
  39.  ' SetupDiEnumDeviceInterfaces
  40.  <DllImport("setupapi.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  41.  Public Shared Function SetupDiEnumDeviceInterfaces(ByVal hDevInfo As IntPtr, _
  42.    ByRef devInfo As SP_DEVICE_INTERFACE_DATA, _
  43.    ByRef interfaceClassGuid As Guid, _
  44.    ByVal memberIndex As UInt32, _
  45.    ByRef deviceInterfaceData As SP_DEVICE_INTERFACE_DATA) As Boolean
  46.  End Function
  47.  
  48.  ' SetupDiGetDeviceInterfaceDetail
  49.  <DllImport("setupapi.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  50.    Public Shared Function SetupDiGetDeviceInterfaceDetail ( _
  51.    ByVal DeviceInfoSet As IntPtr, _
  52.    ByRef DeviceInterfaceData As SP_DEVICE_INTERFACE_DATA, _
  53.    ByRef DeviceInterfaceDetailData As SP_DEVICE_INTERFACE_DETAIL_DATA, _
  54.    ByVal DeviceInterfaceDetailDataSize As UInteger, _
  55.    ByRef RequiredSize As UInteger, _
  56.    ByRef DeviceInfoData As SP_DEVINFO_DATA) As Boolean
  57.    end function
  58.  
  59.    ' SetupDiDestroyDeviceInfoList
  60.    <DllImport("setupapi.dll", _
  61.    EntryPoint:="SetupDiDestroyDeviceInfoList", _
  62.    SetLastError:=True, _
  63.    ExactSpelling:=True, _
  64.    PreserveSig:=True, _
  65.    CallingConvention:=CallingConvention.Winapi)> _
  66.    Private Shared Function SetupDiDestroyDeviceInfoList( _
  67.    ByVal DeviceInfoSet As Integer) As Boolean
  68.    End Function
  69.  
  70.    '------CharSet:=CharSet.Auto
  71. '    <DllImport("ole32.dll", CharSet:=CharSet.Unicode,PreserveSig:=false)> _
  72. ' static IIDFromString(lpsz as String)
  73.  
  74.  
  75. ' Funcion
  76. Public Function FlashSerials() As String
  77.  
  78.   Dim TGUID As GUID
  79.   Dim lCount       As Long
  80.   Dim lSize        As Long
  81.   Dim DTL          As new SP_DEVICE_INTERFACE_DETAIL_DATA
  82.   Dim DTA          As new SP_DEVICE_INTERFACE_DATA
  83.   dim cad as String  
  84.   Dim strValue As String ="{a5dcbf10-6530-11d2-901f-00c04fb951ed}"
  85.  
  86.   ' esto era: Call IIDFromString(StrPtr("{a5dcbf10-6530-11d2-901f-00c04fb951ed}"), TGUID)
  87.   ' pero no encuentro un equivalente en NET de IIDFromString
  88.   ' encontre por ahi que se podia crear un Guid asi, estara bien?
  89.   TGUID = New Guid(strvalue)
  90.  
  91.   Dim hDev As Long  
  92.   hDev = SetupDiGetClassDevs(TGUID, &H0, &H0, &H12)
  93.  
  94.   If hDev = -1 Then
  95.   return "No hay nada"
  96.   Exit Function
  97.   End If
  98.  
  99.   DTA.cbSize = Len(DTA)
  100.   DTL.cbSize = &H5
  101.  
  102.   Dim dia As  SP_DEVICE_INTERFACE_DATA  
  103.   dia.cbSize = &H0  
  104.  
  105.   Dim dd As SP_DEVINFO_DATA
  106.   dd.cbSize = &H0
  107.   cad = ""
  108.   lCount = 0
  109.   Dim nulo As new SP_DEVICE_INTERFACE_DETAIL_DATA
  110.  
  111.   ' Esto era asi
  112.   'do while not (SetupDiEnumDeviceInterfaces(hDev, &H0, TGUID, lCount, DTA) = &H0)
  113.   ' Entra en un bucle interminable,que estoy haciendo mal?
  114.   do While not SetupDiEnumDeviceInterfaces(hDev, dia , TGUID, lCount, DTA)
  115.  
  116.     ' Tengo mis dudas si estoy utilizando bien las funciones
  117.     'call SetupDiGetDeviceInterfaceDetail(hDev, DTA, ByVal &H0, &H0, lSize, ByVal &H0)      
  118.     Call SetupDiGetDeviceInterfaceDetail(hDev, dia, nulo , &H0, lSize, dd)      
  119.  
  120.     ' Call SetupDiGetDeviceInterfaceDetail(hDev, DTA, DTL, ByVal lSize, &H0, ByVal &H0)
  121.     call SetupDiGetDeviceInterfaceDetail(hDev, DTA, DTL, lSize, &H0, dd)
  122.  
  123.     If UBound(Split(DTL.strDevicePath, "#")) > 1 Then
  124.       cad = cad & Split(UCase(DTL.strDevicePath), "#")(2) & Chr(&HD)      
  125.     End If    
  126.     lCount = lCount + 1
  127.   loop
  128.   Call SetupDiDestroyDeviceInfoList(hDev)
  129.  
  130.   If cad = "" Then cad = "No hay conexiones"
  131.   return cad
  132. End Function
  133.  

Si alguien tiene sugerencias o alguna informacion sobre las funciones de la API que comento, se los

agradeceria.

Saludos.


« Última modificación: 17 Noviembre 2011, 16:47 pm por Maurice_Lupin » En línea

Un error se comete al equivocarse.
Hasseds

Desconectado Desconectado

Mensajes: 145



Ver Perfil
Re: Ayuda pasar codigo a VB.Net: Seriales Pen-Drives vb6
« Respuesta #1 en: 16 Noviembre 2011, 21:37 pm »



   ' esto era: Call IIDFromString(StrPtr("{a5dcbf10-6530-11d2-901f-00c04fb951ed}"), TGUID)
   ' pero no encuentro un equivalente en NET de IIDFromString
   ' encontre por ahi que se podia crear un Guid asi, estara bien?

   TGUID = New Guid(strvalue)
   
 


Fijate si esto ayuda

http://bytes.com/topic/visual-basic-net/answers/381152-how-declare-globally-unique-identifier-guid




En línea

Sergio Desanti
Maurice_Lupin


Desconectado Desconectado

Mensajes: 356

GPS


Ver Perfil WWW
Re: Ayuda pasar codigo a VB.Net: Seriales Pen-Drives vb6
« Respuesta #2 en: 17 Noviembre 2011, 15:15 pm »

Muchas gracias Hasseds por todo, logre solucionar hoy en la madrugada. Voy a postear el codigo, también encontre como detectar cuando se conecta/desconecta un USB. Lo posteo aqui mismo?
En línea

Un error se comete al equivocarse.
Maurice_Lupin


Desconectado Desconectado

Mensajes: 356

GPS


Ver Perfil WWW
Re: (SOLUCIONADO)Ayuda pasar codigo a VB.Net: Seriales Pen-Drives vb6
« Respuesta #3 en: 17 Noviembre 2011, 16:45 pm »

Yo utilizo el IDE http://www.icsharpcode.net/OpenSource/SD/Default.aspx para programar en VB.Net. Casi lo mismo que el VB.net 2005. Sólo que no hay que pagar licencia, eso lei. Corrijanme si estoy equivocado.

Gracias a Hasseds.

En esta pagina encontre buena info para llamar a las funciones de la API en VB.NET
http://pinvoke.net/default.aspx/setupapi.SetupDiEnumDeviceInterfaces

Código
  1.  
  2. Imports System.Runtime.InteropServices
  3.   <StructLayout(LayoutKind.Sequential, Pack := 1)> _
  4. Public Structure DeviceInterfaceData
  5. Public Size As Integer
  6. Public InterfaceClassGuid As Guid
  7. Public Flags As Integer
  8. Public Reserved As Integer
  9. End Structure
  10.  
  11. <StructLayout(LayoutKind.Sequential, Pack := 1)> _
  12. Public Structure DeviceInterfaceDetailData
  13. Public Size As Integer
  14. <MarshalAs(UnmanagedType.ByValTStr, SizeConst := 256)> _
  15. Public DevicePath As String
  16. End Structure
  17.  
  18.    ' SetupDiGetClassDevs: Retorna información sobre el dispositivo
  19.    <DllImport("setupapi.dll",EntryPoint:="SetupDiGetClassDevsW", SetLastError:=True, _
  20.    CharSet:=CharSet.Unicode, ExactSpelling:=True, PreserveSig:=True, _
  21.    CallingConvention:=CallingConvention.Winapi)> _
  22.    Private Shared Function SetupDiGetClassDevs( _
  23.    ByRef ClassGuid As GUID, _
  24.    ByVal Enumerator As Integer, _
  25.    ByVal hwndParent As Integer, _
  26.    ByVal Flags As Integer) As Integer
  27.    End Function    
  28.  
  29.  
  30.   ' SetupDiDestroyDeviceInfoList
  31.    <DllImport("setupapi.dll", _
  32.    EntryPoint:="SetupDiDestroyDeviceInfoList", _
  33.    SetLastError:=True, _
  34.    ExactSpelling:=True, _
  35.    PreserveSig:=True, _
  36.    CallingConvention:=CallingConvention.Winapi)> _
  37.    Private Shared Function SetupDiDestroyDeviceInfoList( _
  38.    ByVal DeviceInfoSet As Integer) As Boolean
  39.    End Function
  40.  
  41.    <DllImport("setupapi.dll", SetLastError := True)> _
  42. Public Shared Function SetupDiEnumDeviceInterfaces(ByVal lpDeviceInfoSet As IntPtr, ByVal nDeviceInfoData As UInteger, ByRef gClass As Guid, ByVal nIndex As UInteger, ByRef oInterfaceData As DeviceInterfaceData) As Boolean
  43. End Function
  44.  
  45. <DllImport("setupapi.dll", SetLastError := True)> _
  46. Public Shared Function SetupDiGetDeviceInterfaceDetail(ByVal lpDeviceInfoSet As IntPtr, ByRef oInterfaceData As DeviceInterfaceData, ByVal lpDeviceInterfaceDetailData As IntPtr, ByVal nDeviceInterfaceDetailDataSize As UInteger, ByRef nRequiredSize As UInteger, ByVal lpDeviceInfoData As IntPtr) As Boolean
  47. End Function
  48.  
  49. <DllImport("setupapi.dll", SetLastError := True)> _
  50. Public Shared Function SetupDiGetDeviceInterfaceDetail(ByVal lpDeviceInfoSet As IntPtr, ByRef oInterfaceData As DeviceInterfaceData, ByRef oDetailData As DeviceInterfaceDetailData, ByVal nDeviceInterfaceDetailDataSize As UInteger, ByRef nRequiredSize As UInteger, ByVal lpDeviceInfoData As IntPtr) As Boolean
  51. End Function    
  52.  
  53.  
  54. ' Funcion
  55. Public Function FlashSerials() As String
  56.   Dim TGUID As GUID
  57.   Dim lCount       As UInt32
  58.   Dim lSize        As Integer = &H0
  59.   Dim DTL          As new DeviceInterfaceDetailData
  60.   Dim DTA          As new DeviceInterfaceData
  61.   Dim cad as String        
  62.   TGUID = New Guid("{a5dcbf10-6530-11d2-901f-00c04fb951ed}")  
  63.  
  64.   Dim hDev As IntPtr = SetupDiGetClassDevs( TGUID, &H0 ,&H0, &H12)
  65.  
  66.   If hDev = -1 Then
  67.   return "Nada"
  68.   Exit Function
  69.   End If  
  70.  
  71.   DTA.Size  = Marshal.SizeOf(DTA)
  72.   DTL.Size = &H5  
  73.   cad = ""
  74.   lCount = 0
  75.  
  76.   While SetupDiEnumDeviceInterfaces(hDev, &H0, TGUID, lCount, DTA)
  77.  
  78.      If Not SetupDiGetDeviceInterfaceDetail(hDev, DTA, IntPtr.Zero , 0, lSize, IntPtr.Zero ) Then        
  79.     If SetupDiGetDeviceInterfaceDetail(hDev, DTA, DTL , lSize, lSize, IntPtr.Zero) Then        
  80.     If Ubound(Split(DTL.DevicePath ,"#"))>1 then
  81.        cad = cad & Split(Ucase(DTL.DevicePath) ,"#")(2)  & chr(10)
  82.     End If    
  83. End If
  84.  End if
  85.      lCount = lCount + 1
  86.   End while  
  87.   SetupDiDestroyDeviceInfoList(hDev)
  88.   If cad = "" Then cad = "No hay conexiones"  
  89.   return cad
  90. End Function
  91.  
  92.  

Es codigo lo encontre en
http://tempuzfugit.wordpress.com/2007/06/05/deteccion-de-insercion-de-disco/

Código
  1. ' Al sobreescribir este Metodo se puede detectar la acción de conectar/desconectar USB
  2. Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
  3.  
  4.  'Se ha producido un cambio en los dispositivos
  5.  Const deviceChange As Integer = &H219
  6.  
  7.  'El sistema detecta un nuevo dispositivo
  8.  Const deviceArrival As Integer = &H8000
  9.  
  10.  'Dispositivo extraído del sistema
  11.  Const deviceRemoveComplete As Integer = &H8004
  12.  
  13.  ' Volumen lógico (Se ha insertado un disco)
  14.  Const deviceTypeVolume As Integer = &H2
  15.  
  16.  Select Case m.Msg
  17.   'Cambian los dispositivos del sistema
  18.   Case deviceChange
  19.   Select Case m.WParam.ToInt32
  20.   'Llegada de un dispositivo
  21.  Case deviceArrival
  22.    Dim devType As Integer = Marshal.ReadInt32(m.LParam, 4)
  23.    'Si es un volumen lógico..(unidad de disco)
  24.    If devType = deviceTypeVolume Then
  25.   msgbox("Se inserto: " & FlashSerials())
  26.  End If
  27.  Case deviceRemoveComplete
  28.  MessageBox.Show("Se retiró el dispositivo.")
  29.  End Select
  30.  End Select
  31.  
  32.  'Ahora se usa el manejador predeterminado
  33.  MyBase.WndProc(m)
  34. End Sub
  35.  
  36.  
  37.  
« Última modificación: 17 Noviembre 2011, 16:49 pm por Maurice_Lupin » En línea

Un error se comete al equivocarse.
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[Ayuda] Pasar codigo de VB.Net a VB6
Programación Visual Basic
v3ng4nz3 2 3,935 Último mensaje 10 Septiembre 2010, 01:12 am
por v3ng4nz3
Seriales de Pen-Drives conectados (SRC) « 1 2 3 »
Programación Visual Basic
Hasseds 20 10,603 Último mensaje 6 Diciembre 2011, 05:41 am
por Maurice_Lupin
Pasar a código ensamblador [Ayuda]
ASM
leogtz 6 10,827 Último mensaje 6 Diciembre 2011, 21:41 pm
por leogtz
[QT][C++]Problemas al compilar los drives mysql (SOLUCIONADO)
Programación C/C++
anonimo12121 1 2,247 Último mensaje 7 Noviembre 2012, 21:14 pm
por anonimo12121
Errores seriales, AYUDA!! (SOLUCIONADO)
Programación C/C++
g.izmo 3 2,448 Último mensaje 13 Octubre 2017, 18:44 pm
por ivancea96
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines