Attempted to read or write protected memory. Then calling function from external API (dll)

Posted on 2006-03-28
Last Modified: 2008-05-28
I'm using an dll and have declared some functions that I need to use:
Declare Function API_GetSeg Lib "MLAPI32.dll" (ByVal strSeg As String, ByVal Level As Integer, ByVal Threshold As Integer) As <MarshalAs(UnmanagedType.U1)> Boolean
Declare Function API_GetElm Lib "MLAPI32.dll" (ByVal strSegment As String, ByVal strElement As String, ByRef rData As String) As <MarshalAs(UnmanagedType.U1)> Boolean

The first function works fine (API_GetSeg).
I've got problems with the second function (API_GetElm) that gives the following error:
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

API_GetElm,  Variable rData is declared with ByRef because the function is going to change this value/return a string.

When I'm running the code with rData declared with ByVal, the error does not appear, but then I don't get my return value.

I think the dll is written in C.

How should I declare the function?

Please help.
Question by:trulseri
    LVL 48

    Expert Comment

    Please show unmanaged C function declaration.
    LVL 12

    Accepted Solution

    Hello, trulseri,

    I'm sorry that I don't know anything about the API you are calling.  Do you have any documantation for it?  With many calls to C functions it is typically necessary to preallocate the buffer before the call.  For example, if the call will return a maximum of 256 characters you need to pass in a buffer large enough to receive 256 characters.  Have you tried something like:

        Dim rDataBuffer As New String(" "c, 256)
        booResult = API_GetElm(..., rDataBuffer)

    where you are passing rDataBuffer ByVal?  (Of course, you have to know how large a string to allocate.  256 characters is just intended to be an example.)


    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now