首页
社区
课程
招聘
[求助].net的 类型“Integer”的值无法转换为“System.IntPtr”。该如何解决呢?
发表于: 2008-12-31 21:06 8686

[求助].net的 类型“Integer”的值无法转换为“System.IntPtr”。该如何解决呢?

2008-12-31 21:06
8686
如下代码中的 所有下划线处都会出现 错误 类型“Integer”的值无法转换为“System.IntPtr”。该如何解决呢?
Imports WindowsApplication1.WindowsFormsApplication1

Class form1
    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim PinballHandle As IntPtr
        Dim PinballScoreAddressPointer As IntPtr = DirectCast(&H5D55888, IntPtr)
        '内存0x1025040中的值+0x52->PinballScoreAddress
        Dim PinballScoreAddress As UInteger() = New UInteger(1) {}
        '游戏分数的内存地址
        Dim PinballScoreWrite As UInteger() = New UInteger() {&H1869F}
        '将分数改为:999600000
        Dim PinballScoreRead As UInteger() = New UInteger(1) {}
        '用来存放当前分数
        '"3D Pinball"在“Windows 任务管理器”中显示为"PINBALL.EXE",但作为
        'GetProcessesByName的参数使用时并不能包含扩展名".EXE"。
        Dim PinballProcessID As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("ra3_1.4.game")
        '根据进程名获取进程ID
        If PinballProcessID.Length <> 0 Then
            PinballHandle = ProcessMemoryWorkApi.OpenProcess(&H1F0FFF, 0, DirectCast(PinballProcessID(0).Id, UInt32))
            'PROCESS_ALL_ACCESS=0x1F0FFF
            ProcessMemoryWorkApi.ReadProcessMemory(PinballHandle, PinballScoreAddressPointer, PinballScoreAddress, 4, DirectCast(0, IntPtr))
            '通过指针得到PinballScoreAddress
            PinballScoreAddress(0) = PinballScoreAddress(0) + &H4
            'PinballScoreAddress+0x52才是游戏分数的内存地址
          ProcessMemoryWorkApi.ReadProcessMemory(PinballHandle, DirectCast(PinballScoreAddress(0), IntPtr), PinballScoreRead, 4, DirectCast(0, IntPtr))
            '当前游戏分数
            MessageBox.Show("当前分数为: " + PinballScoreRead(0).ToString() + vbLf + "分数即将被修改成: 99999", "ProcessMemoryWork_Demo")
            ProcessMemoryWorkApi.WriteProcessMemory(PinballHandle, DirectCast(PinballScoreAddress(0), IntPtr), PinballScoreWrite, 4, DirectCast(0, IntPtr))            '修改游戏分数
            ProcessMemoryWorkApi.CloseHandle(PinballHandle)
        Else
            MessageBox.Show("""3D Pinball"" 还没有运行吧?", "ProcessMemoryWork_Demo")
        End If

    End Sub
End Class

相关的 自定义类调用

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Runtime.InteropServices

Namespace WindowsFormsApplication1
    Class ProcessMemoryWorkApi
        'HANDLE OpenProcess(
        ' DWORD dwDesiredAccess,// access flag
        ' BOOL bInheritHandle, // handle inheritance flag
        ' DWORD dwProcessId // process identifier
        ' );
        <DllImport("kernel32.dll")> _
        Public Shared Function OpenProcess(ByVal dwDesiredAccess As UInt32, ByVal bInheritHandle As Int32, ByVal dwProcessId As UInt32) As IntPtr
        End Function
        
        'BOOL CloseHandle(
        ' HANDLE hObject // handle to object to close
        ' );
        <DllImport("kernel32.dll")> _
        Public Shared Function CloseHandle(ByVal hObject As IntPtr) As Int32
        End Function
        
        'BOOL WriteProcessMemory(
        ' HANDLE hProcess, // handle to process whose memory is written to
        ' LPVOID lpBaseAddress, // address to start writing to
        ' LPVOID lpBuffer, // pointer to buffer to write data to
        ' DWORD nSize, // number of bytes to write
        ' LPDWORD lpNumberOfBytesWritten // actual number of bytes written
        ' );
        <DllImport("kernel32.dll")> _
        Public Shared Function WriteProcessMemory(ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer As UInteger(), ByVal nSize As UInt32, ByVal lpNumberOfBytesWritten As IntPtr) As Int32
        End Function
        
        'BOOL ReadProcessMemory(
        ' HANDLE hProcess, // handle of the process whose memory is read
        ' LPCVOID lpBaseAddress, // address to start reading
        ' LPVOID lpBuffer, // address of buffer to place read data
        ' DWORD nSize, // number of bytes to read
        ' LPDWORD lpNumberOfBytesRead // address of number of bytes read
        ' );
        <DllImport("kernel32.dll")> _
        Public Shared Function ReadProcessMemory(ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer As UInteger(), ByVal nSize As UInt32, ByVal lpNumberOfBytesRead As IntPtr) As Int32
        End Function
    End Class
   
End Namespace

[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 325
活跃值: (97)
能力值: ( LV13,RANK:530 )
在线值:
发帖
回帖
粉丝
2
ra3怎么跟pinball联系到一起了??郁闷。
2009-1-1 15:41
0
雪    币: 288
活跃值: (112)
能力值: ( LV12,RANK:290 )
在线值:
发帖
回帖
粉丝
3
用 IntPtr 的构造函数
2009-1-1 23:37
0
游客
登录 | 注册 方可回帖
返回
//