Handling exception from unmanaged dll in C#

Posted by StuffHappens on Stack Overflow See other posts from Stack Overflow or by StuffHappens
Published on 2010-05-18T06:22:39Z Indexed on 2010/05/18 6:30 UTC
Read the original article Hit count: 656

Filed under:
|
|

Hello.
I have the following function written in C#

public static string GetNominativeDeclension(string surnameNamePatronimic)
{
    if(surnameNamePatronimic == null) 
       throw new ArgumentNullException("surnameNamePatronimic");

IntPtr[] ptrs = null;
try
{
    ptrs = StringsToIntPtrArray(surnameNamePatronimic);

    int resultLen = MaxResultBufSize;
    int err = decGetNominativePadeg(ptrs[0], ptrs[1], ref resultLen);
    ThrowException(err);
    return IntPtrToString(ptrs, resultLen);
}
catch
{
    return surnameNamePatronimic;
}
finally
{
    FreeIntPtr(ptrs);
}

}

Function decGetNominativePadeg is in unmanaged dll


[DllImport("Padeg.dll", EntryPoint = "GetNominativePadeg")]
private static extern Int32 decGetNominativePadeg(IntPtr surnameNamePatronimic,
    IntPtr result, ref Int32 resultLength);

and throws an exception: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The catch that is in C# code doesn't actually catch it. Why? How to handle this exception?
Thank you for your help!

© Stack Overflow or respective owner

Handling exception from unmanaged dll in C#

Posted by StuffHappens on Stack Overflow See other posts from Stack Overflow or by StuffHappens
Published on 2010-05-18T06:18:32Z Indexed on 2010/05/18 6:41 UTC
Read the original article Hit count: 656

Filed under:
|
|
|

Hello.
I have the following function written in C#

public static string GetNominativeDeclension(string surnameNamePatronimic)
{
    if(surnameNamePatronimic == null) 
       throw new ArgumentNullException("surnameNamePatronimic");

IntPtr[] ptrs = null;
try
{
    ptrs = StringsToIntPtrArray(surnameNamePatronimic);

    int resultLen = MaxResultBufSize;
    int err = decGetNominativePadeg(ptrs[0], ptrs[1], ref resultLen);
    ThrowException(err);
    return IntPtrToString(ptrs, resultLen);
}
catch
{
    return surnameNamePatronimic;
}
finally
{
    FreeIntPtr(ptrs);
}

}

Function decGetNominativePadeg is in unmanaged dll


[DllImport("Padeg.dll", EntryPoint = "GetNominativePadeg")]
private static extern Int32 decGetNominativePadeg(IntPtr surnameNamePatronimic,
    IntPtr result, ref Int32 resultLength);

and throws an exception: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The catch that is in C# code doesn't actually catch it. Why? How to handle this exception?
Thank you for your help! UPDATED


private static IntPtr[] StringsToIntPtrArray(params string[] strs)
{
    IntPtr[] ptrs = new IntPtr[strs.Length + 1];

for(int i = 0; i < ptrs.Length - 1; i++) ptrs[i] = StringToIntPtr(strs[i]);

ptrs[ptrs.Length - 1] = Marshal.AllocHGlobal(m_MaxResultStringBufSize);

return ptrs;

}

© Stack Overflow or respective owner

Related posts about c#

Related posts about unmanaged