Pass pointer to allocated array to dll

WinstonSmith

New member
Joined
Feb 8, 2007
Messages
2
Programming Experience
3-5
I have a developer that has given me a C++ dll to use in my application (VB.NET windows forms). All is working well until he has a function that asks for a pointer to a buffer to store returned data in. His function call is this within the dll

extern "C" unsigned int __export __pascal ToolStatusRequest(short int*, int);

Written as such so i can access from VB.

In my app i have the following
Declaration

VB.NET:
Public Declare Function TOOLSTATUSREQUEST Lib "C:\athenadll\AthenaSpt.dll" (ByVal ptrBuff As IntPtr, ByVal sz As Integer) As Integer

Calling code
VB.NET:
Dim intBuff(SIZEOF_STATUS_REC) As Int16
'Create Gchandle instance and pin variable required
Dim MyGC As GCHandle = GCHandle.Alloc(intBuff, GCHandleType.Pinned)
 'get address of variable in pointer variable
Dim AddofLongValue As IntPtr = MyGC.AddrOfPinnedObject()
Dim shrtToolStatusRequest As Integer = TOOLSTATUSREQUEST(AddofLongValue.ToInt32, SIZEOF_STATUS_REC)

I get the following error message - Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
After help from here and hours of searching on the web it looked like i can grab the address of the allocated array and write to it but it fails everytime.

Can anyone tell me how to pass a pointer to the address of a writeable allocated array!!?

I will donate first born / soul etc etc

Cheers :D
 
Last edited:
Back
Top