AccessViolationException

Résolu
capo666 Messages postés 35 Date d'inscription jeudi 8 septembre 2005 Statut Membre Dernière intervention 1 juin 2008 - 14 mai 2007 à 22:39
capo666 Messages postés 35 Date d'inscription jeudi 8 septembre 2005 Statut Membre Dernière intervention 1 juin 2008 - 15 mai 2007 à 00:49
salut j'ai un gros problème incompréhensible. j'ai trouvé un bon module sur planet-source pour écrire et lire la mémoire. il marche très très bien en VB6.0 mais lorque je les transferer pour vb.net sa me donne un erreur AccessViolationException  (a la ligne souligner dans le code) sa dit que la mémoire est protéger mais c'est impossible car se code marche super bien en vb.60 mais un coup sur vb.net boum il ne marche plus !

merci d'avance pour la solution!

Module Module1
    '|--------------------------------------------------------------------
    '| These functions are written by Cola-kattn (opersvik@hotmail.com)
    '|--------------------------------------------------------------------
    '| You can use the functions in this module for learning purpose or you can
    '| use this whole module or part of it in your own trainers if you like, there
    '| are no real copyright on the code :)
    '| Enjoy!
    '|
    '| The usage of the functions is pretty straight forward.
    '| If the Read functions are succesfull, they will return the found value in a specified memory
    '| offset from a specified process
    '|
    '| The Write functions will return True if writing to the specified memory offset was succesfull
    '| and False if it failed.
    '| You can take use of these returns as error checking though I made error checking in every
    '| function that will display an message box and terminate the function when an error occurs
    '| (If the process window is not found, or the program can't get a process handle.)
    '|
    '| The memory offset is commonly found by using a memory search tool, such as GameHack or TSearch.
    '| The windowname of the game\program can be found in the window list box when you hit Ctrl + Alt + Del
    '|---------------------------------------------------------------------

    '//All API declarations we will need to make these functions useful:

    'Thanks to Robert Meffe for pointing out this API line because he didn't get it
    'to work properly in his Win XP. Greets!
    Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF

    Private Declare Function GetWindowThreadProcessId Lib "User32" (ByVal hwnd As Long, ByVal lpdwProcessId As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Object, ByVal lpBuffer As Object, ByVal nSize As Long, ByVal lpNumberOfBytesWritten As Long) As Long
    Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Object, ByVal lpBuffer As Object, ByVal nSize As Long, ByVal lpNumberOfBytesWritten As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal Classname As String, ByVal WindowName As String) As Long
    Private Declare Function ReadProcessMem Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Long, ByVal lpBaseAddress As Object, ByRef lpBuffer As Object, ByVal nSize As Long, ByVal lpNumberOfBytesWritten As Long) As Long

    '||-------------------------------------------------------------------------------------------------||
    '|| The two next functions read\write BYTE values.                                                  ||
    '|| BYTE is an 8-bit datatype that can store values from 0 to 255.                                  ||
    '||-------------------------------------------------------------------------------------------------||

    Public Function ReadByte(ByVal Offset As Long, ByVal WindowName As String) As Byte

        Dim hwnd As Long
        Dim ProcessID As Long
        Dim ProcessHandle As Long
        Dim Value As Byte

        'Try to find the window that was passed in the variable WindowName to this function.
        hwnd = FindWindow(vbNullString, WindowName)

        If hwnd = 0 Then

            'This is executed if the window cannot be found.
            'You can add or write own code here to customize your program.

            MsgBox("Could not find process window!", vbCritical, "Read error")

            Exit Function

        End If

        'Get the window's process ID.
        GetWindowThreadProcessId(hwnd, ProcessID)

        'Get a process handle.
        ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID)

        If ProcessHandle = 0 Then

            'This is executed if a process handle cannot be found.
            'You can add or write your own code here to customize your program.

            MsgBox("Could not get a process handle!", vbCritical, "Read error")

            Exit Function

        End If

        'Read a BYTE value from the specified memory offset.
        ReadProcessMem(ProcessHandle, Offset, Value, 1, 0&)

        'Return the found memory value.
        ReadByte = Value

        'It is important to close the current process handle.
        CloseHandle(ProcessHandle)

    End Function

    Public Function WriteByte(ByVal Offset As Long, ByVal WindowName As String, ByVal Value As Byte) As Boolean

        Dim hwnd As Long
        Dim ProcessID As Long
        Dim ProcessHandle As Long

        'Try to find the window that was passed in the variable WindowName to this function.
        hwnd = FindWindow(vbNullString, WindowName)

        If hwnd = 0 Then

            'This is executed if the window cannot be found.
            'You can add or write own code here to customize your program.

            MsgBox("Could not find process window!", vbCritical, "Write error")

            Exit Function

        End If

        'Get the window's process ID.
        GetWindowThreadProcessId(hwnd, ProcessID)

        'Get a process handle.
        ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID)

        If ProcessHandle = 0 Then

            'This is executed if a process handle cannot be found.
            'You can add or write your own code here to customize your program.

            MsgBox("Could not get a process handle!", vbCritical, "Write error")

            Exit Function

        End If

        'Write a specified BYTE value to the specified memory offset.
        WriteProcessMemory(ProcessHandle, Offset, Value, 1, 0&)

        'It is important to close the current process handle.
        CloseHandle(ProcessHandle)

    End Function

    '||-------------------------------------------------------------------------------------------------||
    '|| The two next functions read\write INTEGER values.                                               ||
    '|| INTEGER is a 16-bit(2 byte) datatype and can store values from -32768 to 32767                  ||
    '||-------------------------------------------------------------------------------------------------||

    Public Function ReadInteger(ByVal Offset As Long, ByVal WindowName As String) As Integer

        Dim hwnd As Long
        Dim ProcessID As Long
        Dim ProcessHandle As Long
        Dim Value As Integer

        'Try to find the window that was passed in the variable WindowName to this function.
        hwnd = FindWindow(vbNullString, WindowName)

        If hwnd = 0 Then

            'This is executed if the window cannot be found.
            'You can add or write your own code here to customize your program.

            MsgBox("Could not find process window!", vbCritical, "Read error")

            Exit Function

        End If

        'Get the window's process ID.
        GetWindowThreadProcessId(hwnd, ProcessID)

        'Get a process handle.
        ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID)

        If ProcessHandle = 0 Then

            'This is executed if a process handle cannot be found.
            'You can add or write your own code here to customize your program.

            MsgBox("Could not get a process handle!", vbCritical, "Read error")

            Exit Function

        End If

        'Read an INTEGER value from the specified memory offset.
        ReadProcessMem(ProcessHandle, Offset, Value, 2, 0&)

        'Return the found memory value.
        ReadInteger = Value

        'It is important to close the current process handle.
        CloseHandle(ProcessHandle)

    End Function

    Public Function WriteInteger(ByVal Offset As Long, ByVal WindowName As String, ByVal Value As Integer) As Boolean

        Dim hwnd As Long
        Dim ProcessID As Long
        Dim ProcessHandle As Long

        'Try to find the window that was passed in the variable WindowName to this function.
        hwnd = FindWindow(vbNullString, WindowName)

        If hwnd = 0 Then

            'This is executed if the window cannot be found.
            'You can add or write your own code here to customize your program.

            MsgBox("Could not find process window!", vbCritical, "Write error")

            Exit Function

        End If

        'Get the window's process ID.
        GetWindowThreadProcessId(hwnd, ProcessID)

        'Get a process handle.
        ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID)

        If ProcessHandle = 0 Then

            'This is executed if a process handle cannot be found.
            'You can add or write your own code here to customize your program.

            MsgBox("Could not get a process handle!", vbCritical, "Write error")

            Exit Function

        End If

        'Write a specified INTEGER value to the specified memory offset.
        WriteProcessMemory(ProcessHandle, Offset, Value, 2, 0&)

        'It is important to close the current process handle.
        CloseHandle(ProcessHandle)

    End Function

    '||-------------------------------------------------------------------------------------------------||
    '|| The two next functions read\write LONG values.                                                  ||
    '|| LONG is a 32-bit(4 byte) datatype and can store values from -2,147,483,648 to 2,147,483,647     ||
    '||-------------------------------------------------------------------------------------------------||

    Public Function ReadLong(ByVal Offset As Long, ByVal WindowName As String) As Long

        Dim hwnd As Long
        Dim ProcessID As Long
        Dim ProcessHandle As Long
        Dim Value As Long

        'Try to find the window that was passed in the variable WindowName to this function.
        hwnd = FindWindow(vbNullString, WindowName)

        If hwnd = 0 Then

            'This is executed if the window cannot be found.
            'You can add or write your own code here to customize your program.

            MsgBox("Could not find process window!", vbCritical, "Read error")

            Exit Function

        End If

        'Get the window's process ID.
        GetWindowThreadProcessId(hwnd, ProcessID)

        'Get a process handle
        ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID)

        If ProcessHandle = 0 Then

            'This is executed if a process handle cannot be found.
            'You can add or write your own code here to customize your program.

            MsgBox("Could not get a process handle!", vbCritical, "Read error")

            Exit Function

        End If

        'Read a LONG from the specified memory offset.
        ReadProcessMem(ProcessHandle, Offset, Value, 4, 0&)

        'Return the found memory value.
        ReadLong = Value

        'It is important to close the current process handle.
        CloseHandle(ProcessHandle)

    End Function

    Public Function WriteLong(ByVal Offset As Long, ByVal WindowName As String, ByVal Value As Long) As Boolean

        Dim hwnd As Long
        Dim ProcessID As Long
        Dim ProcessHandle As Long

        'Try to find the window that was passed in the variable WindowName to this function.
        hwnd = FindWindow(vbNullString, WindowName)

        If hwnd = 0 Then

            'This is executed if the window cannot be found.
            'You can add or write your own code here to customize your program.

            MsgBox("Could not find process window!", vbCritical, "Write error")

            Exit Function

        End If

        'Get the window's process ID.
        GetWindowThreadProcessId(hwnd, ProcessID)

        'Get a process handle
        ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID)

        If ProcessHandle = 0 Then

            'This is executed if a process handle cannot be found.
            'You can add or write your own code here to customize your program.

            MsgBox("Could not get a process handle!", vbCritical, "Write error")

            Exit Function

        End If

        'Read a LONG from the specified memory offset.
        WriteProcessMemory(ProcessHandle, Offset, Value, 4, 0&)

        'It is important to close the current process handle.
        CloseHandle(ProcessHandle)

    End Function

End Module

7 réponses

cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
14 mai 2007 à 22:46
Toujours le même problème, il va fallior apprendre à chercher sur le forum un peu

Avant le suicide, corrige toutes les déclarations des API. Remplace tes Longs par des Integers

---- Sevyc64  (alias Casy) ---- # LE PARTAGE EST NOTRE FORCE #
3
capo666 Messages postés 35 Date d'inscription jeudi 8 septembre 2005 Statut Membre Dernière intervention 1 juin 2008
14 mai 2007 à 23:35
bon jai fais le changement mais est ce que faut je change les fonctions déclarer en long en integer aussi? ou juste les api

Private Const PROCESS_ALL_ACCESS As Integer = &H1F0FFF

    Private Declare Function GetWindowThreadProcessId Lib "User32" (ByVal hwnd As Integer, ByVal lpdwProcessId As Integer) As Integer
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
    Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Object, ByVal lpBuffer As Object, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer
    Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Object, ByVal lpBuffer As Object, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
    Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal Classname As String, ByVal WindowName As String) As Long
    Private Declare Function ReadProcessMem Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Object, ByRef lpBuffer As Object, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer
3
Joke758 Messages postés 34 Date d'inscription mercredi 21 février 2007 Statut Membre Dernière intervention 6 juin 2008
14 mai 2007 à 22:41
Le suicide est ta seule option.
0
capo666 Messages postés 35 Date d'inscription jeudi 8 septembre 2005 Statut Membre Dernière intervention 1 juin 2008
14 mai 2007 à 23:06
tu crois que le problème vien de la moi je crois pas
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Joke758 Messages postés 34 Date d'inscription mercredi 21 février 2007 Statut Membre Dernière intervention 6 juin 2008
14 mai 2007 à 23:09
Pourquoi changer les Long par Integers? Avec Integer on ne peux pas aller plus haut que 10.
0
cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
14 mai 2007 à 23:12
Un long en VB6 ou api Windows fait 32 bits
Un long en .Net fait 64 bits.

Et oui, les types de variables ont changés en .Net.

Alors même si tu as un problème ailleurs, commence par corriger tes déclarations foireuses.

Il est tout de même à noter que ce problème revient souvent sur le forum et que de plus c'est la toute premiere page que l'on peut lire dans la msdn lorsque on s'interesse à la transition VB6/.Net

---- Sevyc64  (alias Casy) ---- # LE PARTAGE EST NOTRE FORCE #
0
capo666 Messages postés 35 Date d'inscription jeudi 8 septembre 2005 Statut Membre Dernière intervention 1 juin 2008
15 mai 2007 à 00:49
c'était vraiment les déclarations le problème merci beaucoup de l'aide!
0
Rejoignez-nous