Hi all
I am Vivek Bhola, working C#.net. I am trying to invoke a unmanaged code function in c#code. This function has prototype like:
public static extern int SsmRecToFileA(int ch, string pszFileName, int nFormat, int dwStartPos, int dwBytes, int dwTime, int nMask, LPRECTOMEM pfnCallbackA);
here pfnCallbackA is pointer to a callback function and it.s prototype and deffinition is:
public delegate void LPRECTOMEM(int ch, ref byte lpData, uint dwDataLen);
CardAPI.LPRECTOMEM pfnLiveListening = new CardAPI.LPRECTOMEM(LiveListening);
private static void LiveListening(int ch, ref byte lpData, uint dwDataLen)
{
try
{
byte[] VoiceBuffer = new byte[dwDataLen];
IntPtr ptrData = new IntPtr(lpData);
[B]Marshal.Copy(ptrData, VoiceBuffer, 0, Convert.ToInt32(dwDataLen));[/B]
if (CardAPI.SsmPlayMem(ch, 6, VoiceBuffer, dwDataLen, 0, dwDataLen) == -1)
{
MessageBox.Show(CardAPI.SsmGetLastErrMsgA());
}
}
catch (Exception exp)
{
}
}
when I call SsmRecToFileA function, after every 2 seconds callback function gets Called.
In this callback function i need to access the buffer pointed by lpData. So I copy the entire content on this address to a local buffer called voicebuffer through Marshal.copy method.
ACTUAL PROBLEM BEGINS NOW, at Bold line i.e Marshal.copy . It gives me an exception:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
and stack trace is :
at System.Runtime.InteropServices.Marshal.CopyToManaged(IntPtr source, Object destination, Int32 startIndex, Int32 length)
at System.Runtime.InteropServices.Marshal.Copy(IntPtr source, Byte[] destination, Int32 startIndex, Int32 length)
at ATP_Event.Form1.LiveListening(Int32 ch, Byte& lpData, UInt32 dwDataLen) in D:\My Projects\Synway-Digital\Copy of Synway Digital VL1.0.1\ATP_Event\Form1.cs:line 1214
Please tell where I am wrong? Please reply soon.