Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: F3B14N en 19 Febrero 2011, 16:08 pm



Título: A por un MultiThread Decente!
Publicado por: F3B14N en 19 Febrero 2011, 16:08 pm
Hola gente, hago este thread para ver si alguien puede hacer un codigo decente para crear threads y que se pueda acceder a todos los recursos normales de VB6 sin que crashe.

-Este es el codigo que utilizo algún tiempo, pero tiene limitaciones, al crear un thread con un nuevo FORM VISIBLE crashea (.Show,.Visible=True, de cualquier manera, inluyendo el api.).
-Tambien la simple llamada a MsgBox crashea, pero se puede solucionar llamando al api.

Yo creo que esos dos problemas estan relacionados, si alguien tiene el conocimiento y tiempo, le agradecería que intentara crear un codigo para crear varios threads sin problemas. Seria el UNICO en toda la internet, porque no lo hay, almenos en VB6 :P

Código
  1. Option Explicit
  2.  
  3. Private Declare Function CreateThread Lib "KERNEL32" (ByVal lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, ByRef lpParameter As Any, ByVal dwCreationFlags As Long, ByRef lpThreadId As Long) As Long
  4. Private Declare Sub ExitThread Lib "KERNEL32" (ByVal dwExitCode As Long)
  5. Private Declare Function TlsGetValue Lib "KERNEL32" (ByVal dwTlsIndex As Long) As Long
  6. Private Declare Function TlsSetValue Lib "KERNEL32" (ByVal dwTlsIndex As Long, ByRef lpTlsValue As Any) As Long
  7.  
  8. Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
  9.  
  10. Private Declare Function GetProcAddress Lib "KERNEL32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
  11. Private Declare Function LoadLibrary Lib "KERNEL32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
  12. Private Declare Function FreeLibrary Lib "KERNEL32" (ByVal hLibModule As Long) As Long
  13.  
  14. Private MemAddress As Long
  15. Private TlsAddress As Long
  16. Private TlsIndex As Long
  17.  
  18. Public Function CreateNewThread(ByVal hThreadProc As Long, Optional ByVal Param As Long = 0) As Long
  19.    If (MemAddress + TlsIndex) = 0 Then
  20.        Call InitTlsIndex: Call CopyMemory(TlsIndex, ByVal TlsAddress, Len(TlsIndex)) 'Retrieve TlsIndx from TlsAddress
  21.        MemAddress = TlsGetValue(TlsIndex)
  22.    End If
  23.  
  24.    CreateNewThread = CreateThread(0, 0, hThreadProc, ByVal Param, 0, 0)
  25. End Function
  26.  
  27. Public Sub InitThread()
  28.    Call TlsSetValue(TlsIndex, ByVal MemAddress) 'VB will use this address to store DLL error information and etcs.
  29. End Sub
  30.  
  31. Private Sub InitTlsIndex()
  32.    'Tls Index's address of our thread.
  33.    Dim bB(40) As Byte, St As String
  34.    Dim hProc As Long, hLib As Long, i As Integer, j As Integer
  35.  
  36.    hLib = LoadLibrary("MSVBVM60")
  37.    hProc = GetProcAddress(hLib, "__vbaSetSystemError")
  38.    Call CopyMemory(bB(0), ByVal (hProc), 40)
  39.  
  40.    While bB(i) <> &HC3 'RETN
  41.        If bB(i) = &HFF And bB(i + 1) = &H35 Then
  42.            For j = i + 2 To i + 5
  43.                St = Hex(bB(j)) & St
  44.            Next
  45.            TlsAddress = Val("&H" & St): Exit Sub
  46.        End If
  47.        i = i + 1
  48.    Wend
  49.  
  50.    Call FreeLibrary(hProc)
  51. End Sub
  52.  
  53. Public Sub TerminateThread(ByVal dwExitCode As Long)
  54.    Call ExitThread(dwExitCode)
  55. End Sub

Abrazo


Título: Re: A por un MultiThread Decente!
Publicado por: F3B14N en 21 Febrero 2011, 01:59 am
Vamos chicas!!  >:D


Título: Re: A por un MultiThread Decente!
Publicado por: Karcrack en 21 Febrero 2011, 16:43 pm
Código:
http://advancevb.com.ar/?p=521


Título: Re: A por un MultiThread Decente!
Publicado por: F3B14N en 21 Febrero 2011, 21:52 pm
Código:
http://advancevb.com.ar/?p=521

Ya lo había visto, tenia un conflictos al iniciar el form con un control, pero puse el control en otro form que nunca se muestra y no hay problema, gracias Karcrack :)

EDITO:
El problema con el control sigue :(, aca subo un zip con un ejemplo del error por si alguien quiere ayudarme :P

http://www.box.net/shared/pdhdchnqc4

Gracias


Título: Re: A por un MultiThread Decente!
Publicado por: F3B14N en 23 Febrero 2011, 03:48 am
.