Attempted to read or write protected memory.-VBForums
Results 1 to 4 of 4

Thread: Attempted to read or write protected memory.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    5

    Attempted to read or write protected memory.

    I am getting the error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" from a function call using a DLL. The DLL is not manged, and I believe that is causing part of the issue as from what I've found so far it seems like it normally isn't an issue in manged DLLs.

    The function declaration is:

    Code:
    Public Declare Function BISS_SetInterface Lib "BISS1SL_interface.dll" (ByVal ulBISSHandle As Long, ByVal ulInterface As Long, ByRef pcInterfaceOption As Char) As BISS_ErrorEnum
    and is being called:

    Code:
    ulError = Biss_Functions.BISS_SetInterface(ulBissHandle, BISS_InterfaceEnum.eBISS_MB3U, "")
    The dll is also a 32 bit dll, so I also changed the compile options to x86. I'm still new into .net programming, so any help would be greatly appreciated.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,459

    Re: Attempted to read or write protected memory.

    It's very, very likely the variables that are Long in that declaration should be Integer or, more appropriately, IntPtr.

    VB6 used 16-bit numbers for its Integer type in an era where most languages were using 32-bit numbers. So if VB6 needed to interface with a C library, it often used Long for those integers since that was VB6's 32-bit integer type.

    VB .NET uses 32-bit numbers for its Integer type. So you have to use Integer if you want a 32-bit Integer.

    However, philosophically speaking, if you're dealing with pointers you should use the IntPtr type. That'll be 32-bit OR 64-bit depending on the platform, and it can't be used to do some things Integer can do that you probably don't want to do in your .NET code. This is definitely true for ulBISSHandle, as any "handle" is definitely a pointer. I'm not sure if ulInterface is intended to be a pointer or not.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    5

    Re: Attempted to read or write protected memory.

    There is an open function that uses uiBISSHandle as a pointer and assigns it a value, and then that value is used in the other function calls. At least that is how it worked when I made something quick in vba using the dll. I'll play around with changing them to different types and see if that works.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    5

    Re: Attempted to read or write protected memory.

    It looks like that was the issue. I changed uiInterface to an int and the error went away. Thanks!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width