Help with exception "Attempted to read or write protected memory. This is often an indication that o

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
 Hi
Ive been trying to figure this out for a while now without any success and decided its time to try a post. Basically, Im trying to call a function from an unmanaged C++ dll. It works fine the first few times, but after a few minutes, I get the following exception:
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Here are the details of the function:
C++ function:
int someFunction(
long some_handle,
const char* query,
char* queryResponse,
unsigned int& queryResponseLength,
unsigned int timeout
);

C# wrapper:
[DllImport(DLL_NAME, SetLastError = true)]
internal static extern int someFunction(
uint someHandle,
string query,
byte[] queryResponse,
ref uint queryResponseLength,
uint timeout
);
- Note that The queryResponse and queryResponseLength parameters are used as output parameters.

Ive tried using a StringBuilder for the queryResponse Attribute, several combinations of [MarshalAs] tags and also tried pinning the parameters, but Im not sure how correct Ive been in any of these approaches. Any help would be greatly appreciated.
Thanks in advance

View the full article
 
Back
Top