Link to home
Create AccountLog in
Avatar of trulseri
trulseri

asked on

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.
Avatar of AlexFM
AlexFM

Please show unmanaged C function declaration.
ASKER CERTIFIED SOLUTION
Avatar of omegaomega
omegaomega
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer