Results 1 to 5 of 5

Thread: [2005] Win32 API Error: "Attempted to read or write protected memory."

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    [2005] Win32 API Error: "Attempted to read or write protected memory."

    Hey Guys,

    Trying to play with the Win32 API but am getting the following error:

    "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." {System.AccessViolationException}

    This occurs during a call to CreateFile():
    VB Code:
    1. sFullDrivePath = "\\.\" & sDriveLetter & ":"
    2.  
    3. hDevice = CreateFile(sFullDrivePath, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, vbNull, OPEN_EXISTING, 0, 0)

    I am attempting to open a USB disk but when I call CreateFile() to get my handle it fails...

    I am on a machine that is part of a domain, I am a local admin but not domain admin. Therefore I should have enough rights to perform this operation? You only need full admin to open a hard disk.

    Any ideas?
    Thanks,
    Matt.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] Win32 API Error: "Attempted to read or write protected memory."

    I assume this has to be a security issue with the framework.

    I am making an unmanaged call but the error I am receiving is caught by the managed code.

    How can I tell the framework to back off its security?

    Matt.

  3. #3
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: [2005] Win32 API Error: "Attempted to read or write protected memory."

    It is quite likely to be an error in your API declaration.
    Can you post that?

    Also - vbNull is not a .NET friendly thing at all.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] Win32 API Error: "Attempted to read or write protected memory."

    Hey Merrion,

    You got it! But didn't....

    I simply changed the vbNull to 0 and BOOM, my device handle is returned!

    So I was very happy for a second until I then tried to move the file pointer using SetFilePointer() and got the same error!

    VB Code:
    1. <DllImport("kernel32.dll")> _
    2.     Public Function SetFilePointer( _
    3.         ByVal hFile As Long, _
    4.         ByVal lDistanceToMove As Long, _
    5.         ByVal lpDistanceToMoveHigh As Long, _
    6.         ByVal dwMoveMethod As Long) As Long
    7.     End Function
    8.  
    9.     Public Const FILE_BEGIN As Long = 0
    10.     Public Const FILE_EOF As Long = &HFFFFFFFF

    Used like so:

    VB Code:
    1. If SetFilePointer(hDevice, (iSectorSize * iSectorNumber), 0, FILE_BEGIN) = FILE_EOF Then
    2.    MessageBox.Show("Unable to find sector " & iSectorNumber, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    3.    Return baDataBuffer
    4. End If

    This threw the same "Attempted to read or write....." error. Hmmmmmm

    Thanks Merrion,
    Matt.

  5. #5
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: [2005] Win32 API Error: "Attempted to read or write protected memory."

    OK - with the api declaration we can see a problem.

    In .NET an integer is 32 bits so replace Long with Integer:-

    VB Code:
    1. <DllImport("kernel32.dll")> _
    2.     Public Function SetFilePointer( _
    3.         ByVal hFile As Int32 , _
    4.         ByVal lDistanceToMove As int32, _
    5.         ByVal lpDistanceToMoveHigh As Int32, _
    6.         ByVal dwMoveMethod As Int32) As Int32
    7.     End Function
    8.  
    9.     Public Const FILE_BEGIN As Int32 = 0
    10.     Public Const FILE_EOF As int32 = &HFFFFFFFF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width