VB 调用API时出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”错误
问题:如果标题
开发平台:VS2010
程序:如下
Option Explicit On
Public Class Form1
Public Const GENERIC_WRITE = &H40000000
Public Const GENERIC_READ = &H80000000
Const FILE_ATTRIBUTE_NORMAL = &H80
Const CREATE_ALWAYS = 2
Const OPEN_ALWAYS = 4
Const INVALID_HANDLE_VALUE = -1
Const FILE_NAME = "TEST.DAT" 'This can be any file that does not
'currently exist.
'打开USB
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer
'读取数据至缓冲区
Declare Function ReadFile Lib "kernel32" (ByVal hFile As Integer, ByVal lpBuffer As Integer, ByVal nNumberOfBytesToRead As Integer, ByVal lpNumberOfBytesRead As Integer, ByVal lpOverlapped As Integer) As Integer
'写入数据至缓冲区
Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, ByVal lpBuffer As Integer, ByVal nNumberOfBytesToWrite As Int16, ByVal lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Integer
'关闭USB
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
'刷新缓冲区
Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Integer) As Integer
Sub fillArray(ByVal t() As Integer) '为数组赋值
Dim x As Integer
For x = 0 To 63
t(x) = x + 10
Next x
End Sub
Sub Main()
Dim T(63) As Integer
Dim S(63) As Integer
fillArray(T) '为数组赋初值
writearray(FILE_NAME, T) '写入测试数据
readArray(FILE_NAME, S) '读出测试数据
End Sub
Sub readArray(ByVal Fname As String, ByVal s() As Integer)
Dim fHandle As Integer
Dim fSuccess As Integer
'Get a handle to a file Fname.
fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, _
0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
'Here you should test to see if you get a file handle or not.
'CreateFile returns INVALID_HANDLE_VALUE if it fails.
If fHandle <> INVALID_HANDLE_VALUE Then
fSuccess = ReadFile(fHandle, s(0), _
64, 64, 0)
'ReadFile returns a non-zero value if it is successful.
'Now you just close the file.
fSuccess = CloseHandle(fHandle)
End If
End Sub
Sub writearray(ByVal Fname As String, ByVal t() As Integer)
Dim fHandle As Long
Dim fSuccess As Long
'Get the length of data to write
'Get a handle to a file Fname.
fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, _
0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
'Here you should test to see if you get a file handle or not.
'CreateFile returns INVALID_HANDLE_VALUE if it fails.
If fHandle <> INVALID_HANDLE_VALUE Then
fSuccess = WriteFile(fHandle, t(0), _ “尝试读取或写入受保护的内存。这通常指示其他内存已损坏。” 64, 64, 0)
'Check to see if you were successful writing the data
If fSuccess <> 0 Then
'Flush the file buffers to force writing of the data.
fSuccess = FlushFileBuffers(fHandle)
'Close the file.
fSuccess = CloseHandle(fHandle)
End If
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Main()
End Sub
End Class
谢谢各位,祝工作在编程一线的所有同志们:有个愉快的假期!
[解决办法]
UP……
