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

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.
trulseriAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
Please show unmanaged C function declaration.
0
omegaomegaDeveloperCommented:
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.)

Cheers,
Randy
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.