类型“Integer”的值无法转换为“System.IntPtr该如何解决

本文介绍了一段VB.NET代码中出现的错误——无法将Integer类型的值转换为System.IntPtr,并给出了具体的解决方法。该问题发生在尝试使用DirectCast进行类型转换时。

如下代码中的 错误 类型“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

 

 

 

解决方法:Dim PinballScoreAddressPointer As IntPtr = New IntPtr(&H5D55888)

用程序: Torque.exe Framework 版本: v4.0.30319 说明: 由于未经处理的异常,进程终止。异常信息: System.FormatException 在 System.Number.StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuffer ByRef, System.Globalization.NumberFormatInfo, Boolean) 在 System.Number.ParseInt32(System.String, System.Globalization.NumberStyles, System.Globalization.NumberFormatInfo) 在 System.Convert.ToInt32(System.String) 在 Torque.App.Dispatcher_UnhandledException(System.Object, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs) 在 System.Windows.Threading.DispatcherUnhandledExceptionEventHandler.Invoke(System.Object, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs) 在 System.Windows.Threading.Dispatcher.CatchException(System.Exception) 在 System.Windows.Threading.Dispatcher.CatchExceptionStatic(System.Object, System.Exception) 在 System.Windows.Threading.ExceptionWrapper.CatchException(System.Object, System.Exception, System.Delegate) 在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) 在 System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) 在 System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame) 在 System.Windows.Application.RunDispatcher(System.Object) 在 System.Windows.Application.RunInternal(System.Windows.Window) 在 System.Windows.Application.Run(System.Windows.Window) 在 System.Windows.Application.Run() 在 Torque.App.Main()
03-12
上面回答的代码运行后异常提示:System.Reflection.TargetInvocationException: 调用的目标发生了异常。 ---> System.InvalidCastException: 从字符串“无法计算法线交点”到类型Integer”的转换无效。 ---> System.FormatException: 输入字符串的格式不正确。 在 Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) 在 Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) --- 内部异常堆栈跟踪的结尾 --- 在 Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) 在 R17._0.A4RectLayout.GetNormalIntersection(Polyline axis, Int32 index, Polyline targetLine, Transaction trans) 位置 F:\VSTO\CAD版块\R17.0\CAD多段线布矩形.vb:行号 90 在 R17._0.A4RectLayout.CalculateBoundaryPoints(Polyline axis, Polyline leftLine, Polyline rightLine, Transaction trans) 位置 F:\VSTO\CAD版块\R17.0\CAD多段线布矩形.vb:行号 65 在 R17._0.A4RectLayout.LayoutA4Rectangles() 位置 F:\VSTO\CAD版块\R17.0\CAD多段线布矩形.vb:行号 28 --- 内部异常堆栈跟踪的结尾 --- 在 System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) 在 System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 在 AcMgCommandClass.InvokeWorker(AcMgCommandClass* , MethodInfo mi, Object commandObject, Boolean bLispFunction) 在 AcMgPerDocumentCommandClass.Invoke(AcMgPerDocumentCommandClass* , gcroot<System::Reflection::MethodInfo ^>* mi, Boolean bLispFunction) 在 Autodesk.AutoCAD.Runtime.TargetInvocationSEHExceptionFilter.InvokeWorker() 在 Autodesk.AutoCAD.Runtime.ExceptionFilter.Invoke()
11-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值